PHP Class lithium\data\Collection

Inheritance: extends lithium\util\Collection
Show file Open project: unionofrad/lithium Class Usage Examples

Protected Properties

Property Type Description
$_autoConfig array Holds an array of values that should be processed on initialization.
$_exists boolean Indicates whether this array was part of a document loaded from a data source, or is part of a new document, or is in newly-added field of an existing document.
$_handlers array Hold the "data export" handlers where the keys are fully-namespaced class names, and the values are closures that take an instance of the class as a parameter, and return an array or scalar value that the instance represents.
$_model string The fully-namespaced class name of the model object to which this entity set is bound. This is usually the model that executed the query which created this object.
$_parent object A reference to this object's parent Document object.
$_pathKey string If this Collection instance has a parent document (see $_parent), this value indicates the key name of the parent document that contains it.
$_query object A reference to the query object that originated this entity set; usually an instance of lithium\data\model\Query.
$_result resource | object A pointer or resource that is used to load entities from the backend data source that originated this collection.
$_schema lithium\data\Schema If the Collection has a schema object assigned (rather than loading one from a model), it will be assigned here.
$_started boolean Set to true when the collection has begun iterating.
$_stats array Contains an array of backend-specific statistics generated by the query that produced this Collection object. These stats are accessible via the stats() method.
$_valid boolean Indicates whether the current position is valid or not. This overrides the default value of the parent class.

Public Methods

Method Description
__construct ( array $config = [] ) : void Constructor.
__destruct ( ) : void Destructor. Ensures that the data set's connection is closed when the object is destroyed.
assignTo ( object $parent, array $config = [] ) : void Configures protected properties of a Collection so that it is parented to $parent.
close ( ) Executes when the associated result resource pointer reaches the end of its data set. The resource is freed by the connection, and the reference to the connection is unlinked.
closed ( ) : boolean Checks to see if this entity has already fetched all available entities and freed the associated result resource.
current ( ) : object | boolean Returns the currently pointed to record in the set.
data ( ) : array Converts the current state of the data structure to an array.
each ( callback $filter ) : object Applies a callback to all data in the collection.
exists ( ) : boolean A flag indicating whether or not the items of this collection exists.
find ( mixed $filter, array $options = [] ) : mixed Overrides parent find() implementation to enable key/value-based filtering of entity objects contained in this collection.
first ( mixed $filter = null ) : object Overrides parent first() implementation to enable key/value-based filtering.
key ( boolean $full = false ) : mixed Returns the currently pointed to record's unique key.
keys ( ) : array Returns the item keys.
map ( callback $filter, array $options = [] ) : object Applies a callback to a copy of all data in the collection and returns the result.
meta ( ) : array Returns meta information for this Collection.
model ( ) : string Returns the model which this particular collection is based off of.
next ( ) : mixed Returns the next document in the set, and advances the object's internal pointer. If the end of the set is reached, a new document will be fetched from the data source connection handle If no more documents can be fetched, returns null.
offsetExists ( string $offset ) : boolean Returns a boolean indicating whether an offset exists for the current Collection.
offsetGet ( mixed $offset ) : mixed Gets an Entity object using PHP's array syntax, i.e. $documents[3] or $records[5].
offsetSet ( string $offset, mixed $data ) : mixed Adds the specified object to the Collection instance, and assigns associated metadata to the added object.
offsetUnset ( integer $offset ) Unsets an offset.
parent ( ) : object Returns the object's parent Document object.
reduce ( callback $filter, mixed $initial = false ) : mixed Reduce, or fold, a collection down to a single value
result ( ) : object Return's the pointer or resource that is used to load entities from the backend data source that originated this collection. This is useful in many cases for additional methods related to debugging queries.
rewind ( ) Rewinds the collection to the beginning.
schema ( $field = null )
serialize ( ) : string Prepares, enables and executes serialization of the object.
set ( arary $values ) : void Allows several items to be assigned at once.
sort ( string | callable $field = 'id', array $options = [] ) : lithium\data\Collection Sorts the objects in the collection, useful in situations where you are already using the underlying datastore to sort results.
stats ( string $name = null ) : mixed Gets the stat or stats associated with this Collection.
to ( string $format, array $options = [] ) : mixed Converts a Collection object to another type of object, or a simple type such as an array.
unserialize ( string $data ) : void Prepares, enables and executes unserialization of the object.
valid ( ) : boolean Checks if current position is valid.

Protected Methods

Method Description
_filterFromArray ( array $filter ) : Closure Creates a filter based on an array of key/value pairs that must match the items in a Collection.
_init ( )
_populate ( ) : mixed A method to be implemented by concrete Collection classes which, provided a reference to a backend data source, and a resource representing a query result cursor, fetches new result data and wraps it in the appropriate object type, which is added into the Collection and returned.
_set ( mixed $data = null, mixed $offset = null, array $options = [] ) : object A method to be implemented by concrete Collection classes which sets data to a specified offset and wraps all data array in its appropriate object type.

Method Details

__construct() public method

Constructor.
See also: lithium\data\Collection::$_data
See also: lithium\data\Collection::$_model
public __construct ( array $config = [] ) : void
$config array Available configuration options are: - `'data'` _array_ - `'model'` _string|null_
return void

__destruct() public method

Destructor. Ensures that the data set's connection is closed when the object is destroyed.
public __destruct ( ) : void
return void

_filterFromArray() protected method

Creates a filter based on an array of key/value pairs that must match the items in a Collection.
protected _filterFromArray ( array $filter ) : Closure
$filter array An array of key/value pairs used to filter `Collection` items.
return Closure Returns a closure that wraps the array and attempts to match each value against `Collection` item properties.

_init() protected method

protected _init ( )

_populate() abstract protected method

A method to be implemented by concrete Collection classes which, provided a reference to a backend data source, and a resource representing a query result cursor, fetches new result data and wraps it in the appropriate object type, which is added into the Collection and returned.
abstract protected _populate ( ) : mixed
return mixed Returns the next `Record`, `Document` object or other `Entity` object if exists. Returns `null` otherwise.

_set() abstract protected method

A method to be implemented by concrete Collection classes which sets data to a specified offset and wraps all data array in its appropriate object type.
See also: lithium\data\Collection::_populate()
See also: lithium\data\Collection::offsetSet()
abstract protected _set ( mixed $data = null, mixed $offset = null, array $options = [] ) : object
$data mixed An array or an `Entity` object to set.
$offset mixed The offset. If offset is `null` data is simply appended to the set.
$options array Any additional options to pass to the `Entity`'s constructor.
return object Returns the inserted `Record`, `Document` object or other `Entity` object.

assignTo() public method

Configures protected properties of a Collection so that it is parented to $parent.
public assignTo ( object $parent, array $config = [] ) : void
$parent object
$config array
return void

close() public method

Executes when the associated result resource pointer reaches the end of its data set. The resource is freed by the connection, and the reference to the connection is unlinked.
public close ( )

closed() public method

Checks to see if this entity has already fetched all available entities and freed the associated result resource.
public closed ( ) : boolean
return boolean Returns true if all entities are loaded and the database resources have been freed, otherwise returns false.

current() public method

Returns the currently pointed to record in the set.
public current ( ) : object | boolean
return object | boolean An instance of `Record` or `false` if there is no current valid one.

data() public method

Converts the current state of the data structure to an array.
public data ( ) : array
return array Returns the array value of the data in this `Collection`.

each() public method

Overridden to load any data that has not yet been loaded.
public each ( callback $filter ) : object
$filter callback The filter to apply.
return object This collection instance.

exists() public method

A flag indicating whether or not the items of this collection exists.
public exists ( ) : boolean
return boolean `True` if exists, `false` otherwise.

find() public method

Overrides parent find() implementation to enable key/value-based filtering of entity objects contained in this collection.
public find ( mixed $filter, array $options = [] ) : mixed
$filter mixed Callback to use for filtering, or array of key/value pairs which entity properties will be matched against.
$options array Options to modify the behavior of this method. See the documentation for the `$options` parameter of `lithium\util\Collection::find()`.
return mixed The filtered items. Will be an array unless `'collect'` is defined in the `$options` argument, then an instance of this class will be returned.

first() public method

Overrides parent first() implementation to enable key/value-based filtering.
public first ( mixed $filter = null ) : object
$filter mixed In addition to a callback (see parent), can also be an array where the keys and values must match the property values of the objects being inspected.
return object Returns the first object found matching the filter criteria.

key() public method

Returns the currently pointed to record's unique key.
public key ( boolean $full = false ) : mixed
$full boolean If true, returns the complete key.
return mixed

keys() public method

Returns the item keys.
public keys ( ) : array
return array The keys of the items.

map() public method

Overriden to load any data that has not yet been loaded.
public map ( callback $filter, array $options = [] ) : object
$filter callback The filter to apply.
$options array The available options are: - `'collect'`: If `true`, the results will be returned wrapped in a new `Collection` object or subclass.
return object The filtered data.

meta() public method

Returns meta information for this Collection.
public meta ( ) : array
return array

model() public method

Returns the model which this particular collection is based off of.
public model ( ) : string
return string The fully qualified model class name.

next() public method

Returns the next document in the set, and advances the object's internal pointer. If the end of the set is reached, a new document will be fetched from the data source connection handle If no more documents can be fetched, returns null.
public next ( ) : mixed
return mixed Returns the next document in the set, or `false`, if no more documents are available.

offsetExists() public method

Returns a boolean indicating whether an offset exists for the current Collection.
public offsetExists ( string $offset ) : boolean
$offset string String or integer indicating the offset or index of an entity in the set.
return boolean Result.

offsetGet() public method

Gets an Entity object using PHP's array syntax, i.e. $documents[3] or $records[5].
public offsetGet ( mixed $offset ) : mixed
$offset mixed The offset.
return mixed Returns an `Entity` object if exists otherwise returns `null`.

offsetSet() public method

Adds the specified object to the Collection instance, and assigns associated metadata to the added object.
public offsetSet ( string $offset, mixed $data ) : mixed
$offset string The offset to assign the value to.
$data mixed The entity object to add.
return mixed Returns the set `Entity` object.

offsetUnset() public method

Unsets an offset.
public offsetUnset ( integer $offset )
$offset integer The offset to unset.

parent() public method

Returns the object's parent Document object.
public parent ( ) : object
return object

reduce() public method

Overridden to load any data that has not yet been loaded.
public reduce ( callback $filter, mixed $initial = false ) : mixed
$filter callback The filter to apply.
$initial mixed Initial value
return mixed A single reduced value

result() public method

Return's the pointer or resource that is used to load entities from the backend data source that originated this collection. This is useful in many cases for additional methods related to debugging queries.
public result ( ) : object
return object The pointer or resource from the data source

rewind() public method

Rewinds the collection to the beginning.
public rewind ( )

schema() public method

public schema ( $field = null )

serialize() public method

Note: because of the limitations outlined below custom handlers are ignored with serialized objects. Pulls all results to entirely populate _data and closes the object freeing the associated result resource. This allows for skipping the _result property which may hold unserializable PDOStatements. Properties that hold anonymous functions are also skipped. Some of these can almost be reconstructed (_handlers) others cannot (_methodFilters).
public serialize ( ) : string
return string Serialized properties of the object.

set() public method

For example: $collection->set(array( 42 => array('title' => 'Lorem Ipsum'), 43 => array('title' => 'Dolor Amet') ));
public set ( arary $values ) : void
$values arary An associative array of fields and values to assign to the `Collection`.
return void

sort() public method

Overriden to load any data that has not yet been loaded.
See also: lithium\util\Collection::sort()
public sort ( string | callable $field = 'id', array $options = [] ) : lithium\data\Collection
$field string | callable The field to sort the data on, can also be a callback to a custom sort function.
$options array Reserved for future use.
return lithium\data\Collection Returns itself.

stats() public method

Gets the stat or stats associated with this Collection.
public stats ( string $name = null ) : mixed
$name string Stat name.
return mixed Single stat if `$name` supplied, else all stats for this `Collection`.

to() public method

The supported values of $format depend on the format handlers registered in the static property Collection::$_formats. The Collection class comes with built-in support for array conversion, but other formats may be registered. Once the appropriate handlers are registered, a Collection instance can be converted into any handler-supported format, i.e.: $collection->to('json'); // returns a JSON string $collection->to('xml'); // returns an XML string _Please note that Lithium does not ship with a default XML handler, but one can be configured easily._
See also: lithium\util\Collection::toArray()
See also: lithium\util\Collection::formats()
See also: lithium\util\Collection::$_formats
public to ( string $format, array $options = [] ) : mixed
$format string By default the only supported value is `'array'`. However, additional format handlers can be registered using the `formats()` method.
$options array Options for converting this collection: - `'internal'` _boolean_: Indicates whether the current internal representation of the collection should be exported. Defaults to `false`, which uses the standard iterator interfaces. This is useful for exporting record sets, where records are lazy-loaded, and the collection must be iterated in order to fetch all objects. - `'indexed'` _boolean|null_: Allows to control how converted data is keyed. When set to `true` will force indexed conversion of the collection (the default) even if the collection has a parent. When `false` will convert without indexing. Provide `null` as a value to this option to only index when the collection has no parent.
return mixed The object converted to the value specified in `$format`; usually an array or string.

unserialize() public method

Restores state of the object including pulled results. Tries to restore _handlers by calling into _init().
public unserialize ( string $data ) : void
$data string Serialized properties of the object.
return void

valid() public method

Checks if current position is valid.
public valid ( ) : boolean
return boolean `true` if valid, `false` otherwise.

Property Details

$_autoConfig protected property

Holds an array of values that should be processed on initialization.
protected array $_autoConfig
return array

$_exists protected property

Indicates whether this array was part of a document loaded from a data source, or is part of a new document, or is in newly-added field of an existing document.
protected bool $_exists
return boolean

$_handlers protected property

Hold the "data export" handlers where the keys are fully-namespaced class names, and the values are closures that take an instance of the class as a parameter, and return an array or scalar value that the instance represents.
See also: lithium\data\Collection::to()
protected array $_handlers
return array

$_model protected property

The fully-namespaced class name of the model object to which this entity set is bound. This is usually the model that executed the query which created this object.
protected string $_model
return string

$_parent protected property

A reference to this object's parent Document object.
protected object $_parent
return object

$_pathKey protected property

If this Collection instance has a parent document (see $_parent), this value indicates the key name of the parent document that contains it.
See also: lithium\data\Collection::$_parent
protected string $_pathKey
return string

$_query protected property

A reference to the query object that originated this entity set; usually an instance of lithium\data\model\Query.
See also: lithium\data\model\Query
protected object $_query
return object

$_result protected property

A pointer or resource that is used to load entities from the backend data source that originated this collection.
protected resource|object $_result
return resource | object

$_schema protected property

If the Collection has a schema object assigned (rather than loading one from a model), it will be assigned here.
See also: lithium\data\Schema
protected lithium\data\Schema $_schema
return lithium\data\Schema

$_started protected property

Set to true when the collection has begun iterating.
protected bool $_started
return boolean

$_stats protected property

Contains an array of backend-specific statistics generated by the query that produced this Collection object. These stats are accessible via the stats() method.
See also: lithium\data\Collection::stats()
protected array $_stats
return array

$_valid protected property

Indicates whether the current position is valid or not. This overrides the default value of the parent class.
See also: lithium\util\Collection::valid()
protected bool $_valid
return boolean