com\setasign\SetaFramework\Collection

Collection Class Collection

File: /SetaFramework/src/Collection/Collection.php

Class hierarchy

Implements

Summary

Properties

$items

protected array Collection::$items

Static Methods

explode()

public static Collection::explode (
string $delimiter,
string $string,
?int $limit = null
): Collection
Parameters
$delimiter: string
 
$string: string
 
$limit: ?int
 

from()

public static Collection::from (
mixed $items = array ( )
): Collection

Static builder function

Parameters
$items: mixed
 

Methods

__construct()

public Collection::__construct (
mixed $items = array ( )
)
Parameters
$items: mixed
 

__toString()

public Collection::__toString (
void
): string

all()

public Collection::all (
void
): array

Return all items as array

asort()

public Collection::asort (
?int $sortFlags = null
): self

Sort the collection and maintain index association

Parameters
$sortFlags: ?int
 
See

chunk()

public Collection::chunk (
int $size,
bool $preserveKeys = false
): Collection

Split the collection into chunks

Parameters
$size: int
 
$preserveKeys: bool
 
Return Values

Returns a new instance.

count()

public Collection::count (
void
): int

Counts the collection

delete()

public Collection::delete (
int|string ...$keys
): self

Remove one (or more) item(s) from the collection by key

Parameters
$keys: int|string
 

each()

public Collection::each (
callable $callback
): self

Execute a callback over each item

Parameters
$callback: callable

If the callback returns false the remaining elements will be skipped.

every()

public Collection::every (
int $step,
int $offset = 0
): Collection

Create a new collection consisting of every n-th element

Parameters
$step: int
 
$offset: int
 
Return Values

Returns a new instance.

except()

public Collection::except (
int|string ...$keys
): Collection

Get all items except for those with the specified keys

Parameters
$keys: int|string
 
Return Values

Returns a new instance.

filter()

public Collection::filter (
?callable $callback = null,
int $flag = \ARRAY_FILTER_USE_BOTH
): Collection

Filters elements of an array using a callback function

The keys will be preserved.

Parameters
$callback: ?callable
 
$flag: int
 
Return Values

Returns a new instance.

See

first()

public Collection::first (
mixed $default = null
): ?mixed

Returns the first item of the collection

Parameters
$default: mixed
 
See

flatten()

public Collection::flatten (
float|int $depth = \INF
): Collection
Parameters
$depth: float|int
 

flip()

public Collection::flip (
void
): Collection

Exchanges all keys with their associated values in an array

Return Values

Returns a new instance.

See

get()

public Collection::get (
mixed $key,
?mixed $default = null
): ?mixed

Get an item from the collection by key

Parameters
$key: mixed
 
$default: ?mixed

Will be returned if the item doesn't exist

getIterator()

has()

public Collection::has (
mixed $key
): bool

Checks if the given key or index exists

Parameters
$key: mixed
 

implode()

public Collection::implode (
string $glue = ''
): string

Join array elements with a string

Parameters
$glue: string
 

in()

public Collection::in (
mixed $value,
bool $strict = true
): bool

Checks if a value exists in the collection

Parameters
$value: mixed
 
$strict: bool
 
See

jsonSerialize()

public Collection::jsonSerialize (
void
): array

keys()

public Collection::keys (
void
): array

Return all the keys

ksort()

public Collection::ksort (
?int $sortFlags = null
): self

Sort the collection by key

Parameters
$sortFlags: ?int
 
See

last()

public Collection::last (
mixed $default = null
): ?mixed

Returns the last item of the collection

Parameters
$default: mixed
 
See

map()

public Collection::map (
callable $callable,
array ...$arrays
): Collection

Applies the callback to the elements of the given arrays

Parameters
$callable: callable
 
$arrays: array
 
Return Values

Returns a new instance.

See

offsetExists()

public Collection::offsetExists (
mixed $offset
): bool
Parameters
$offset: mixed
 

offsetGet()

public Collection::offsetGet (
mixed $offset
): ?mixed
Parameters
$offset: mixed
 

offsetSet()

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

offsetUnset()

public Collection::offsetUnset (
mixed $offset
): void
Parameters
$offset: mixed
 

only()

public Collection::only (
int|string ...$keys
): Collection

Get all items with the specified keys

Parameters
$keys: int|string
 
Return Values

Returns a new instance.

pipe()

public Collection::pipe (
callable(
static
): ReturnType
$callable
): ?mixed

Pass the collection to the given callback and return the result

Templates
ReturnType
Parameters
$callable: callable(
static
): ReturnType
 

pipeCollection()

public Collection::pipeCollection (
callable $callable
): Collection

Pass the collection to the given callback and return the result

Like pipe but the result must be a collection too!

Parameters
$callable: callable
 

pop()

public Collection::pop (
void
): ?mixed

Pop the element off the end of the collection

prepend()

public Collection::prepend (
mixed $value,
mixed $key = null
): self

Push an item onto the beginning of the collection

Parameters
$value: mixed
 
$key: mixed
 

pull()

public Collection::pull (
null|int|string $key,
mixed $default = null
): ?mixed

Pulls an item out of the collection

Parameters
$key: null|int|string
 
$default: mixed

Will be returned if the item doesn't exist

See

push()

public Collection::push (
mixed ...$values
): self

Push an item onto the end of the collection

Parameters
$values: mixed
 

put()

public Collection::put (
null|int|string $key,
mixed $value
): self

Puts an item onto the end of the collection

Parameters
$key: null|int|string
 
$value: mixed

Will be returned if the item doesn't exist

random()

public Collection::random (
int $amount = 1,
bool $preserveKeys = false
): Collection

Pick one or more random entries out of the collection

Parameters
$amount: int

Must be >= 0 and <= count()

$preserveKeys: bool
 
Return Values

Returns a new instance.

reduce()

public Collection::reduce (
callable $callback,
mixed $initial = null
): ?mixed

Iteratively reduce the collection to a single value using a callback function

Parameters
$callback: callable

Callback mixed callback ( mixed $carry , mixed $item )

$initial: mixed
 
See

reverse()

public Collection::reverse (
void
): Collection

Return a new collection with elements in reverse order

Return Values

Returns a new instance.

See

search()

public Collection::search (
mixed $value,
bool $strict = true
): string|int|false

Searches the collection for a given value and returns the first corresponding key if successful

Parameters
$value: mixed
 
$strict: bool
 
Return Values

Returns the key for $value if it is found in the array, FALSE otherwise.

See

shift()

public Collection::shift (
void
): ?mixed

Shift an element off the beginning of the collection

shuffle()

public Collection::shuffle (
void
): self

Shuffle the collection

slice()

public Collection::slice (
int $offset,
?int $length = null,
bool $preserveKeys = false
): Collection

Extract a slice of the array

Parameters
$offset: int
 
$length: ?int
 
$preserveKeys: bool
 
Return Values

Returns a new instance.

See

sort()

public Collection::sort (
?int $sortFlags = null
): self

Sort the collection

Parameters
$sortFlags: ?int
 
See

toArray()

public Collection::toArray (
void
): array

Return all items as array

transform()

public Collection::transform (
callable $callable,
bool $withKeys = false
): self

Transform each item in the collection using a callback

Parameters
$callable: callable
 
$withKeys: bool
 
See

uasort()

public Collection::uasort (
callable $callable
): self

Sort the collection with a user-defined comparison function and maintain index association

Parameters
$callable: callable
 
See

union()

public Collection::union (
array ...$arrays
): Collection

Union this collection with one or multiple arrays

Parameters
$arrays: array
 
Return Values

Returns a new instance.

unique()

public Collection::unique (
int $sortFlags = \SORT_STRING
): Collection

Removes duplicate values from an array

Parameters
$sortFlags: int
 
Return Values

Returns a new instance.

usort()

public Collection::usort (
callable $callable
): self

Sort the collection by values using a user-defined comparison function

Parameters
$callable: callable
 
See

values()

public Collection::values (
void
): array

Return all the values