Fr3nch13/CakePHP Utilities

SortIterator extends Collection
in package

An iterator that will return the passed items in order. The order is given by the value returned in a callback function that maps each of the elements.

Example:

$items = [$user1, $user2, $user3];
$sorted = new SortIterator($items, function ($user) {
 return $user->age;
});

// output all user name order by their age in descending order
foreach ($sorted as $user) {
 echo $user->name;
}

This iterator does not preserve the keys passed in the original elements.

Table of Contents

Methods

__construct()  : mixed
Wraps this iterator around the passed items so when iterated they are returned in order.
__debugInfo()  : array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
__serialize()  : array<string|int, mixed>
Returns an array for serializing this of this object.
__unserialize()  : void
Rebuilds the Collection instance.
append()  : CollectionInterface
appendItem()  : CollectionInterface
avg()  : mixed
buffered()  : CollectionInterface
cartesianProduct()  : CollectionInterface
{@inheritDoc}
chunk()  : CollectionInterface
chunkWithKeys()  : CollectionInterface
combine()  : CollectionInterface
compile()  : CollectionInterface
contains()  : bool
count()  : int
Returns the amount of elements in the collection.
countBy()  : CollectionInterface
countKeys()  : int
Returns the number of unique keys in this iterator. This is the same as the number of elements the collection will contain after calling `toArray()`
each()  : mixed
every()  : bool
extract()  : CollectionInterface
filter()  : CollectionInterface
first()  : mixed
firstMatch()  : mixed
groupBy()  : CollectionInterface
indexBy()  : CollectionInterface
insert()  : CollectionInterface
isEmpty()  : bool
jsonSerialize()  : array<string|int, mixed>
last()  : mixed
lazy()  : CollectionInterface
listNested()  : CollectionInterface
map()  : CollectionInterface
match()  : CollectionInterface
max()  : mixed
median()  : mixed
min()  : mixed
nest()  : CollectionInterface
prepend()  : CollectionInterface
prependItem()  : CollectionInterface
reduce()  : mixed
reject()  : CollectionInterface
sample()  : CollectionInterface
serialize()  : string
Returns a string representation of this object that can be used to reconstruct it
shuffle()  : CollectionInterface
skip()  : CollectionInterface
some()  : bool
sortBy()  : CollectionInterface
stopWhen()  : CollectionInterface
sumOf()  : mixed
take()  : CollectionInterface
takeLast()  : CollectionInterface
through()  : CollectionInterface
toArray()  : array<string|int, mixed>
toList()  : array<string|int, mixed>
transpose()  : CollectionInterface
{@inheritDoc}
unfold()  : CollectionInterface
unserialize()  : void
Unserializes the passed string and rebuilds the Collection instance
unwrap()  : Traversable
Returns the closest nested iterator that can be safely traversed without losing any possible transformations. This is used mainly to remove empty IteratorIterator wrappers that can only slowdown the iteration process.
zip()  : CollectionInterface
zipWith()  : CollectionInterface
newCollection()  : CollectionInterface
Returns a new collection.
optimizeUnwrap()  : iterable<string|int, mixed>
Unwraps this iterator and returns the simplest traversable that can be used for getting the data out

Methods

__construct()

Wraps this iterator around the passed items so when iterated they are returned in order.

public __construct(iterable<string|int, mixed> $items, callable|string $callback[, int $dir = SORT_DESC ][, int $type = SORT_NUMERIC ]) : mixed

The callback will receive as first argument each of the elements in $items, the value returned in the callback will be used as the value for sorting such element. Please note that the callback function could be called more than once per element.

Parameters
$items : iterable<string|int, mixed>

The values to sort

$callback : callable|string

A function used to return the actual value to be compared. It can also be a string representing the path to use to fetch a column or property in each element

$dir : int = SORT_DESC

either SORT_DESC or SORT_ASC

$type : int = SORT_NUMERIC

the type of comparison to perform, either SORT_STRING SORT_NUMERIC or SORT_NATURAL

__debugInfo()

Returns an array that can be used to describe the internal state of this object.

public __debugInfo() : array<string, mixed>
Return values
array<string, mixed>

__serialize()

Returns an array for serializing this of this object.

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

__unserialize()

Rebuilds the Collection instance.

public __unserialize(array<string|int, mixed> $data) : void
Parameters
$data : array<string|int, mixed>

Data array.

avg()

public avg([mixed $path = null ]) : mixed
Parameters
$path : mixed = null
Tags
inheritDoc

cartesianProduct()

{@inheritDoc}

public cartesianProduct([callable|null $operation = null ][, callable|null $filter = null ]) : CollectionInterface
Parameters
$operation : callable|null = null

A callable that allows you to customize the product result.

$filter : callable|null = null

A filtering callback that must return true for a result to be part of the final results.

Tags
throws
LogicException
Return values
CollectionInterface

contains()

public contains(mixed $value) : bool
Parameters
$value : mixed
Tags
inheritDoc
Return values
bool

count()

Returns the amount of elements in the collection.

public count() : int
Return values
int

countKeys()

Returns the number of unique keys in this iterator. This is the same as the number of elements the collection will contain after calling `toArray()`

public countKeys() : int
Return values
int

each()

public each(callable $callback) : mixed
Parameters
$callback : callable
Tags
inheritDoc

every()

public every(callable $callback) : bool
Parameters
$callback : callable
Tags
inheritDoc
Return values
bool

firstMatch()

public firstMatch(array<string|int, mixed> $conditions) : mixed
Parameters
$conditions : array<string|int, mixed>
Tags
inheritDoc

isEmpty()

public isEmpty() : bool
Tags
inheritDoc
Return values
bool

jsonSerialize()

public jsonSerialize() : array<string|int, mixed>
Tags
inheritDoc
Return values
array<string|int, mixed>

max()

public max(mixed $path[, int $sort = SORT_NUMERIC ]) : mixed
Parameters
$path : mixed
$sort : int = SORT_NUMERIC
Tags
inheritDoc

median()

public median([mixed $path = null ]) : mixed
Parameters
$path : mixed = null
Tags
inheritDoc

min()

public min(mixed $path[, int $sort = SORT_NUMERIC ]) : mixed
Parameters
$path : mixed
$sort : int = SORT_NUMERIC
Tags
inheritDoc

reduce()

public reduce(callable $callback[, mixed $initial = null ]) : mixed
Parameters
$callback : callable
$initial : mixed = null
Tags
inheritDoc

serialize()

Returns a string representation of this object that can be used to reconstruct it

public serialize() : string
Return values
string

some()

public some(callable $callback) : bool
Parameters
$callback : callable
Tags
inheritDoc
Return values
bool

sumOf()

public sumOf([mixed $path = null ]) : mixed
Parameters
$path : mixed = null
Tags
inheritDoc

toArray()

public toArray([bool $keepKeys = true ]) : array<string|int, mixed>
Parameters
$keepKeys : bool = true
Tags
inheritDoc
Return values
array<string|int, mixed>

toList()

public toList() : array<string|int, mixed>
Tags
inheritDoc
Return values
array<string|int, mixed>

unserialize()

Unserializes the passed string and rebuilds the Collection instance

public unserialize(string $collection) : void
Parameters
$collection : string

The serialized collection

unwrap()

Returns the closest nested iterator that can be safely traversed without losing any possible transformations. This is used mainly to remove empty IteratorIterator wrappers that can only slowdown the iteration process.

public unwrap() : Traversable
Return values
Traversable

newCollection()

Returns a new collection.

protected newCollection(mixed ...$args) : CollectionInterface

Allows classes which use this trait to determine their own type of returned collection interface

Parameters
$args : mixed

Constructor arguments.

Return values
CollectionInterface

optimizeUnwrap()

Unwraps this iterator and returns the simplest traversable that can be used for getting the data out

protected optimizeUnwrap() : iterable<string|int, mixed>
Return values
iterable<string|int, mixed>

        
On this page

Search results