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 preferrable to subclass AbstractCollection to create your own typed collections. For example:

namespace My\Foo;

class FooCollection extends \Ramsey\Collection\AbstractCollection
{
    public function __construct()
    {
        parent::__construct('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 instancof \My\Foo\FooCollection) {
    // the collection is a collection of My\Foo objects
}

Traits

Provides functionality to check values for specific types

Properties

protected array $data from AbstractArray

Methods

__construct(string $collectionType)

Constructs a collection object of the specified type

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

Returns true if this collection contains the specified element

string
getType()

Returns the type associated with this collection

bool
remove(mixed $element)

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

Details

__construct(string $collectionType)

Constructs a collection object of the specified type

Parameters

string $collectionType

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

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)

Returns true if this collection contains the specified element

This performs a strict type check on the value.

Parameters

mixed $element

Return Value

bool

string getType()

Returns the type associated with this collection

Return Value

string

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