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.

void
next()

Advances the internal pointer.

void
rewind()

Resets the internal pointer.

bool
valid()

Checks if current position is valid.

$this
delete(mixed $key)

Removes an item from the collection by its key.

$this
clear()

Clears the Collection.

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.

mixed[]
all()

Returns all items.

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.

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.

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

No description

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.

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

Sets a key-value pair.

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.

filter(callable $closure)

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

values()

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

Details

at line 26
__construct(array $data = null)

I think you are supposed to know what this does.

Parameters

array $data

at line 37
mixed __debugInfo()

Return Value

mixed

at line 47
mixed current()

Returns the current element.

Return Value

mixed

at line 57
mixed key()

Fetch the key from the current element.

Return Value

mixed

at line 67
void next()

Advances the internal pointer.

Return Value

void

at line 77
void rewind()

Resets the internal pointer.

Return Value

void

at line 87
bool valid()

Checks if current position is valid.

Return Value

bool

at line 97
$this delete(mixed $key)

Removes an item from the collection by its key.

Parameters

mixed $key

Return Value

$this

at line 108
$this clear()

Clears the Collection.

Return Value

$this

at line 120
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 129
int count()

Returns the total number of items in the collection.

Return Value

int

at line 138
Collection copy()

Returns a copy of itself.

Return Value

Collection

at line 148
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 161
mixed[] all()

Returns all items.

Return Value

mixed[]

at line 171
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 185
$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 202
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 218
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 235
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 261
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 274
protected array flattenDo(array $array, int $depth, int $inDepth = 0)

Parameters

array $array
int $depth
int $inDepth

Return Value

array

at line 295
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 305
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 332
bool has(mixed $key)

Determines if a given key exists in the collection.

Parameters

mixed $key

Return Value

bool

at line 345
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 375
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 395
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 408
Collection keys()

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

Return Value

Collection

at line 418
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 445
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 458
int max(mixed|null $key = null)

Return the maximum value of a given key.

Parameters

mixed|null $key

Return Value

int

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

Return the minimum value of a given key.

Parameters

mixed|null $key

Return Value

int

at line 491
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 503
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 524
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 541
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 563
$this set(mixed $key, mixed $value)

Sets a key-value pair.

Parameters

mixed $key
mixed $value

Return Value

$this

at line 575
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 607
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 628
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 642
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 662
Collection shuffle()

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

Return Value

Collection

at line 677
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 688
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 705
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 724
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 742
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 755
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 770
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 808
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 825
Collection values()

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

Return Value

Collection