class Collection extends AbstractCollection (View source)

A collection represents a group of objects. Each object in the collection is of a specific, defined type.

This is a direct implementation of CollectionInterface, provided for the sake of convenience.

Example usage:

$collection = new \Ramsey\Collection\Collection('My\\Foo');
$collection->add(new \My\Foo());
$collection->add(new \My\Foo());

foreach ($collection as $foo) {
    // Do something with $foo
}

It is preferable to subclass AbstractCollection to create your own typed collections. For example:

namespace My\Foo;

class FooCollection extends \Ramsey\Collection\AbstractCollection
{
    public function getType()
    {
        return 'My\\Foo';
    }
}

And then use it similarly to the earlier example:

$fooCollection = new \My\Foo\FooCollection();
$fooCollection->add(new \My\Foo());
$fooCollection->add(new \My\Foo());

foreach ($fooCollection as $foo) {
    // Do something with $foo
}

The benefit with this approach is that you may do type-checking on the collection object:

if ($collection instanceof \My\Foo\FooCollection) {
    // the collection is a collection of My\Foo objects
}

Traits

Provides functionality to check values for specific types
Provides functionality to express a value as string

Properties

protected array $data from AbstractArray

Methods

__construct(string $collectionType, array $data = [])

Constructs a collection object of the specified type, optionally with the specified data

getIterator()

Returns a new iterator from this array

bool
offsetExists(mixed $offset)

Checks whether the specified offset exists in the array

mixed
offsetGet(mixed $offset)

Returns the value stored at the specified offset in the array

offsetSet(mixed $offset, mixed $value)

Sets the specified offset in the map with the given value

offsetUnset(mixed $offset)

Removes the specified offset and its value from the map

string
serialize()

Converts this map object to a string when the object is serialized with serialize()

unserialize(string $serialized)

Re-constructs the object from its serialized form

int
count()

Returns the number of elements contained in this array

void
clear()

Remove all the elements from this array object

toArray()

Returns a native PHP array containing all of the elements in this array object

bool
isEmpty()

Returns true if this array object contains no elements

bool
checkType(string $type, mixed $value)

Returns true if value is of the specified type

from TypeTrait
string
toolValueToString(mixed $value)

Return a string with the information of the value null value: NULL boolean: TRUE, FALSE array: Array scalar: converted-value resource: (type resource #number) object with __toString(): result of __toString() object DateTime: ISO 8601 date object: (className Object) anonymous function: same as object

bool
add(mixed $element)

Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

bool
contains(mixed $element, bool $strict = true)

Returns true if this collection contains the specified element

bool
remove(mixed $element)

Removes a single instance of the specified element from this collection, if it is present

string
getType()

Returns the type associated with this collection

Details

__construct(string $collectionType, array $data = [])

Constructs a collection object of the specified type, optionally with the specified data

Parameters

string $collectionType
array $data

ArrayIterator getIterator()

Returns a new iterator from this array

Return Value

ArrayIterator

bool offsetExists(mixed $offset)

Checks whether the specified offset exists in the array

Parameters

mixed $offset

Return Value

bool

mixed offsetGet(mixed $offset)

Returns the value stored at the specified offset in the array

Parameters

mixed $offset

Return Value

mixed

offsetSet(mixed $offset, mixed $value)

Sets the specified offset in the map with the given value

Parameters

mixed $offset
mixed $value

Exceptions

InvalidArgumentException

offsetUnset(mixed $offset)

Removes the specified offset and its value from the map

Parameters

mixed $offset

string serialize()

Converts this map object to a string when the object is serialized with serialize()

Return Value

string

unserialize(string $serialized)

Re-constructs the object from its serialized form

Parameters

string $serialized

int count()

Returns the number of elements contained in this array

Return Value

int

void clear()

Remove all the elements from this array object

Return Value

void

toArray()

Returns a native PHP array containing all of the elements in this array object

bool isEmpty()

Returns true if this array object contains no elements

Return Value

bool

protected bool checkType(string $type, mixed $value)

Returns true if value is of the specified type

Parameters

string $type
mixed $value

Return Value

bool

protected string toolValueToString(mixed $value)

Return a string with the information of the value null value: NULL boolean: TRUE, FALSE array: Array scalar: converted-value resource: (type resource #number) object with __toString(): result of __toString() object DateTime: ISO 8601 date object: (className Object) anonymous function: same as object

Parameters

mixed $value

Return Value

string

bool add(mixed $element)

Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.

If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.

Parameters

mixed $element

Return Value

bool true if this collection changed as a result of the call

bool contains(mixed $element, bool $strict = true)

Returns true if this collection contains the specified element

Parameters

mixed $element
bool $strict Whether to perform a strict type check on the value.

Return Value

bool

bool remove(mixed $element)

Removes a single instance of the specified element from this collection, if it is present

Parameters

mixed $element

Return Value

bool true if an element was removed as a result of this call

string getType()

Returns the type associated with this collection

Return Value

string