DiscordPHP Documentation

Collection
in package
implements ArrayAccess, JsonSerializable, IteratorAggregate, Countable

Collection of items. Inspired by Laravel Collections.

Tags
since
5.0.0

No longer extends Laravel's BaseCollection

since
4.0.0

Interfaces, Classes, Traits and Enums

ArrayAccess
JsonSerializable
IteratorAggregate
Countable

Table of Contents

$class  : string
Class type allowed into the collection.
$discrim  : string
The collection discriminator.
$items  : array<string|int, mixed>
The items contained in the collection.
__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.
clear()  : void
Clears the collection.
count()  : int
Counts the amount of objects in the collection.
fill()  : Collection
Fills an array of items into the collection.
filter()  : Collection
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.
first()  : mixed
Returns the first element of the collection.
for()  : static
Creates a collection for a class.
from()  : static
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.
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().
last()  : mixed
Returns the last element of the collection.
map()  : Collection
Runs a callback over the collection and creates a new collection.
merge()  : Collection
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()  : Collection
Pushes items to the collection.
pushItem()  : Collection
Pushes a single item to the collection.
serialize()  : string
Returns the string representation of the collection.
set()  : mixed
Sets a value in the collection.
toArray()  : array<string|int, mixed>
Converts the collection to an array.
unserialize()  : void
Unserializes the collection.

Properties

$class

Class type allowed into the collection.

protected string $class

$discrim

The collection discriminator.

protected string $discrim

$items

The items contained in the collection.

protected array<string|int, mixed> $items

Methods

__construct()

Create a new collection.

public __construct([mixed $items = [] ][, string $discrim = 'id' ][, string|null $class = null ]) : mixed
Parameters
$items : mixed = []
$discrim : string = 'id'
$class : string|null = null
Return values
mixed

__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(array<string|int, mixed> $data) : void
Parameters
$data : array<string|int, mixed>
Return values
void

clear()

Clears the collection.

public clear() : void
Return values
void

count()

Counts the amount of objects in the collection.

public count() : int
Return values
int

fill()

Fills an array of items into the collection.

public fill(array<string|int, mixed> $items) : Collection
Parameters
$items : array<string|int, mixed>
Return values
Collection

filter()

Runs a filter callback over the collection and returns a new collection based on the response of the callback.

public filter(callable $callback) : Collection
Parameters
$callback : callable
Return values
Collection

find()

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.

first()

Returns the first element of the collection.

public first() : mixed
Return values
mixed

for()

Creates a collection for a class.

public static for(string $class[, string $discrim = 'id' ]) : static
Parameters
$class : string
$discrim : string = 'id'
Return values
static

from()

Creates a collection from an array.

public static from([array<string|int, mixed> $items = [] ][, string $discrim = 'id' ][, string $class = null ]) : static
Parameters
$items : array<string|int, mixed> = []
$discrim : string = 'id'
$class : string = null
Return values
static

get()

Gets an item from the collection.

public get(string $discrim, mixed $key) : mixed
Parameters
$discrim : string
$key : mixed
Return values
mixed

getIterator()

Returns an iterator for the collection.

public getIterator() : Traversable
Return values
Traversable

has()

Checks if the array has multiple offsets.

public has(array<string|int, mixed> ...$keys) : bool
Parameters
$keys : array<string|int, mixed>
Return values
bool

isset()

If the collection has an offset.

public isset(mixed $offset) : bool
Parameters
$offset : mixed
Return values
bool

jsonSerialize()

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>

last()

Returns the last element of the collection.

public last() : mixed
Return values
mixed

map()

Runs a callback over the collection and creates a new collection.

public map(callable $callback) : Collection
Parameters
$callback : callable
Return values
Collection

offsetExists()

If the collection has an offset.

public offsetExists(mixed $offset) : bool
Parameters
$offset : mixed
Return values
bool

offsetGet()

Gets an item from the collection.

public offsetGet(mixed $offset) : mixed
Parameters
$offset : mixed
Return values
mixed

offsetSet()

Sets an item into the collection.

public offsetSet(mixed $offset, mixed $value) : void
Parameters
$offset : mixed
$value : mixed
Return values
void

offsetUnset()

Unsets an index from the collection.

public offsetUnset(mixed $offset) : void
Parameters
$offset : mixed
Return values
void

pull()

Pulls an item from the collection.

public pull(mixed $key[, mixed $default = null ]) : mixed
Parameters
$key : mixed
$default : mixed = null
Return values
mixed

pushItem()

Pushes a single item to the collection.

public pushItem(mixed $item) : Collection
Parameters
$item : mixed
Return values
Collection

serialize()

Returns the string representation of the collection.

public serialize() : string
Return values
string

set()

Sets a value in the collection.

public set(mixed $offset, mixed $value) : mixed
Parameters
$offset : mixed
$value : mixed
Return values
mixed

toArray()

Converts the collection to an array.

public toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

unserialize()

Unserializes the collection.

public unserialize(string $serialized) : void
Parameters
$serialized : string
Return values
void

Search results