Skip to content

Commit

Permalink
updated Arr class docblocks to @template generics
Browse files Browse the repository at this point in the history
  • Loading branch information
AVMG20 committed Jan 14, 2025
1 parent f73b10c commit fed83c3
Showing 1 changed file with 48 additions and 37 deletions.
85 changes: 48 additions & 37 deletions src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class Arr
/**
* Filter items by the given key value pair.
*
* @param array $array
* @param string|callable $key
* @template T
* @param array<mixed, T> $array
* @param string|callable(T, mixed): bool $key
* @param mixed $operator
* @param mixed $value
* @return array
* @return array<mixed, T>
*/
public static function where(array $array, string|callable $key, mixed $operator = null, mixed $value = null): array
{
Expand All @@ -29,8 +30,9 @@ public static function where(array $array, string|callable $key, mixed $operator
* Determine if an array contains a given item or key-value pair,
* or if a callback returns true for any item.
*
* @param array $array
* @param mixed|callable|string $key
* @template T
* @param array<mixed, T> $array
* @param mixed|callable(T, mixed): bool|string $key
* @param mixed $operator
* @return bool
*/
Expand Down Expand Up @@ -66,25 +68,27 @@ public static function contains(array $array, mixed $key, mixed $operator = null
/**
* Filter items by the given key value pairs.
*
* @param array $array
* @template T
* @param array<mixed, T> $array
* @param string $key
* @param array $values
* @return array
* @param array<mixed> $values
* @return array<mixed, T>
*/
public static function whereIn(array $array, string $key, array $values): array
{
return static::where($array, fn($item) =>
in_array(static::dataGet($item, $key), $values, true)
in_array(static::dataGet($item, $key), $values, true)
);
}

/**
* Filter items by the given key value pair.
*
* @param array $array
* @template T
* @param array<mixed, T> $array
* @param string $key
* @param mixed $value
* @return array
* @return array<mixed, T>
*/
public static function whereNot(array $array, string $key, mixed $value): array
{
Expand All @@ -96,10 +100,11 @@ public static function whereNot(array $array, string $key, mixed $value): array
/**
* Get the first element from the array passing the given truth test.
*
* @param array $array
* @param callable|null $callback
* @param mixed $default
* @return mixed
* @template T
* @param array<mixed, T> $array
* @param (callable(T, mixed): bool)|null $callback
* @param T|null $default
* @return T|null
*/
public static function first(array $array, ?callable $callback = null, mixed $default = null): mixed
{
Expand All @@ -125,11 +130,12 @@ public static function first(array $array, ?callable $callback = null, mixed $de
/**
* Get the first element in the array matching the given key/value pair.
*
* @param array $array
* @param string|callable $key
* @template T
* @param array<mixed, T> $array
* @param string|callable(T, mixed): bool $key
* @param mixed $operator
* @param mixed $value
* @return mixed
* @return T|null
*/
public static function firstWhere(array $array, string|callable $key, mixed $operator = null, mixed $value = null): mixed
{
Expand All @@ -142,7 +148,6 @@ public static function firstWhere(array $array, string|callable $key, mixed $ope
return null;
}

// Handle the case where only key is provided (truthy check)
if (func_num_args() === 2) {
foreach ($array as $item) {
$result = static::dataGet($item, $key);
Expand All @@ -153,7 +158,6 @@ public static function firstWhere(array $array, string|callable $key, mixed $ope
return null;
}

// Use the existing where logic to get the operator closure
$callback = static::operatorForWhere($key, $operator, $value);

foreach ($array as $k => $item) {
Expand All @@ -168,10 +172,11 @@ public static function firstWhere(array $array, string|callable $key, mixed $ope
/**
* Get the last element from the array.
*
* @param array $array
* @param callable|null $callback
* @param mixed $default
* @return mixed
* @template T
* @param array<mixed, T> $array
* @param (callable(T, mixed): bool)|null $callback
* @param T|null $default
* @return T|null
*/
public static function last(array $array, ?callable $callback = null, mixed $default = null): mixed
{
Expand All @@ -191,9 +196,10 @@ public static function last(array $array, ?callable $callback = null, mixed $def
/**
* Filter the array using the given callback.
*
* @param array $array
* @param callable|null $callback
* @return array
* @template T
* @param array<mixed, T> $array
* @param (callable(T, mixed): bool)|null $callback
* @return array<mixed, T>
*/
public static function filter(array $array, ?callable $callback = null): array
{
Expand All @@ -203,9 +209,11 @@ public static function filter(array $array, ?callable $callback = null): array
/**
* Map over each of the items in the array.
*
* @param array $array
* @param callable $callback
* @return array
* @template TIn
* @template TOut
* @param array<mixed, TIn> $array
* @param callable(TIn, mixed): TOut $callback
* @return array<mixed, TOut>
*/
public static function map(array $array, callable $callback): array
{
Expand All @@ -218,8 +226,9 @@ public static function map(array $array, callable $callback): array
/**
* Iterate over each of the items in the array.
*
* @param array $array
* @param callable $callback
* @template T
* @param array<mixed, T> $array
* @param callable(T, mixed): void $callback
* @return void
*/
public static function each(array $array, callable $callback): void
Expand All @@ -243,10 +252,11 @@ protected static function useAsCallable(mixed $value): bool
/**
* Get a callback for filtering using an operator.
*
* @template T
* @param string $key
* @param mixed $operator
* @param mixed $value
* @return Closure
* @return Closure(T, mixed): bool
*/
protected static function operatorForWhere(string $key, mixed $operator = null, mixed $value = null): Closure
{
Expand All @@ -258,7 +268,6 @@ protected static function operatorForWhere(string $key, mixed $operator = null,
return function ($item) use ($key, $operator, $value) {
$retrieved = static::dataGet($item, $key, null);

// Handle null values
if (is_null($retrieved)) {
return false;
}
Expand All @@ -280,7 +289,8 @@ protected static function operatorForWhere(string $key, mixed $operator = null,
/**
* Get an item from an array or object using "dot" notation.
*
* @param mixed $target
* @template T
* @param T $target
* @param string $key
* @param mixed $default
* @return mixed
Expand All @@ -307,8 +317,9 @@ protected static function dataGet(mixed $target, string $key, mixed $default = n
/**
* Return the default value of the given value.
*
* @param mixed $value
* @return mixed
* @template T
* @param T|Closure(): T $value
* @return T
*/
protected static function value(mixed $value): mixed
{
Expand Down

0 comments on commit fed83c3

Please sign in to comment.