Update Composer packages

This commit is contained in:
Paul Nicoué 2023-04-14 16:30:28 +02:00
parent d482354bdf
commit e7e7361480
219 changed files with 6487 additions and 4444 deletions

View file

@ -4,6 +4,7 @@ namespace Kirby\Toolkit;
use Closure;
use Exception;
use InvalidArgumentException;
/**
* The `A` class provides a set of handy methods
@ -47,6 +48,17 @@ class A
return $array;
}
/**
* Counts the number of elements in an array
*
* @param array $array
* @return int
*/
public static function count(array $array): int
{
return count($array);
}
/**
* Gets an element of an array by key
*
@ -114,9 +126,16 @@ class A
// the element needs to exist and also needs to be an array; otherwise
// we cannot find the remaining keys within it (invalid array structure)
if (isset($array[$currentKey]) === true && is_array($array[$currentKey]) === true) {
if (
isset($array[$currentKey]) === true &&
is_array($array[$currentKey]) === true
) {
// $keys only holds the remaining keys that have not been shifted off yet
return static::get($array[$currentKey], implode('.', $keys), $default);
return static::get(
$array[$currentKey],
implode('.', $keys),
$default
);
}
}
@ -127,7 +146,11 @@ class A
// if the input array uses a completely nested structure,
// recursively progress layer by layer
if (is_array($array[$firstKey]) === true) {
return static::get($array[$firstKey], implode('.', $keys), $default);
return static::get(
$array[$firstKey],
implode('.', $keys),
$default
);
}
// the $firstKey element was found, but isn't an array, so we cannot
@ -138,6 +161,19 @@ class A
return $default;
}
/**
* Checks if array has a value
*
* @param array $array
* @param mixed $value
* @param bool $strict
* @return bool
*/
public static function has(array $array, $value, bool $strict = false): bool
{
return in_array($value, $array, $strict);
}
/**
* Joins the elements of an array to a string
*/
@ -146,9 +182,37 @@ class A
if (is_string($value) === true) {
return $value;
}
return implode($separator, $value);
}
/**
* Takes an array and makes it associative by an argument.
* If the argument is a callable, it will be used to map the array.
* If it is a string, it will be used as a key to pluck from the array.
*
* <code>
* $array = [['id'=>1], ['id'=>2], ['id'=>3]];
* $keyed = A::keyBy($array, 'id');
*
* // Now you can access the array by the id
* </code>
*
* @param array $array
* @param string|callable $keyBy
* @return array
*/
public static function keyBy(array $array, string|callable $keyBy): array
{
$keys = is_callable($keyBy) ? static::map($array, $keyBy) : static::pluck($array, $keyBy);
if (count($keys) !== count($array)) {
throw new InvalidArgumentException('The "key by" argument must be a valid key or a callable');
}
return array_combine($keys, $array);
}
public const MERGE_OVERWRITE = 0;
public const MERGE_APPEND = 1;
public const MERGE_REPLACE = 2;
@ -251,6 +315,7 @@ class A
public static function pluck(array $array, string $key): array
{
$output = [];
foreach ($array as $a) {
if (isset($a[$key]) === true) {
$output[] = $a[$key];
@ -268,6 +333,19 @@ class A
return $prepend + $array;
}
/**
* Reduce an array to a single value
*
* @param array $array
* @param callable $callback
* @param mixed $initial
* @return mixed
*/
public static function reduce(array $array, callable $callback, $initial = null): mixed
{
return array_reduce($array, $callback, $initial);
}
/**
* Shuffles an array and keeps the keys
*
@ -304,6 +382,36 @@ class A
return $new;
}
/**
* Returns a slice of an array
*
* @param array $array
* @param int $offset
* @param int|null $length
* @param bool $preserveKeys
* @return array
*/
public static function slice(
array $array,
int $offset,
int $length = null,
bool $preserveKeys = false
): array {
return array_slice($array, $offset, $length, $preserveKeys);
}
/**
* Sums an array
*
* @param array $array
* @return int|float
*/
public static function sum(array $array): int|float
{
return array_sum($array);
}
/**
* Returns the first element of an array
*
@ -391,17 +499,16 @@ class A
* @param int $limit The number of elements the array should
* contain after filling it up.
* @param mixed $fill The element, which should be used to
* fill the array
* fill the array. If it's a callable, it
* will be called with the current index
* @return array The filled-up result array
*/
public static function fill(array $array, int $limit, $fill = 'placeholder'): array
{
if (count($array) < $limit) {
$diff = $limit - count($array);
for ($x = 0; $x < $diff; $x++) {
$array[] = $fill;
}
for ($x = count($array); $x < $limit; $x++) {
$array[] = is_callable($fill) ? $fill($x) : $fill;
}
return $array;
}
@ -467,13 +574,7 @@ class A
*/
public static function missing(array $array, array $required = []): array
{
$missing = [];
foreach ($required as $r) {
if (isset($array[$r]) === false) {
$missing[] = $r;
}
}
return $missing;
return array_values(array_diff($required, array_keys($array)));
}
/**