Collection
in package
implements
ExCollectionInterface
uses
CollectionTrait
Collection of items. Inspired by Laravel Collections.
Tags
Table of Contents
Interfaces
Properties
- $class : string
- Class type allowed into the collection.
- $discrim : string|null
- The collection discriminator.
- $items : array<string|int, mixed>
- The items contained in the collection.
Methods
- __construct() : mixed
- Create a new Collection.
- __debugInfo() : array<string|int, mixed>
- Returns an item that will be displayed for debugging.
- __serialize() : array<string|int, mixed>
- Returns the string representation of the collection.
- __unserialize() : void
- Unserializes the collection.
- all() : bool
- Checks if all items match the callback.
- any() : bool
- Checks if any item matches the callback.
- clear() : void
- Clears the collection.
- collect() : ExCollectionInterface
- Converts the items into a new collection.
- count() : int
- Counts the amount of objects in the collection.
- diff() : ExCollectionInterface
- Gets the difference between the items.
- fill() : self
- Fills an array of items into the collection.
- filter() : ExCollectionInterface
- Runs a filter callback over the collection and returns a new Collection based on the response of the callback.
- find() : mixed
- Runs a filter callback over the collection and returns the first item where the callback returns `true` when given the item.
- find_key() : mixed
- Finds the key of the first item that matches the callback.
- first() : mixed
- Returns the first element of the collection.
- for() : ExCollectionInterface
- Creates a collection for a class.
- from() : ExCollectionInterface
- Creates a collection from an array.
- get() : mixed
- Gets an item from the collection.
- getIterator() : Traversable
- Returns an iterator for the collection.
- has() : bool
- Checks if the array has multiple offsets.
- intersect() : ExCollectionInterface
- Gets the intersection of the items.
- isset() : bool
- If the collection has an offset.
- jsonSerialize() : array<string|int, mixed>
- Serializes the object to a value that can be serialized natively by json_encode().
- keys() : array<string|int, int>|array<string|int, string>
- last() : mixed
- Returns the last element of the collection.
- map() : ExCollectionInterface
- Runs a callback over the collection and creates a new Collection.
- merge() : self
- Merges another collection into this collection.
- offsetExists() : bool
- If the collection has an offset.
- offsetGet() : mixed
- Gets an item from the collection.
- offsetSet() : void
- Sets an item into the collection.
- offsetUnset() : void
- Unsets an index from the collection.
- pull() : mixed
- Pulls an item from the collection.
- push() : self
- Pushes items to the collection.
- pushItem() : self
- Pushes a single item to the collection.
- reduce() : ExCollectionInterface
- Reduces the collection to a single value using a callback function.
- search() : string|int|false
- Searches for a given value within the collection and returns the corresponding key if successful.
- serialize() : string
- Returns the string representation of the collection.
- set() : mixed
- Sets a value in the collection.
- shift() : mixed
- Shifts an item from the collection.
- slice() : ExCollectionInterface
- Slices the collection.
- sort() : ExCollectionInterface
- Sort through each item with a callback.
- splice() : self
- Splices the collection, removing a portion of the items and replacing them with the given replacement.
- toArray() : array<string|int, mixed>
- Converts the collection to an array.
- unique() : ExCollectionInterface
- Returns unique items.
- unserialize() : void
- Unserializes the collection.
- values() : array<string|int, mixed>
- Get the values of the items.
- walk() : ExCollectionInterface
- Applies the given callback function to each item in the collection.
Properties
$class
Class type allowed into the collection.
protected
string
$class
$discrim
The collection discriminator.
protected
string|null
$discrim
$items
The items contained in the collection.
protected
array<string|int, mixed>
$items
Methods
__construct()
Create a new Collection.
public
__construct([array<string|int, mixed> $items = [] ][, string|null $discrim = 'id' ][, string|null $class = null ]) : mixed
Parameters
- $items : array<string|int, mixed> = []
- $discrim : string|null = 'id'
- $class : string|null = null
__debugInfo()
Returns an item that will be displayed for debugging.
public
__debugInfo() : array<string|int, mixed>
Return values
array<string|int, mixed>__serialize()
Returns the string representation of the collection.
public
__serialize() : array<string|int, mixed>
Return values
array<string|int, mixed>__unserialize()
Unserializes the collection.
public
__unserialize(CollectionInterface|array<string|int, mixed> $data) : void
Parameters
- $data : CollectionInterface|array<string|int, mixed>
all()
Checks if all items match the callback.
public
all(callable $callback) : bool
Parameters
- $callback : callable
Return values
boolany()
Checks if any item matches the callback.
public
any(callable $callback) : bool
Parameters
- $callback : callable
Return values
boolclear()
Clears the collection.
public
clear() : void
collect()
Converts the items into a new collection.
public
collect() : ExCollectionInterface
Return values
ExCollectionInterfacecount()
Counts the amount of objects in the collection.
public
count() : int
Return values
intdiff()
Gets the difference between the items.
public
diff(mixed $items[, callable|null $callback = null ]) : ExCollectionInterface
If a callback is provided and is callable, it uses array_udiff_assoc
to compute the difference.
Otherwise, it uses array_diff
.
Parameters
- $items : mixed
- $callback : callable|null = null
Return values
ExCollectionInterfacefill()
Fills an array of items into the collection.
public
fill(CollectionInterface|array<string|int, mixed> $items) : self
Parameters
- $items : CollectionInterface|array<string|int, mixed>
Return values
selffilter()
Runs a filter callback over the collection and returns a new Collection based on the response of the callback.
public
filter(callable $callback) : ExCollectionInterface
Parameters
- $callback : callable
Tags
Return values
ExCollectionInterfacefind()
Runs a filter callback over the collection and returns the first item where the callback returns `true` when given the item.
public
find(callable $callback) : mixed
Parameters
- $callback : callable
Return values
mixed —null
if no items returns true
when called in the $callback
.
find_key()
Finds the key of the first item that matches the callback.
public
find_key(callable $callback) : mixed
Parameters
- $callback : callable
first()
Returns the first element of the collection.
public
first() : mixed
for()
Creates a collection for a class.
public
static for(string $class[, string|null $discrim = 'id' ]) : ExCollectionInterface
Parameters
- $class : string
- $discrim : string|null = 'id'
Return values
ExCollectionInterfacefrom()
Creates a collection from an array.
public
static from([array<string|int, mixed> $items = [] ][, string|null $discrim = 'id' ][, string|null $class = null ]) : ExCollectionInterface
Parameters
- $items : array<string|int, mixed> = []
- $discrim : string|null = 'id'
- $class : string|null = null
Return values
ExCollectionInterfaceget()
Gets an item from the collection.
public
get(string $discrim, mixed $key) : mixed
Parameters
- $discrim : string
- $key : mixed
getIterator()
Returns an iterator for the collection.
public
getIterator() : Traversable
Return values
Traversablehas()
Checks if the array has multiple offsets.
public
has(string|int ...$keys) : bool
Parameters
- $keys : string|int
Return values
boolintersect()
Gets the intersection of the items.
public
intersect(mixed $items[, callable|null $callback = null ]) : ExCollectionInterface
If a callback is provided and is callable, it uses array_uintersect_assoc
to compute the intersection.
Otherwise, it uses array_intersect
.
Parameters
- $items : mixed
- $callback : callable|null = null
Return values
ExCollectionInterfaceisset()
If the collection has an offset.
public
isset(mixed $offset) : bool
Parameters
- $offset : mixed
Return values
booljsonSerialize()
Serializes the object to a value that can be serialized natively by json_encode().
public
jsonSerialize() : array<string|int, mixed>
Return values
array<string|int, mixed>keys()
public
keys() : array<string|int, int>|array<string|int, string>
Tags
Return values
array<string|int, int>|array<string|int, string>last()
Returns the last element of the collection.
public
last() : mixed
map()
Runs a callback over the collection and creates a new Collection.
public
map(callable $callback) : ExCollectionInterface
Parameters
- $callback : callable
Return values
ExCollectionInterfacemerge()
Merges another collection into this collection.
public
merge(mixed $collection) : self
Parameters
- $collection : mixed
Return values
selfoffsetExists()
If the collection has an offset.
public
offsetExists(mixed $offset) : bool
Parameters
- $offset : mixed
Return values
booloffsetGet()
Gets an item from the collection.
public
offsetGet(mixed $offset) : mixed
Parameters
- $offset : mixed
Attributes
- #[ReturnTypeWillChange]
offsetSet()
Sets an item into the collection.
public
offsetSet(mixed $offset, mixed $value) : void
Parameters
- $offset : mixed
- $value : mixed
offsetUnset()
Unsets an index from the collection.
public
offsetUnset(mixed $offset) : void
Parameters
- $offset : mixed
pull()
Pulls an item from the collection.
public
pull(mixed $key[, mixed $default = null ]) : mixed
Parameters
- $key : mixed
- $default : mixed = null
push()
Pushes items to the collection.
public
push(mixed ...$items) : self
Parameters
- $items : mixed
Return values
selfpushItem()
Pushes a single item to the collection.
public
pushItem(mixed $item) : self
Parameters
- $item : mixed
Return values
selfreduce()
Reduces the collection to a single value using a callback function.
public
reduce(callable $callback[, mixed|null $initial = null ]) : ExCollectionInterface
Parameters
- $callback : callable
- $initial : mixed|null = null
Return values
ExCollectionInterfacesearch()
Searches for a given value within the collection and returns the corresponding key if successful.
public
search(mixed $needle[, bool $strict = false ]) : string|int|false
Parameters
- $needle : mixed
- $strict : bool = false
-
[optional]
Return values
string|int|falseserialize()
Returns the string representation of the collection.
public
serialize([int $flags = 0 ][, int|null $depth = 512 ]) : string
Parameters
- $flags : int = 0
- $depth : int|null = 512
Return values
stringset()
Sets a value in the collection.
public
set(mixed $offset, mixed $value) : mixed
Parameters
- $offset : mixed
- $value : mixed
shift()
Shifts an item from the collection.
public
shift() : mixed
slice()
Slices the collection.
public
slice(int $offset[, int|null $length = null ][, bool $preserve_keys = false ]) : ExCollectionInterface
Parameters
- $offset : int
- $length : int|null = null
- $preserve_keys : bool = false
Return values
ExCollectionInterfacesort()
Sort through each item with a callback.
public
sort(callable|int|null $callback) : ExCollectionInterface
Parameters
- $callback : callable|int|null
Return values
ExCollectionInterfacesplice()
Splices the collection, removing a portion of the items and replacing them with the given replacement.
public
splice(int $offset, int|null $length[, mixed $replacement = [] ]) : self
Parameters
- $offset : int
- $length : int|null
- $replacement : mixed = []
Return values
selftoArray()
Converts the collection to an array.
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>unique()
Returns unique items.
public
unique([int $flags = SORT_STRING ]) : ExCollectionInterface
Parameters
- $flags : int = SORT_STRING
Return values
ExCollectionInterfaceunserialize()
Unserializes the collection.
public
unserialize(string $serialized) : void
Parameters
- $serialized : string
values()
Get the values of the items.
public
values() : array<string|int, mixed>
Return values
array<string|int, mixed>walk()
Applies the given callback function to each item in the collection.
public
walk(callable $callback, mixed $arg) : ExCollectionInterface
Parameters
- $callback : callable
- $arg : mixed