A Model Collection
For iterating instances of Charcoal\Model\ModelInterface
.
Used by Charcoal\Loader\CollectionLoader
for storing results.
The collection stores models by \Charcoal\Source\StorableInterface their primary key.
If two objects share the same storable ID but hold disparate data, they are considered
to be alike. Adding an object that shares the same ID as an object previously stored in
the collection will replace the latter.
Methods summary
public
|
#
__construct( array|Traversable|null $objs = null )
Create a new collection.
Parameters
- $objs
- Array of objects to pre-populate this collection.
|
public
object|null
|
#
first( )
Retrieve the first object in the collection.
Retrieve the first object in the collection.
Returns
object|null Returns the first object, or NULL if the collection is empty.
|
public
object|null
|
#
last( )
Retrieve the last object in the collection.
Retrieve the last object in the collection.
Returns
object|null Returns the last object, or NULL if the collection is empty.
|
public
Charcoal\Model\Collection
|
#
merge( array|Traversable $objs )
Merge the collection with the given objects.
Merge the collection with the given objects.
Parameters
- $objs
- Array of objects to append to this collection.
Returns
Throws
InvalidArgumentException If the given array contains an unacceptable value.
Implementation of
|
public
Charcoal\Model\Collection
|
#
add( object $obj )
Add an object to the collection.
Add an object to the collection.
Parameters
- $obj
- An acceptable object.
Returns
Throws
InvalidArgumentException If the given object is not acceptable.
Implementation of
|
public
object|null
|
#
get( mixed $key )
Retrieve the object by primary key.
Retrieve the object by primary key.
Parameters
Returns
object|null Returns the requested object or NULL if not in the collection.
Implementation of
|
public
boolean
|
#
has( mixed $key )
Determine if an object exists in the collection by key.
Determine if an object exists in the collection by key.
Parameters
- $key
- The primary key to lookup.
Returns
boolean
Implementation of
|
public
Charcoal\Model\Collection
|
#
remove( mixed $key )
Remove object from collection by primary key.
Remove object from collection by primary key.
Parameters
- $key
- The object primary key to remove.
Returns
Throws
InvalidArgumentException If the given key is not acceptable.
Implementation of
|
public
Charcoal\Model\Collection
|
#
clear( )
Remove all objects from collection.
Remove all objects from collection.
Returns
Implementation of
|
public
object[]
|
#
all( )
Retrieve all objects in collection indexed by primary keys.
Retrieve all objects in collection indexed by primary keys.
Returns
object[] An associative array of objects.
Implementation of
|
public
object[]
|
#
values( )
Retrieve all objects in the collection indexed numerically.
Retrieve all objects in the collection indexed numerically.
Returns
object[] A sequential array of objects.
Implementation of
|
public
array
|
#
keys( )
Retrieve the primary keys of the objects in the collection.
Retrieve the primary keys of the objects in the collection.
Returns
array A sequential array of keys.
Implementation of
|
public
boolean
|
|
public
mixed
|
#
offsetGet( mixed $offset )
Alias of CollectionInterface::get().
Parameters
- $offset
- The object primary key or array offset.
Returns
mixed Returns the requested object or NULL if not in the collection.
See
\ArrayAccess
|
public
|
#
offsetSet( mixed $offset, mixed $value )
Alias of CollectionInterface::set().
Parameters
- $offset
- The object primary key or array offset.
- $value
- The object.
Throws
LogicException Attempts to assign an offset.
See
\ArrayAccess
|
public
|
|
protected
integer
|
#
resolveOffset( integer $offset )
Parse the array offset.
If offset is non-negative, the sequence will start at that offset in the collection.
If offset is negative, the sequence will start that far from the end of the collection.
Parameters
- $offset
- The array offset.
Returns
integer Returns the resolved array offset.
|
public
integer
|
#
count( )
Get number of objects in collection
Get number of objects in collection
Returns
integer
See
\Countable
|
public
ArrayIterator
|
#
getIterator( )
Retrieve an external iterator.
Retrieve an external iterator.
Returns
ArrayIterator
See
\IteratorAggregate
|
public
CachingIterator
|
#
getCachingIterator( integer $flags = Charcoal\Model\CachingIterator::CALL_TOSTRING )
Retrieve a cached iterator.
Retrieve a cached iterator.
Parameters
Returns
CachingIterator
|
public
integer
|
#
pos( mixed $key )
Retrieve the array offset from the given key.
Retrieve the array offset from the given key.
Deprecated
Parameters
- $key
- The primary key to retrieve the offset from.
Returns
integer Returns an array offset.
|
public
object[]
|
|
public
object[]
|
|
public
boolean
|
#
isAcceptable( mixed $value )
Determine if the given value is acceptable for the collection.
Determine if the given value is acceptable for the collection.
Note: Practical for specialized collections extending the base collection.
Parameters
- $value
- The value being vetted.
Returns
boolean
|
protected
boolean
|
#
modelKey( object $obj )
Convert a given object into a model identifier.
Convert a given object into a model identifier.
Note: Practical for specialized collections extending the base collection.
Parameters
- $obj
- An acceptable object.
Returns
boolean
Throws
InvalidArgumentException If the given object is not acceptable.
|
public
boolean
|
#
isEmpty( )
Determine if the collection is empty or not.
Determine if the collection is empty or not.
Returns
boolean
|
public
Charcoal\Model\Collection
|
#
toBase( )
Get a base collection instance from this collection.
Get a base collection instance from this collection.
Note: Practical for extended classes.
Returns
|
protected
array
|
#
asArray( mixed $value )
Parse the given value into an array.
Parse the given value into an array.
Parameters
- $value
- The value being converted.
Returns
array
Link
|