Documentation

ReadableCollection extends Countable, IteratorAggregate

Tags
psalm-template

TKey of array-key

template-covariant

T

template-extends

IteratorAggregate<TKey, T>

Table of Contents

Methods

contains()  : bool
Checks whether an element is contained in the collection.
containsKey()  : bool
Checks whether the collection contains an element with the specified key/index.
current()  : mixed
Gets the element of the collection at the current iterator position.
exists()  : bool
Tests for the existence of an element that satisfies the given predicate.
filter()  : ReadableCollection<string|int, mixed>
Returns all the elements of this collection that satisfy the predicate p.
first()  : mixed
Sets the internal iterator to the first element in the collection and returns this element.
forAll()  : bool
Tests whether the given predicate p holds for all elements of this collection.
get()  : mixed
Gets the element at the specified key/index.
getKeys()  : array<string|int, int>|array<string|int, string>
Gets all keys/indices of the collection.
getValues()  : array<string|int, mixed>
Gets all values of the collection.
indexOf()  : int|string|bool
Gets the index/key of a given element. The comparison of two elements is strict, that means not only the value but also the type must match.
isEmpty()  : bool
Checks whether the collection is empty (contains no elements).
key()  : int|string|null
Gets the key/index of the element at the current iterator position.
last()  : mixed
Sets the internal iterator to the last element in the collection and returns this element.
map()  : Collection<string|int, mixed>
Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.
next()  : mixed
Moves the internal iterator position to the next element and returns this element.
partition()  : array<string|int, ReadableCollection<string|int, mixed>>
Partitions this collection in two collections according to a predicate.
slice()  : array<string|int, mixed>
Extracts a slice of $length elements starting at position $offset from the Collection.
toArray()  : array<string|int, mixed>
Gets a native PHP array representation of the collection.

Methods

contains()

Checks whether an element is contained in the collection.

public contains(mixed $element) : bool

This is an O(n) operation, where n is the size of the collection.

Parameters
$element : mixed

The element to search for.

Tags
psalm-param

TMaybeContained $element

psalm-return

(TMaybeContained is T ? bool : false)

template

TMaybeContained

Return values
bool

TRUE if the collection contains the element, FALSE otherwise.

containsKey()

Checks whether the collection contains an element with the specified key/index.

public containsKey(string|int $key) : bool
Parameters
$key : string|int

The key/index to check for.

Tags
psalm-param

TKey $key

Return values
bool

TRUE if the collection contains an element with the specified key/index, FALSE otherwise.

current()

Gets the element of the collection at the current iterator position.

public current() : mixed
Tags
psalm-return

T|false

exists()

Tests for the existence of an element that satisfies the given predicate.

public exists(Closure $p) : bool
Parameters
$p : Closure

The predicate.

Tags
psalm-param

Closure(TKey, T):bool $p

Return values
bool

TRUE if the predicate is TRUE for at least one element, FALSE otherwise.

filter()

Returns all the elements of this collection that satisfy the predicate p.

public filter(Closure $p) : ReadableCollection<string|int, mixed>

The order of the elements is preserved.

Parameters
$p : Closure

The predicate used for filtering.

Tags
psalm-param

Closure(T):bool $p

psalm-return

ReadableCollection<TKey, T>

Return values
ReadableCollection<string|int, mixed>

A collection with the results of the filter operation.

first()

Sets the internal iterator to the first element in the collection and returns this element.

public first() : mixed
Tags
psalm-return

T|false

forAll()

Tests whether the given predicate p holds for all elements of this collection.

public forAll(Closure $p) : bool
Parameters
$p : Closure

The predicate.

Tags
psalm-param

Closure(TKey, T):bool $p

Return values
bool

TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.

get()

Gets the element at the specified key/index.

public get(string|int $key) : mixed
Parameters
$key : string|int

The key/index of the element to retrieve.

Tags
psalm-param

TKey $key

psalm-return

T|null

getKeys()

Gets all keys/indices of the collection.

public getKeys() : array<string|int, int>|array<string|int, string>
Tags
psalm-return

list<TKey>

Return values
array<string|int, int>|array<string|int, string>

The keys/indices of the collection, in the order of the corresponding elements in the collection.

getValues()

Gets all values of the collection.

public getValues() : array<string|int, mixed>
Tags
psalm-return

list<T>

Return values
array<string|int, mixed>

The values of all elements in the collection, in the order they appear in the collection.

indexOf()

Gets the index/key of a given element. The comparison of two elements is strict, that means not only the value but also the type must match.

public indexOf(mixed $element) : int|string|bool

For objects this means reference equality.

Parameters
$element : mixed

The element to search for.

Tags
psalm-param

TMaybeContained $element

psalm-return

(TMaybeContained is T ? TKey|false : false)

template

TMaybeContained

Return values
int|string|bool

The key/index of the element or FALSE if the element was not found.

isEmpty()

Checks whether the collection is empty (contains no elements).

public isEmpty() : bool
Return values
bool

TRUE if the collection is empty, FALSE otherwise.

key()

Gets the key/index of the element at the current iterator position.

public key() : int|string|null
Tags
psalm-return

TKey|null

Return values
int|string|null

last()

Sets the internal iterator to the last element in the collection and returns this element.

public last() : mixed
Tags
psalm-return

T|false

map()

Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.

public map(Closure $func) : Collection<string|int, mixed>
Parameters
$func : Closure
Tags
psalm-param

Closure(T):U $func

psalm-return

Collection<TKey, U>

psalm-template

U

Return values
Collection<string|int, mixed>

next()

Moves the internal iterator position to the next element and returns this element.

public next() : mixed
Tags
psalm-return

T|false

partition()

Partitions this collection in two collections according to a predicate.

public partition(Closure $p) : array<string|int, ReadableCollection<string|int, mixed>>

Keys are preserved in the resulting collections.

Parameters
$p : Closure

The predicate on which to partition.

Tags
psalm-param

Closure(TKey, T):bool $p

psalm-return

array{0: ReadableCollection<TKey, T>, 1: ReadableCollection<TKey, T>}

Return values
array<string|int, ReadableCollection<string|int, mixed>>

An array with two elements. The first element contains the collection of elements where the predicate returned TRUE, the second element contains the collection of elements where the predicate returned FALSE.

slice()

Extracts a slice of $length elements starting at position $offset from the Collection.

public slice(int $offset[, int|null $length = null ]) : array<string|int, mixed>

If $length is null it returns all elements from $offset to the end of the Collection. Keys have to be preserved by this method. Calling this method will only return the selected slice and NOT change the elements contained in the collection slice is called on.

Parameters
$offset : int

The offset to start from.

$length : int|null = null

The maximum number of elements to return, or null for no limit.

Tags
psalm-return

array<TKey,T>

Return values
array<string|int, mixed>

toArray()

Gets a native PHP array representation of the collection.

public toArray() : array<string|int, mixed>
Tags
psalm-return

array<TKey,T>

Return values
array<string|int, mixed>

        
On this page

Search results