interface CollectionInterface implements ArrayInterface (View source)

A collection represents a group of objects, known as its elements.

Some collections allow duplicate elements and others do not. Some are ordered and others unordered.

Constants

SORT_ASC

Ascending sort type.

SORT_DESC

Descending sort type.

Methods

void
clear()

Removes all items from this array.

array
toArray()

Returns a native PHP array representation of this array object.

bool
isEmpty()

Returns true if this array is empty.

bool
add(mixed $element)

Ensures that this collection contains the specified element (optional operation).

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

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.

array
column(string $propertyOrMethod)

Returns the values from the given property or method.

mixed
first()

Returns the first item of the collection.

mixed
last()

Returns the last item of the collection.

sort(string $propertyOrMethod, string $order = self::SORT_ASC)

Sort the collection by a property or method with the given sort order.

filter(callable $callback)

Filter out items of the collection which don't match the criteria of given callback.

where(string $propertyOrMethod, mixed $value)

Create a new collection where items match the criteria of given callback.

map(callable $callback)

Apply a given callback method on each item of the collection.

diff(CollectionInterface $other)

Create a new collection with divergent items between current and given collection.

intersect(CollectionInterface $other)

Create a new collection with intersecting item between current and given collection.

merge(CollectionInterface ...$collections)

Merge current items and items of given collections into a new one.

Details

void clear()

Removes all items from this array.

Return Value

void

array toArray()

Returns a native PHP array representation of this array object.

Return Value

array

bool isEmpty()

Returns true if this array is empty.

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 The element to add to the collection.

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 The element to check whether the collection contains.
bool $strict Whether to perform a strict type check on the value.

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 The element to remove from the collection.

Return Value

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

array column(string $propertyOrMethod)

Returns the values from the given property or method.

Parameters

string $propertyOrMethod The property or method name to filter by.

Return Value

array

mixed first()

Returns the first item of the collection.

Return Value

mixed

mixed last()

Returns the last item of the collection.

Return Value

mixed

CollectionInterface sort(string $propertyOrMethod, string $order = self::SORT_ASC)

Sort the collection by a property or method with the given sort order.

This will always leave the original collection untouched and will return a new one.

Parameters

string $propertyOrMethod The property or method to sort by.
string $order The sort order for the resulting collection (one of this interface's SORT_* constants).

Return Value

CollectionInterface

CollectionInterface filter(callable $callback)

Filter out items of the collection which don't match the criteria of given callback.

This will always leave the original collection untouched and will return a new one.

See the {@link http://php.net/manual/en/function.array-filter.php PHP array_filter() documentation} for examples of how the $callback parameter works.

Parameters

callable $callback A callable to use for filtering elements.

Return Value

CollectionInterface

CollectionInterface where(string $propertyOrMethod, mixed $value)

Create a new collection where items match the criteria of given callback.

This will always leave the original collection untouched and will return a new one.

Parameters

string $propertyOrMethod The property or method to evaluate.
mixed $value The value to match.

Return Value

CollectionInterface

CollectionInterface map(callable $callback)

Apply a given callback method on each item of the collection.

This will always leave the original collection untouched and will return a new one.

See the {@link http://php.net/manual/en/function.array-map.php PHP array_map() documentation} for examples of how the $callback parameter works.

Parameters

callable $callback A callable to apply to each item of the collection.

Return Value

CollectionInterface

CollectionInterface diff(CollectionInterface $other)

Create a new collection with divergent items between current and given collection.

Parameters

CollectionInterface $other The collection to check for divergent items.

Return Value

CollectionInterface

CollectionInterface intersect(CollectionInterface $other)

Create a new collection with intersecting item between current and given collection.

Parameters

CollectionInterface $other The collection to check for intersecting items.

Return Value

CollectionInterface

CollectionInterface merge(CollectionInterface ...$collections)

Merge current items and items of given collections into a new one.

Parameters

CollectionInterface ...$collections The collections to merge.

Return Value

CollectionInterface