class Collection implements Countable, Iterator

Collection, an util to conventionally store a key-value pair.

Properties

protected array $data

Methods

__construct(array $data = null)

I think you are supposed to know what this does.

mixed
__debugInfo()

No description

mixed
current()

Returns the current element.

mixed
key()

Fetch the key from the current element.

mixed|false
next()

Advances the internal pointer.

mixed|false
rewind()

Resets the internal pointer.

bool
valid()

Checks if current position is valid.

$this
set(mixed $key, mixed $value)

Sets a key-value pair.

$this
delete(mixed $key)

Removes an item from the collection by its key.

$this
clear()

Clears the Collection.

mixed[]
all()

Returns all items.

chunk(int $numitems, bool $preserve_keys = false)

Breaks the collection into multiple, smaller chunks of a given size. Returns a new Collection.

int
count()

Returns the total number of items in the collection.

copy()

Returns a copy of itself.

diff(mixed[]|Collection $arr)

Compares the collection against another collection or a plain PHP array based on its value. Returns a new Collection.

diffKeys(mixed[]|Collection $arr)

Compares the collection against another collection or a plain PHP array based on its key. Returns a new Collection.

$this
each(callable $closure)

Iterates over the items in the collection and passes each item to a given callback. Returning false in the callback will stop the processing.

bool
every(callable $closure)

Returns true if all elements pass the given truth test.

except(array $keys)

Returns all items in the collection except for those with the specified keys. Returns a new Collection.

filter(callable $closure)

Filters the collection by a given callback, keeping only those items that pass a given truth test. Returns a new Collection.

mixed|null
first(callable $closure = null)

Returns the first element in the collection that passes a given truth test.

flatten(int $depth = 0)

Flattens a multi-dimensional collection into a single dimension. Returns a new Collection.

mixed|null
get(mixed $key)

Returns the item for a given key. If the key does not exist, null is returned.

groupBy(callable|mixed $column)

Groups the collection's items by a given key. Returns a new Collection.

bool
has(mixed $key)

Determines if a given key exists in the collection.

string
implode(mixed $col, string $glue = ', ')

Joins the items in a collection. Its arguments depend on the type of items in the collection.

int|null
indexOf(mixed $value)

Returns the position of the given value in the collection. Returns null if the given value couldn't be found.

intersect(mixed[]|Collection $arr)

Removes any values that are not present in the given array or collection. Returns a new Collection.

keys()

Returns all of the collection's keys. Returns a new Collection.

mixed|null
last(callable $closure = null)

Returns the last element in the collection that passes a given truth test.

map(callable $closure)

Iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items.

int
max(mixed|null $key = null)

Return the maximum value of a given key.

int
min(mixed|null $key = null)

Return the minimum value of a given key.

merge(Collection $collection)

Merges the given collection into this collection, resulting in a new collection.

nth(int $nth, int $offset = 0)

Creates a new collection consisting of every n-th element.

only(array $keys)

Returns the items in the collection with the specified keys. Returns a new Collection.

partition(callable $closure)

Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.

pluck(mixed $key, mixed $index = null)

Return the values from a single column in the input array. Returns a new Collection.

random(int $num = 1)

Returns one random item, or multiple random items inside a Collection, from the Collection. Returns a new Collection.

mixed|null|void
reduce(callable $closure, mixed|null $carry = null)

Reduces the collection to a single value, passing the result of each iteration into the subsequent iteration.

reverse(bool $preserve_keys = false)

Reverses the order of the collection's items. Returns a new Collection.

mixed|bool
search(mixed $needle, bool $strict = true)

Searches the collection for the given value and returns its key if found. If the item is not found, false is returned.

shuffle()

Randomly shuffles the items in the collection. Returns a new Collection.

slice(int $offset, int $limit = null, bool $preserve_keys = false)

Returns a slice of the collection starting at the given index. Returns a new Collection.

bool
some(callable $closure)

Returns true if at least one element passes the given truth test.

sort(bool $descending = false, int $options = \SORT_REGULAR)

Sorts the collection, using sort behaviour flags. Returns a new Collection.

sortKey(bool $descending = false, int $options = \SORT_REGULAR)

Sorts the collection by key, using sort behaviour flags. Returns a new Collection.

sortCustom(callable $closure)

Sorts the collection using a custom sorting function. Returns a new Collection.

sortCustomKey(callable $closure)

Sorts the collection by key using a custom sorting function. Returns a new Collection.

unique(mixed|null $key, int $options = \SORT_REGULAR)

Returns all of the unique items in the collection. Returns a new Collection.

values()

Returns a new collection with the keys reset to consecutive integers.

array
flattenDo(array $array, int $depth, int $inDepth = 0)

No description

Details

at line 25
__construct(array $data = null)

I think you are supposed to know what this does.

Parameters

array $data

at line 35
mixed __debugInfo()

Return Value

mixed

at line 44
mixed current()

Returns the current element.

Return Value

mixed

at line 53
mixed key()

Fetch the key from the current element.

Return Value

mixed

at line 62
mixed|false next()

Advances the internal pointer.

Return Value

mixed|false

at line 71
mixed|false rewind()

Resets the internal pointer.

Return Value

mixed|false

at line 80
bool valid()

Checks if current position is valid.

Return Value

bool

at line 90
$this set(mixed $key, mixed $value)

Sets a key-value pair.

Parameters

mixed $key
mixed $value

Return Value

$this

at line 100
$this delete(mixed $key)

Removes an item from the collection by its key.

Parameters

mixed $key

Return Value

$this

at line 110
$this clear()

Clears the Collection.

Return Value

$this

at line 119
mixed[] all()

Returns all items.

Return Value

mixed[]

at line 129
Collection chunk(int $numitems, bool $preserve_keys = false)

Breaks the collection into multiple, smaller chunks of a given size. Returns a new Collection.

Parameters

int $numitems
bool $preserve_keys

Return Value

Collection

at line 137
int count()

Returns the total number of items in the collection.

Return Value

int

at line 145
Collection copy()

Returns a copy of itself.

Return Value

Collection

at line 154
Collection diff(mixed[]|Collection $arr)

Compares the collection against another collection or a plain PHP array based on its value. Returns a new Collection.

Parameters

mixed[]|Collection $arr

Return Value

Collection

at line 167
Collection diffKeys(mixed[]|Collection $arr)

Compares the collection against another collection or a plain PHP array based on its key. Returns a new Collection.

Parameters

mixed[]|Collection $arr

Return Value

Collection

at line 180
$this each(callable $closure)

Iterates over the items in the collection and passes each item to a given callback. Returning false in the callback will stop the processing.

Parameters

callable $closure Callback specification: function ($value, $key): bool

Return Value

$this

at line 196
bool every(callable $closure)

Returns true if all elements pass the given truth test.

Parameters

callable $closure Callback specification: function ($value, $key): bool

Return Value

bool

at line 211
Collection except(array $keys)

Returns all items in the collection except for those with the specified keys. Returns a new Collection.

Parameters

array $keys

Return Value

Collection

at line 227
Collection filter(callable $closure)

Filters the collection by a given callback, keeping only those items that pass a given truth test. Returns a new Collection.

Parameters

callable $closure Callback specification: function ($value, $key): bool

Return Value

Collection

at line 244
mixed|null first(callable $closure = null)

Returns the first element in the collection that passes a given truth test.

Parameters

callable $closure Callback specification: function ($value, $key): bool

Return Value

mixed|null

at line 269
Collection flatten(int $depth = 0)

Flattens a multi-dimensional collection into a single dimension. Returns a new Collection.

Parameters

int $depth

Return Value

Collection

at line 279
mixed|null get(mixed $key)

Returns the item for a given key. If the key does not exist, null is returned.

Parameters

mixed $key

Return Value

mixed|null

at line 288
Collection groupBy(callable|mixed $column)

Groups the collection's items by a given key. Returns a new Collection.

Parameters

callable|mixed $column Callback specification: function ($value, $key): mixed

Return Value

Collection

at line 314
bool has(mixed $key)

Determines if a given key exists in the collection.

Parameters

mixed $key

Return Value

bool

at line 326
string implode(mixed $col, string $glue = ', ')

Joins the items in a collection. Its arguments depend on the type of items in the collection.

If the collection contains arrays or objects, you should pass the key of the attributes you wish to join, and the "glue" string you wish to place between the values.

Parameters

mixed $col
string $glue

Return Value

string

Exceptions

BadMethodCallException

at line 355
int|null indexOf(mixed $value)

Returns the position of the given value in the collection. Returns null if the given value couldn't be found.

Parameters

mixed $value

Return Value

int|null

at line 374
Collection intersect(mixed[]|Collection $arr)

Removes any values that are not present in the given array or collection. Returns a new Collection.

Parameters

mixed[]|Collection $arr

Return Value

Collection

at line 386
Collection keys()

Returns all of the collection's keys. Returns a new Collection.

Return Value

Collection

at line 395
mixed|null last(callable $closure = null)

Returns the last element in the collection that passes a given truth test.

Parameters

callable $closure Callback specification: function ($value, $key): bool

Return Value

mixed|null

at line 421
Collection map(callable $closure)

Iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items.

Parameters

callable $closure Callback specification: function ($value, $key): mixed

Return Value

Collection

at line 433
int max(mixed|null $key = null)

Return the maximum value of a given key.

Parameters

mixed|null $key

Return Value

int

at line 448
int min(mixed|null $key = null)

Return the minimum value of a given key.

Parameters

mixed|null $key

Return Value

int

at line 464
Collection merge(Collection $collection)

Merges the given collection into this collection, resulting in a new collection.

Any string key in the given collection matching a string key in this collection will overwrite the value in this collection.

Parameters

Collection $collection

Return Value

Collection

at line 475
Collection nth(int $nth, int $offset = 0)

Creates a new collection consisting of every n-th element.

Parameters

int $nth
int $offset

Return Value

Collection

Exceptions

InvalidArgumentException

at line 495
Collection only(array $keys)

Returns the items in the collection with the specified keys. Returns a new Collection.

Parameters

array $keys

Return Value

Collection

at line 511
Collection[] partition(callable $closure)

Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.

Parameters

callable $closure Callback specification: function ($value, $key): bool

Return Value

Collection[]

at line 532
Collection pluck(mixed $key, mixed $index = null)

Return the values from a single column in the input array. Returns a new Collection.

Parameters

mixed $key
mixed $index

Return Value

Collection

at line 563
Collection random(int $num = 1)

Returns one random item, or multiple random items inside a Collection, from the Collection. Returns a new Collection.

Parameters

int $num

Return Value

Collection

at line 583
mixed|null|void reduce(callable $closure, mixed|null $carry = null)

Reduces the collection to a single value, passing the result of each iteration into the subsequent iteration.

Parameters

callable $closure Callback specification: function ($carry, $value): mixed
mixed|null $carry

Return Value

mixed|null|void

at line 596
Collection reverse(bool $preserve_keys = false)

Reverses the order of the collection's items. Returns a new Collection.

Parameters

bool $preserve_keys

Return Value

Collection

Searches the collection for the given value and returns its key if found. If the item is not found, false is returned.

Parameters

mixed $needle
bool $strict

Return Value

mixed|bool

at line 614
Collection shuffle()

Randomly shuffles the items in the collection. Returns a new Collection.

Return Value

Collection

at line 628
Collection slice(int $offset, int $limit = null, bool $preserve_keys = false)

Returns a slice of the collection starting at the given index. Returns a new Collection.

Parameters

int $offset
int $limit
bool $preserve_keys

Return Value

Collection

at line 638
bool some(callable $closure)

Returns true if at least one element passes the given truth test.

Parameters

callable $closure Callback specification: function ($value, $key): bool

Return Value

bool

at line 654
Collection sort(bool $descending = false, int $options = \SORT_REGULAR)

Sorts the collection, using sort behaviour flags. Returns a new Collection.

Parameters

bool $descending
int $options

Return Value

Collection

at line 672
Collection sortKey(bool $descending = false, int $options = \SORT_REGULAR)

Sorts the collection by key, using sort behaviour flags. Returns a new Collection.

Parameters

bool $descending
int $options

Return Value

Collection

at line 689
Collection sortCustom(callable $closure)

Sorts the collection using a custom sorting function. Returns a new Collection.

Parameters

callable $closure Callback specification: function ($a, $b): int

Return Value

Collection

at line 701
Collection sortCustomKey(callable $closure)

Sorts the collection by key using a custom sorting function. Returns a new Collection.

Parameters

callable $closure Callback specification: function ($a, $b): int

Return Value

Collection

at line 715
Collection unique(mixed|null $key, int $options = \SORT_REGULAR)

Returns all of the unique items in the collection. Returns a new Collection.

Parameters

mixed|null $key
int $options

Return Value

Collection

Exceptions

BadMethodCallException

at line 751
Collection values()

Returns a new collection with the keys reset to consecutive integers.

Return Value

Collection

at line 762
protected array flattenDo(array $array, int $depth, int $inDepth = 0)

Parameters

array $array
int $depth
int $inDepth

Return Value

array