PHP Class FOF30\Utils\Collection

Inheritance: implements ArrayAcces\ArrayAccess, implements Countabl\Countable, implements IteratorAggregat\IteratorAggregate, implements JsonSerializabl\JsonSerializable
Mostra file Open project: akeeba/fof Class Usage Examples

Protected Properties

Property Type Description
$items array The items contained in the collection.

Public Methods

Method Description
__construct ( array $items = [] ) Create a new collection.
__toString ( ) : string Convert the collection to its string representation.
all ( ) : array Get all of the items in the collection.
collapse ( ) : Collection Collapse the collection items into a single array.
count ( ) : integer Count the number of items in the collection.
diff ( Collection | array $items ) : Collection Diff the collection with the given items.
each ( Closure $callback ) : Collection Execute a callback over each item.
fetch ( string $key ) : Collection Fetch a nested element of the collection.
filter ( Closure $callback ) : Collection Run a filter over each of the items.
first ( Closure $callback = null, mixed $default = null ) : mixed | null Get the first item from the collection.
flatten ( ) : array Get a flattened array of the items in the collection.
forget ( mixed $key ) : void Remove an item from the collection by key.
get ( mixed $key, mixed $default = null ) : mixed Get an item from the collection by key.
getCachingIterator ( integer $flags = CachingIterator::CALL_TOSTRING ) : CachingIterator Get a CachingIterator instance.
getIterator ( ) : ArrayIterator Get an iterator for the items.
groupBy ( callable | string $groupBy ) : Collection Group an associative array by a field or Closure value.
has ( mixed $key ) : boolean Determine if an item exists in the collection by key.
implode ( string $value, string $glue = null ) : string Concatenate values of a given key as a string.
intersect ( Collection | array $items ) : Collection Intersect the collection with the given items.
isEmpty ( ) : boolean Determine if the collection is empty or not.
jsonSerialize ( ) : array Convert the object into something JSON serializable.
last ( ) : mixed | null Get the last item from the collection.
lists ( string $value, string $key = null ) : array Get an array with the values of a given key.
make ( mixed $items ) : static Create a new collection instance if the value isn't one already.
map ( Closure $callback ) : Collection Run a map over each of the items.
merge ( Collection | array $items ) : Collection Merge the collection with the given items.
offsetExists ( mixed $key ) : boolean Determine if an item exists at an offset.
offsetGet ( mixed $key ) : mixed Get an item at a given offset.
offsetSet ( mixed $key, mixed $value ) : void Set the item at a given offset.
offsetUnset ( string $key ) : void Unset the item at a given offset.
pop ( ) : mixed | null Get and remove the last item from the collection.
prepend ( mixed $value ) : void Push an item onto the beginning of the collection.
push ( mixed $value ) : void Push an item onto the end of the collection.
put ( mixed $key, mixed $value ) : void Put an item in the collection by key.
random ( integer $amount = 1 ) : mixed Get one or more items randomly from the collection.
reduce ( callable $callback, mixed $initial = null ) : mixed Reduce the collection to a single value.
reset ( ) : Collection Resets the Collection (removes all items)
reverse ( ) : Collection Reverse items order.
shift ( ) : mixed | null Get and remove the first item from the collection.
slice ( integer $offset, integer $length = null, boolean $preserveKeys = false ) : Collection Slice the underlying collection array.
sort ( Closure $callback ) : Collection Sort through each item with a callback.
sortBy ( Closure | string $callback, integer $options = SORT_REGULAR, boolean $descending = false ) : Collection Sort the collection using the given Closure.
sortByDesc ( Closure | string $callback, integer $options = SORT_REGULAR ) : Collection Sort the collection in descending order using the given Closure.
splice ( integer $offset, integer $length, mixed $replacement = [] ) : Collection Splice portion of the underlying collection array.
sum ( Closure | string $callback ) : mixed Get the sum of the given values.
take ( integer $limit = null ) : Collection Take the first or last {$limit} items.
toArray ( ) : array Get the collection of items as a plain array.
toJson ( integer $options ) : string Get the collection of items as JSON.
transform ( callable $callback ) : Collection Transform each item in the collection using a callback.
unique ( ) : Collection Return only unique items from the collection array.
values ( ) : Collection Reset the keys on the underlying array.

Protected Methods

Method Description
valueRetriever ( string $value ) : Closure Get a value retrieving callback.

Private Methods

Method Description
getArrayableItems ( Collection | array $items ) : array Results array of items from Collection.

Method Details

__construct() public method

Create a new collection.
public __construct ( array $items = [] )
$items array

__toString() public method

Convert the collection to its string representation.
public __toString ( ) : string
return string

all() public method

Get all of the items in the collection.
public all ( ) : array
return array

collapse() public method

Collapse the collection items into a single array.
public collapse ( ) : Collection
return Collection

count() public method

Count the number of items in the collection.
public count ( ) : integer
return integer

diff() public method

Diff the collection with the given items.
public diff ( Collection | array $items ) : Collection
$items Collection | array
return Collection

each() public method

Execute a callback over each item.
public each ( Closure $callback ) : Collection
$callback Closure
return Collection

fetch() public method

Fetch a nested element of the collection.
public fetch ( string $key ) : Collection
$key string
return Collection

filter() public method

Run a filter over each of the items.
public filter ( Closure $callback ) : Collection
$callback Closure
return Collection

first() public method

Get the first item from the collection.
public first ( Closure $callback = null, mixed $default = null ) : mixed | null
$callback Closure
$default mixed
return mixed | null

flatten() public method

Get a flattened array of the items in the collection.
public flatten ( ) : array
return array

forget() public method

Remove an item from the collection by key.
public forget ( mixed $key ) : void
$key mixed
return void

get() public method

Get an item from the collection by key.
public get ( mixed $key, mixed $default = null ) : mixed
$key mixed
$default mixed
return mixed

getCachingIterator() public method

Get a CachingIterator instance.
public getCachingIterator ( integer $flags = CachingIterator::CALL_TOSTRING ) : CachingIterator
$flags integer Caching iterator flags
return CachingIterator

getIterator() public method

Get an iterator for the items.

groupBy() public method

Group an associative array by a field or Closure value.
public groupBy ( callable | string $groupBy ) : Collection
$groupBy callable | string
return Collection

has() public method

Determine if an item exists in the collection by key.
public has ( mixed $key ) : boolean
$key mixed
return boolean

implode() public method

Concatenate values of a given key as a string.
public implode ( string $value, string $glue = null ) : string
$value string
$glue string
return string

intersect() public method

Intersect the collection with the given items.
public intersect ( Collection | array $items ) : Collection
$items Collection | array
return Collection

isEmpty() public method

Determine if the collection is empty or not.
public isEmpty ( ) : boolean
return boolean

jsonSerialize() public method

Convert the object into something JSON serializable.
public jsonSerialize ( ) : array
return array

last() public method

Get the last item from the collection.
public last ( ) : mixed | null
return mixed | null

lists() public method

Get an array with the values of a given key.
public lists ( string $value, string $key = null ) : array
$value string
$key string
return array

make() public static method

Create a new collection instance if the value isn't one already.
public static make ( mixed $items ) : static
$items mixed
return static

map() public method

Run a map over each of the items.
public map ( Closure $callback ) : Collection
$callback Closure
return Collection

merge() public method

Merge the collection with the given items.
public merge ( Collection | array $items ) : Collection
$items Collection | array
return Collection

offsetExists() public method

Determine if an item exists at an offset.
public offsetExists ( mixed $key ) : boolean
$key mixed
return boolean

offsetGet() public method

Get an item at a given offset.
public offsetGet ( mixed $key ) : mixed
$key mixed
return mixed

offsetSet() public method

Set the item at a given offset.
public offsetSet ( mixed $key, mixed $value ) : void
$key mixed
$value mixed
return void

offsetUnset() public method

Unset the item at a given offset.
public offsetUnset ( string $key ) : void
$key string
return void

pop() public method

Get and remove the last item from the collection.
public pop ( ) : mixed | null
return mixed | null

prepend() public method

Push an item onto the beginning of the collection.
public prepend ( mixed $value ) : void
$value mixed
return void

push() public method

Push an item onto the end of the collection.
public push ( mixed $value ) : void
$value mixed
return void

put() public method

Put an item in the collection by key.
public put ( mixed $key, mixed $value ) : void
$key mixed
$value mixed
return void

random() public method

Get one or more items randomly from the collection.
public random ( integer $amount = 1 ) : mixed
$amount integer
return mixed

reduce() public method

Reduce the collection to a single value.
public reduce ( callable $callback, mixed $initial = null ) : mixed
$callback callable
$initial mixed
return mixed

reset() public method

Resets the Collection (removes all items)
public reset ( ) : Collection
return Collection

reverse() public method

Reverse items order.
public reverse ( ) : Collection
return Collection

shift() public method

Get and remove the first item from the collection.
public shift ( ) : mixed | null
return mixed | null

slice() public method

Slice the underlying collection array.
public slice ( integer $offset, integer $length = null, boolean $preserveKeys = false ) : Collection
$offset integer
$length integer
$preserveKeys boolean
return Collection

sort() public method

Sort through each item with a callback.
public sort ( Closure $callback ) : Collection
$callback Closure
return Collection

sortBy() public method

Sort the collection using the given Closure.
public sortBy ( Closure | string $callback, integer $options = SORT_REGULAR, boolean $descending = false ) : Collection
$callback Closure | string
$options integer
$descending boolean
return Collection

sortByDesc() public method

Sort the collection in descending order using the given Closure.
public sortByDesc ( Closure | string $callback, integer $options = SORT_REGULAR ) : Collection
$callback Closure | string
$options integer
return Collection

splice() public method

Splice portion of the underlying collection array.
public splice ( integer $offset, integer $length, mixed $replacement = [] ) : Collection
$offset integer
$length integer
$replacement mixed
return Collection

sum() public method

Get the sum of the given values.
public sum ( Closure | string $callback ) : mixed
$callback Closure | string
return mixed

take() public method

Take the first or last {$limit} items.
public take ( integer $limit = null ) : Collection
$limit integer
return Collection

toArray() public method

Get the collection of items as a plain array.
public toArray ( ) : array
return array

toJson() public method

Get the collection of items as JSON.
public toJson ( integer $options ) : string
$options integer
return string

transform() public method

Transform each item in the collection using a callback.
public transform ( callable $callback ) : Collection
$callback callable
return Collection

unique() public method

Return only unique items from the collection array.
public unique ( ) : Collection
return Collection

valueRetriever() protected method

Get a value retrieving callback.
protected valueRetriever ( string $value ) : Closure
$value string
return Closure

values() public method

Reset the keys on the underlying array.
public values ( ) : Collection
return Collection

Property Details

$items protected_oe property

The items contained in the collection.
protected array $items
return array