phpDocumentor

Collection
in package
implements Countable, IteratorAggregate, ArrayAccess

Represents an easily accessible collection of elements.

The goal for this class is to allow Descriptors to be easily retrieved and set so that interaction in templates becomes easier.

Interfaces, Classes and Traits

Countable
IteratorAggregate
ArrayAccess

Table of Contents

$items  : array<string|int, T>
__construct()  : mixed
Constructs a new collection object with optionally a series of items, generally Descriptors.
__get()  : mixed
Retrieves an item as if it were a property of the collection.
add()  : void
Adds a new item to this collection, generally a Descriptor.
clear()  : void
Empties the collection.
count()  : int
Returns a count of the number of elements in this collection.
fetch()  : TChild
Retrieves a specific item from the Collection with its index.
filter()  : Collection<string|int, F>
fromClassString()  : Collection<string|int, C>
get()  : T
Retrieves a specific item from the Collection with its index. If index is not found, an exception is thrown
getAll()  : array<string|int, T>
Retrieves all items from this collection as PHP Array.
getIterator()  : ArrayIterator<string|int, T>
Retrieves an iterator to traverse this object.
merge()  : Collection<string|int, T>
Returns a new collection with the items from this collection and the provided combined.
offsetExists()  : bool
Checks whether an item in this collection exists.
offsetGet()  : T|null
Retrieves an item from the collection with the given index.
offsetSet()  : void
Sets an item at the given index.
offsetUnset()  : void
Removes an item with the given index from the collection.
set()  : void
Sets a new object onto the collection or clear it using null.

Properties

$items

protected array<string|int, T> $items = []

Methods

__construct()

Constructs a new collection object with optionally a series of items, generally Descriptors.

public __construct([array<string|int, T$items = [] ]) : mixed
Parameters
$items : array<string|int, T> = []
Return values
mixed

__get()

Retrieves an item as if it were a property of the collection.

public __get(string $name) : mixed
Parameters
$name : string
Tags
phpstan-return

?T

Return values
mixed

add()

Adds a new item to this collection, generally a Descriptor.

public add(T $item) : void
Parameters
$item : T
Return values
void

clear()

Empties the collection.

public clear() : void
Return values
void

count()

Returns a count of the number of elements in this collection.

public count() : int
Return values
int

fetch()

Retrieves a specific item from the Collection with its index.

public fetch(string|int $index[, TChild|null $valueIfEmpty = null ]) : TChild

Please note that this method (intentionally) has the side effect that whenever a key does not exist that it will be created with the value provided by the $valueIfEmpty argument. This will allow for easy initialization during tree building operations.

Parameters
$index : string|int
$valueIfEmpty : TChild|null = null

If the index does not exist it will be created with this value and returned.

Tags
psalm-return

($valueIfEmpty is null ? ?TChild: TChild)

phpstan-return

T|TChild

Return values
TChild

The contents of the element with the given index and the provided default if the key doesn't exist.

filter()

public filter(class-string<\phpDocumentor\Descriptor\F> $className) : Collection<string|int, F>
Parameters
$className : class-string<\phpDocumentor\Descriptor\F>
Return values
Collection<string|int, F>

fromClassString()

public static fromClassString(class-string<\phpDocumentor\Descriptor\C> $classString[, array<string|int, C$elements = [] ]) : Collection<string|int, C>
Parameters
$classString : class-string<\phpDocumentor\Descriptor\C>
$elements : array<string|int, C> = []
Return values
Collection<string|int, C>

get()

Retrieves a specific item from the Collection with its index. If index is not found, an exception is thrown

public get(string|int $index) : T
Parameters
$index : string|int
Return values
T

The contents of the element with the given index

getAll()

Retrieves all items from this collection as PHP Array.

public getAll() : array<string|int, T>
Return values
array<string|int, T>

getIterator()

Retrieves an iterator to traverse this object.

public getIterator() : ArrayIterator<string|int, T>
Return values
ArrayIterator<string|int, T>

merge()

Returns a new collection with the items from this collection and the provided combined.

public merge(Collection<string|int, T$collection) : Collection<string|int, T>
Parameters
$collection : Collection<string|int, T>
Return values
Collection<string|int, T>

offsetExists()

Checks whether an item in this collection exists.

public offsetExists(string|int $offset) : bool
Parameters
$offset : string|int

The index to check on.

Return values
bool

offsetGet()

Retrieves an item from the collection with the given index.

public offsetGet(string|int $offset) : T|null
Parameters
$offset : string|int

The offset to retrieve.

Return values
T|null

offsetSet()

Sets an item at the given index.

public offsetSet(string|int|null $offset, T $value) : void
Parameters
$offset : string|int|null

The offset to assign the value to.

$value : T

The value to set.

Tags
throws
InvalidArgumentException

If the key is null or an empty string.

Return values
void

offsetUnset()

Removes an item with the given index from the collection.

public offsetUnset(string|int $offset) : void
Parameters
$offset : string|int

The offset to unset.

Return values
void

set()

Sets a new object onto the collection or clear it using null.

public set(string|int $index, T $item) : void
Parameters
$index : string|int

An index value to recognize this item with.

$item : T

The item to store, generally a Descriptor but may be something else.

Return values
void

Search results