• Full name: \App\Shared\Services\ListUtil
  • This class is marked as final and can't be subclassed
  • This class is a Final class

Properties

input

private array<array-key,array<string|int,mixed>|object> $input

output

private array<array-key,mixed> $output

Methods

__construct

public __construct(array<array-key,array<string|int,mixed>|object> $input): mixed

Parameters:

Parameter Type Description
$input array\|object>

filter

Filters the list, based on a set of key => value arguments.

public filter(array $args = [], string $operator = 'AND'): array

Parameters:

Parameter Type Description
$args array Optional. An array of key => value arguments to match
against each object. Default empty array.
$operator string Optional. The logical operation to perform. 'AND' means
all elements from the array must match. 'OR' means only
one element needs to match. 'NOT' means no elements may
match. Default 'AND'.

Return Value:

Array of found values.

Throws:


pluck

Plucks a certain field out of each object in the list.

public pluck(int|string $field, int|string|null $indexKey = null): array

This has the same functionality and prototype of array_column() but also supports objects.

Parameters:

Parameter Type Description
$field int|string Field from the object to place instead of the entire object
$indexKey int|string|null Optional. Field from the object to use as keys for the new array.
Default null.

Return Value:

Array of found values. If $indexKey is set, an array of found values with keys corresponding to $indexKey. If $indexKey is null, array keys from the original $list will be preserved in the results.


sort

Sorts the list, based on one or more orderby arguments.

public sort(array|string $orderby = [], string $order = 'ASC', bool $preserveKeys = false): array

Parameters:

Parameter Type Description
$orderby array|string Optional. Either the field name to order by or an array
of multiple orderby fields as $orderby => $order.
$order string Optional. Either 'ASC' or 'DESC'. Only used if $orderby
is a string.
$preserveKeys bool Optional. Whether to preserve keys. Default false.

Return Value:

The sorted array.


reset

Reset output back to original input.

public reset(): array<array-key,array<string|int,mixed>|object>

all

Return the current working list.

public all(): array<array-key,mixed>

compareItems

private compareItems(mixed $a, mixed $b, array<string,string> $orderBy): int

Parameters:

Parameter Type Description
$a mixed
$b mixed
$orderBy array

compareValues

private compareValues(mixed $a, mixed $b): int

Parameters:

Parameter Type Description
$a mixed
$b mixed

value

private value(mixed $item, int|string $field): \App\Shared\Services\FieldValue

Parameters:

Parameter Type Description
$item mixed
$field int|string

isValidArrayKey

private isValidArrayKey(mixed $value): bool

Parameters:

Parameter Type Description
$value mixed