Nymph Server API  3.0.0
Data objects for JavaScript and PHP.
Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Nymph\Nymph Class Reference

An object relational mapper with PHP and JavaScript interfaces. More...

Static Public Member Functions

static __callStatic ($name, $args)
 
static configure ($config=[])
 Apply configuration to Nymph. More...
 
static checkData (&$data, &$sdata, $selectors, $guid=null, $tags=null, $typesAlreadyChecked=[], $dataValsAreadyChecked=[])
 Check entity data to see if it matches given selectors. More...
 
static deleteEntity (&$entity)
 Delete an entity from the database. More...
 
static saveEntity (&$entity)
 Save an entity to the database. More...
 
static hsort (&$array, $property=null, $parentProperty=null, $caseSensitive=false, $reverse=false)
 Sort an array of entities hierarchically by a specified property's value. More...
 
static psort (&$array, $property=null, $parentProperty=null, $caseSensitive=false, $reverse=false)
 Sort an array of entities by parent and a specified property's value. More...
 
static sort (&$array, $property=null, $caseSensitive=false, $reverse=false)
 Sort an array of entities by a specified property's value. More...
 
static formatSelectors (&$selectors)
 Make all selectors in the format: More...
 
static connect ()
 Connect to the database. More...
 
static deleteEntityByID ($guid, $className=null)
 Delete an entity by its GUID. More...
 
static deleteUID ($name)
 Delete a unique ID. More...
 
static disconnect ()
 Disconnect from the database. More...
 
static export ($filename)
 Export entities to a local file. More...
 
static exportPrint ()
 Export entities to the client as a downloadable file. More...
 
static getEntities ($options=[],... $selectors)
 Get an array of entities. More...
 
static getEntity ($options=[],... $selectors)
 Get the first entity to match all options/selectors. More...
 
static getUID ($name)
 Get the current value of a unique ID. More...
 
static import ($filename)
 Import entities from a file. More...
 
static newUID ($name)
 Increment or create a unique ID and return the new value. More...
 
static renameUID ($oldName, $newName)
 Rename a unique ID. More...
 
static setUID ($name, $value)
 Set the value of a UID. More...
 

Public Attributes

const VERSION = '3.0.0'
 

Static Public Attributes

static $config
 
static $driver
 

Detailed Description

An object relational mapper with PHP and JavaScript interfaces.

Written by Hunter Perrin for SciActive.

Author
Hunter Perrin hperr[email protected][email protected][email protected]mail.[email protected]com
See also
http://nymph.io/

Definition at line 14 of file Nymph.php.

Member Function Documentation

◆ checkData()

static Nymph\Nymph::checkData ( $data,
$sdata,
  $selectors,
  $guid = null,
  $tags = null,
  $typesAlreadyChecked = [],
  $dataValsAreadyChecked = [] 
)
static

Check entity data to see if it matches given selectors.

Parameters
array$dataAn array of unserialized entity data. The data array should contain the cdate and mdate.
array$sdataAn array of serialized entity data. If a value here is checked, it will be unserialized and placed in the $data array.
array$selectorsAn array of formatted selectors.
int | null$guidThe guid. If left null, guid will not be checked, and automatically considered passing.
array | null$tagsThe tags array. If left null, tags will not be checked, and automatically considered passing.
array$typesAlreadyCheckedAn array of clause types that have already been checked. They will be considered passing.
array$dataValsAreadyCheckedAn array of data values that have already been checked. They will be considered passing if the value is identical.
Returns
boolean Whether the entity data passes the given selectors.

Definition at line 97 of file Nymph.php.

◆ configure()

static Nymph\Nymph::configure (   $config = [])
static

Apply configuration to Nymph.

$config should be an associative array of Nymph configuration. Use the following form:

[ 'driver' => 'MySQL', 'pubsub' => true, 'MySql' => [ 'host' => '127.0.0.1' ] ]

Parameters
array$configAn associative array of Nymph's configuration.

Definition at line 51 of file Nymph.php.

◆ connect()

static Nymph\Nymph::connect ( )
static

Connect to the database.

Returns
bool Whether the instance is connected to the database.

Definition at line 255 of file Nymph.php.

◆ deleteEntity()

static Nymph\Nymph::deleteEntity ( $entity)
static

Delete an entity from the database.

Parameters
Entity&$entityThe entity to delete.
Returns
bool True on success, false on failure.

Definition at line 123 of file Nymph.php.

◆ deleteEntityByID()

static Nymph\Nymph::deleteEntityByID (   $guid,
  $className = null 
)
static

Delete an entity by its GUID.

Parameters
int$guidThe GUID of the entity.
string$classNameThe class of the entity.
Returns
bool True on success, false on failure.

Definition at line 265 of file Nymph.php.

◆ deleteUID()

static Nymph\Nymph::deleteUID (   $name)
static

Delete a unique ID.

Parameters
string$nameThe UID's name.
Returns
bool True on success, false on failure.

Definition at line 274 of file Nymph.php.

◆ disconnect()

static Nymph\Nymph::disconnect ( )
static

Disconnect from the database.

Returns
bool Whether the instance is connected to the database.

Definition at line 282 of file Nymph.php.

◆ export()

static Nymph\Nymph::export (   $filename)
static

Export entities to a local file.

This is the file format:

# Comments begin with #
   # And can have white space before them.
# This defines a UID.
<name/of/uid>[5]
<another uid>="">[8000]
# For UIDs, the name is in angle brackets (<>) and the value follows
# in square brackets ([]).
# This starts a new entity.
{1}<entity_etype>[tag,list,with,commas]
# For entities, the GUID is in curly brackets ({}), then the etype in
#  angle brackets, then the comma separated tag list follows in square
#  brackets ([]).
# Variables are stored like this:
# varname=json_encode(serialize(value))
    abilities="a:1:{i:0;s:10:\"system\/all";}"
    groups="a:0:{}"
    inheritAbilities="b:0;"
    name="s:5:"admin";"
# White space before/after "=" and at beginning/end of line is ignored.
        username  =     "s:5:"admin";"
{2}<entity_etype>[tag,list]
    another="s:23:\"This is another entity.";"
    newline="s:1:"
";"
Parameters
string$filenameThe file to export to.
Returns
bool True on success, false on failure.

Definition at line 319 of file Nymph.php.

◆ exportPrint()

static Nymph\Nymph::exportPrint ( )
static

Export entities to the client as a downloadable file.

Returns
bool True on success, false on failure.

Definition at line 327 of file Nymph.php.

◆ formatSelectors()

static Nymph\Nymph::formatSelectors ( $selectors)
static

Make all selectors in the format:

[ 0 => '&', 'clause' => [ ['value'] ], 'clause2' => [ ['var', 'value'] ], [ 0 => '|', 'clause' => [ ['value2'] ] ] ]

Parameters
array$selectors

Definition at line 244 of file Nymph.php.

◆ getEntities()

static Nymph\Nymph::getEntities (   $options = [],
  $selectors 
)
static

Get an array of entities.

$options is an associative array, which contains any of the following settings (in the form $options['name'] = value):

  • class - (string) The class to create each entity with.
  • limit - (int) The limit of entities to be returned.
  • offset - (int) The offset from the oldest matching entity to start retrieving.
  • reverse - (bool) If true, entities will be retrieved from newest to oldest. Therefore, offset will be from the newest entity.
  • sort - (string) How to sort the entities. Accepts "guid", "cdate", and "mdate". Defaults to "cdate".
  • return - (string) What to return. "entity" or "guid". Defaults to "entity".
  • source - (string) Will be 'client' if the query came from a REST call.
  • skip_cache - (bool) If true, Nymph will skip the cache and retrieve the entity from the DB.
  • skip_ac - (bool) If true, Tilmeld will not filter returned entities according to access controls. (If Tilmeld is installed.) (This is Always set to false by the REST endpoint.)

If a class is specified, it must have a factory() static method that returns a new instance.

Selectors are also associative arrays. Any amount of selectors can be provided. Empty selectors will be ignored. The first member of a selector must be a "type" string. The type string can be:

  • & - (and) All values in the selector must be true.
  • | - (or) At least one value in the selector must be true.
  • !& - (not and) All values in the selector must be false.
  • !| - (not or) At least one value in the selector must be false.

The rest of the entries in the selector are either more selectors or associative entries called selector clauses, which can be any of the following (in the form $selector['name'] = value, or $selector['name'] = [value1, value2,...]):

  • guid - A GUID. True if the entity's GUID is equal.
  • tag - A tag. True if the entity has the tag.
  • isset - A name. True if the named property exists and is not null.
  • equal - An array with a name, then value. True if the named property is defined and equal.
  • data (deprecated) - An alias for equal.
  • strict - An array with a name, then value. True if the named property is defined and identical.
  • array - An array with a name, then value. True if the named property is an array containing the value. Uses in_array().
  • match - An array with a name, then regular expression. True if the named property matches. Uses preg_match(). More powerful than "pmatch" but much slower. Must be surrounded by "/" delimiters.
  • pmatch - An array with a name, then regular expression. True if the named property matches. Uses POSIX RegExp. Case sensitive. Faster than "match". Must not be surrounded by any delimiters.
  • ipmatch - An array with a name, then regular expression. True if the named property matches. Uses POSIX RegExp. Case insensitive. Faster than "match". Must not be surrounded by any delimiters.
  • like - An array with a name, then pattern. True if the named property matches. Uses % for variable length wildcard and _ for single character wildcard. Case sensitive.
  • ilike - An array with a name, then pattern. True if the named property matches. Uses % for variable length wildcard and _ for single character wildcard. Case insensitive.
  • gt - An array with a name, then value. True if the named property is greater than the value.
  • gte - An array with a name, then value. True if the named property is greater than or equal to the value.
  • lt - An array with a name, then value. True if the named property is less than the value.
  • lte - An array with a name, then value. True if the named property is less than or equal to the value.
  • ref - An array with a name, then either an entity, or a GUID. True if the named property is the entity or an array containing the entity.

These clauses can all be negated, by prefixing them with an exclamation point, such as "!isset".

Any clause that accepts an array of name and value can also accept a third element. If value is null and the third element is a string, the third element will be used with PHP's strtotime function to set value to a relative timestamp. For example, the following selector will look for all entities that were created in the last day:

[
  '&',
  'gte' => ['cdate', null, '-1 day']
]

This example will retrieve the last two entities where:

  • It has 'person' tag.
  • spouse exists and is not null.
  • gender is male and lname is Smith.
  • warnings is not an integer 0.
  • It has 'level1' and 'level2' tags, or it has 'access1' and 'access2' tags.
  • It has either 'employee' or 'manager' tag.
  • name is either Clark, James, Chris, Christopher, Jake, or Jacob.
  • If age is 22 or more, then pay is not greater than 8.
$entities = \Nymph\Nymph::getEntities(
  ['reverse' => true, 'limit' => 2],
  [
    '&', // all must be true
    'tag' => 'person',
    'isset' => 'spouse',
    'equal' => [
      ['gender', 'male'],
      ['lname', 'Smith']
    ],
    '!strict' => ['warnings', 0]
  ],
  [
    '|', // at least one of the selectors in this must evaluate to true
    [
      '&',
      'tag' => ['level1', 'level2']
    ],
    [
      '&',
      'tag' => ['access1', 'access2']
    ]
  ],
  [
    '|', // at least one must be true
    'tag' => ['employee', 'manager']
  ],
  [
    '|',
    'equal' => [
      ['name', 'Clark'],
      ['name', 'James']
    ],
    'pmatch' => [
      ['name', 'Chris(topher)?'],
      ['name', 'Ja(ke|cob)']
    ]
  ],
  [
    '!|', // at least one must be false
    'gte' => ['age', 22],
    'gt' => ['pay', 8]
  ]
);
Parameters
array$optionsThe options.
array$selectorsUnlimited optional selectors to search for. If none are given, all entities are retrieved for the given options.
array$selectors,...
Returns
array|null An array of entities, or null on failure.
Todo:

An option to place a total count in a var.

Use an asterisk to specify any variable.

Definition at line 490 of file Nymph.php.

◆ getEntity()

static Nymph\Nymph::getEntity (   $options = [],
  $selectors 
)
static

Get the first entity to match all options/selectors.

$options and $selectors are the same as in getEntities().

This function is equivalent to setting $options['limit'] to 1 for getEntities(), except that it will return null if no entity is found. getEntities() would return an empty array.

Parameters
array$optionsThe options to search for.
array | int$selectorsUnlimited optional selectors to search for, or just a GUID.
array$selectors,...
Returns
Entity|null An entity, or null on failure and nothing found.

Definition at line 508 of file Nymph.php.

◆ getUID()

static Nymph\Nymph::getUID (   $name)
static

Get the current value of a unique ID.

Parameters
string$nameThe UID's name.
Returns
int|null The UID's value, or null on failure and if it doesn't exist.

Definition at line 518 of file Nymph.php.

◆ hsort()

static Nymph\Nymph::hsort ( $array,
  $property = null,
  $parentProperty = null,
  $caseSensitive = false,
  $reverse = false 
)
static

Sort an array of entities hierarchically by a specified property's value.

Entities will be placed immediately after their parents. The $parentProperty property must hold either null, or the entity's parent.

Parameters
array&$arrayThe array of entities.
string | null$propertyThe name of the property to sort entities by.
string | null$parentPropertyThe name of the property which holds the parent of the entity.
bool$caseSensitiveSort case sensitively.
bool$reverseReverse the sort order.

Definition at line 156 of file Nymph.php.

◆ import()

static Nymph\Nymph::import (   $filename)
static

Import entities from a file.

Parameters
string$filenameThe file to import from.
Returns
bool True on success, false on failure.

Definition at line 527 of file Nymph.php.

◆ newUID()

static Nymph\Nymph::newUID (   $name)
static

Increment or create a unique ID and return the new value.

Unique IDs, or UIDs, are ID numbers, similar to GUIDs, but without any constraints on how they are used. UIDs can be named anything. A good naming convention, in order to avoid conflicts, is to use your component's name, a slash, then a descriptive name of the sequence being identified for non-entity sequences, and the name of the entity's class for entity sequences. E.g. "com_example/widget_hits" or "com_hrm_employee".

A UID can be used to identify an object when the GUID doesn't suffice. On a system where a new entity is created many times per second, referring to something by its GUID may be unintuitive. However, the component designer is responsible for assigning UIDs to the component's entities. Beware that if a UID is incremented for an entity, and the entity cannot be saved, there is no safe, and therefore, no recommended way to decrement the UID back to its previous value.

If newUID() is passed the name of a UID which does not exist yet, one will be created with that name, and assigned the value 1. If the UID already exists, its value will be incremented. The new value will be returned.

Parameters
string$nameThe UID's name.
Returns
int|null The UID's new value, or null on failure.

Definition at line 557 of file Nymph.php.

◆ psort()

static Nymph\Nymph::psort ( $array,
  $property = null,
  $parentProperty = null,
  $caseSensitive = false,
  $reverse = false 
)
static

Sort an array of entities by parent and a specified property's value.

Entities' will be sorted by their parents' properties, then the entities' properties.

Parameters
array&$arrayThe array of entities.
string | null$propertyThe name of the property to sort entities by.
string | null$parentPropertyThe name of the property which holds the parent of the entity.
bool$caseSensitiveSort case sensitively.
bool$reverseReverse the sort order.

Definition at line 185 of file Nymph.php.

◆ renameUID()

static Nymph\Nymph::renameUID (   $oldName,
  $newName 
)
static

Rename a unique ID.

Parameters
string$oldNameThe old name.
string$newNameThe new name.
Returns
bool True on success, false on failure.

Definition at line 567 of file Nymph.php.

◆ saveEntity()

static Nymph\Nymph::saveEntity ( $entity)
static

Save an entity to the database.

If the entity has never been saved (has no GUID), a variable "cdate" is set on it with the current Unix timestamp using microtime(true).

The variable "mdate" is set to the current Unix timestamp using microtime(true).

Parameters
mixed&$entityThe entity.
Returns
bool True on success, false on failure.

Definition at line 139 of file Nymph.php.

◆ setUID()

static Nymph\Nymph::setUID (   $name,
  $value 
)
static

Set the value of a UID.

Parameters
string$nameThe UID's name.
int$valueThe value.
Returns
bool True on success, false on failure.

Definition at line 577 of file Nymph.php.

◆ sort()

static Nymph\Nymph::sort ( $array,
  $property = null,
  $caseSensitive = false,
  $reverse = false 
)
static

Sort an array of entities by a specified property's value.

Parameters
array&$arrayThe array of entities.
string | null$propertyThe name of the property to sort entities by.
bool$caseSensitiveSort case sensitively.
bool$reverseReverse the sort order.

Definition at line 209 of file Nymph.php.


The documentation for this class was generated from the following file: