Overview

Namespaces

  • Charcoal
    • Loader
    • Model
      • Service
      • ServiceProvider
    • Source
      • Database
    • Validator

Classes

  • AbstractMetadata
  • AbstractModel
  • Collection
  • Model
  • ModelMetadata
  • ModelValidator

Interfaces

  • CollectionInterface
  • DescribableInterface
  • MetadataInterface
  • ModelInterface

Traits

  • DescribableTrait
  • Overview
  • Namespace
  • Class

Class Collection

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.

Charcoal\Model\Collection implements Charcoal\Model\CollectionInterface
Namespace: Charcoal\Model
Located at Charcoal/Model/Collection.php
Methods summary
public
# __construct( array|Traversable|null $objs = null )

Create a new collection.

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

Charcoal\Model\Collection

Throws

InvalidArgumentException
If the given array contains an unacceptable value.

Implementation of

Charcoal\Model\CollectionInterface::merge()
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

Charcoal\Model\Collection

Throws

InvalidArgumentException
If the given object is not acceptable.

Implementation of

Charcoal\Model\CollectionInterface::add()
public object|null
# get( mixed $key )

Retrieve the object by primary key.

Retrieve the object by primary key.

Parameters

$key
The primary key.

Returns

object|null
Returns the requested object or NULL if not in the collection.

Implementation of

Charcoal\Model\CollectionInterface::get()
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

Charcoal\Model\CollectionInterface::has()
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

Charcoal\Model\Collection

Throws

InvalidArgumentException
If the given key is not acceptable.

Implementation of

Charcoal\Model\CollectionInterface::remove()
public Charcoal\Model\Collection
# clear( )

Remove all objects from collection.

Remove all objects from collection.

Returns

Charcoal\Model\Collection

Implementation of

Charcoal\Model\CollectionInterface::clear()
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

Charcoal\Model\CollectionInterface::all()
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

Charcoal\Model\CollectionInterface::values()
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

Charcoal\Model\CollectionInterface::keys()
public boolean
# offsetExists( mixed $offset )

Alias of CollectionInterface::has().

Alias of CollectionInterface::has().

Parameters

$offset
The object primary key or array offset.

Returns

boolean

See

\ArrayAccess
public mixed
# offsetGet( mixed $offset )

Alias of CollectionInterface::get().

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().

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
# offsetUnset( mixed $offset )

Alias of CollectionInterface::remove().

Alias of CollectionInterface::remove().

Parameters

$offset
The object primary key or array offset.

See

\ArrayAccess
protected integer
# resolveOffset( integer $offset )

Parse the array 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

$flags
Bitmask of flags.

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[]
# objects( )

Alias of self::values()

Alias of self::values()

Deprecated

Returns

object[]
public object[]
# map( )

Alias of self::all().

Alias of self::all().

Deprecated

Returns

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

Charcoal\Model\Collection
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

If an object is converted to an array, the result is an array whose elements are the object's properties.
Properties summary
protected object[] $objects

The objects contained in the collection.

The objects contained in the collection.

Stored as a dictionary indexed by each object's primary key. Ensures that each object gets loaded only once by keeping every loaded object in an associative array.

# []
API documentation generated by ApiGen