Update Composer packages

This commit is contained in:
Paul Nicoué 2022-12-19 16:26:24 +01:00
parent df93324906
commit 45bdef9a3b
378 changed files with 28466 additions and 28852 deletions

View file

@ -2,6 +2,7 @@
namespace Kirby\Toolkit;
use Closure;
use Exception;
/**
@ -21,14 +22,10 @@ class A
{
/**
* Appends the given array
*
* @param array $array
* @param array $append
* @return array
*/
public static function append(array $array, array $append): array
{
return $array + $append;
return static::merge($array, $append, A::MERGE_APPEND);
}
/**
@ -37,14 +34,12 @@ class A
* applying the passed parameters
* @since 3.5.6
*
* @param array $array
* @param mixed ...$args Parameters to pass to the closures
* @return array
*/
public static function apply(array $array, ...$args): array
{
array_walk_recursive($array, function (&$item) use ($args) {
if (is_a($item, 'Closure')) {
if ($item instanceof Closure) {
$item = $item(...$args);
}
});
@ -73,13 +68,16 @@ class A
* </code>
*
* @param array $array The source array
* @param mixed $key The key to look for
* @param mixed $default Optional default value, which should be
* returned if no element has been found
* @return mixed
* @param string|int|array|null $key The key to look for
* @param mixed $default Optional default value, which
* should be returned if no element
* has been found
*/
public static function get($array, $key, $default = null)
{
public static function get(
$array,
string|int|array|null $key,
$default = null
) {
if (is_array($array) === false) {
return $array;
}
@ -141,11 +139,9 @@ class A
}
/**
* @param mixed $value
* @param mixed $separator
* @return string
* Joins the elements of an array to a string
*/
public static function join($value, $separator = ', ')
public static function join(array|string $value, string $separator = ', '): string
{
if (is_string($value) === true) {
return $value;
@ -160,43 +156,65 @@ class A
/**
* Merges arrays recursively
*
* @param array $array1
* @param array $array2
* @param int $mode Behavior for elements with numeric keys;
* A::MERGE_APPEND: elements are appended, keys are reset;
* A::MERGE_OVERWRITE: elements are overwritten, keys are preserved
* A::MERGE_REPLACE: non-associative arrays are completely replaced
* @return array
* If last argument is an integer, it defines the
* behavior for elements with numeric keys;
* - A::MERGE_OVERWRITE: elements are overwritten, keys are preserved
* - A::MERGE_APPEND: elements are appended, keys are reset;
* - A::MERGE_REPLACE: non-associative arrays are completely replaced
*/
public static function merge($array1, $array2, int $mode = A::MERGE_APPEND)
public static function merge(array|int ...$arrays): array
{
$merged = $array1;
// get mode from parameters
$last = A::last($arrays);
$mode = is_int($last) ? array_pop($arrays) : A::MERGE_APPEND;
if (static::isAssociative($array1) === false && $mode === static::MERGE_REPLACE) {
return $array2;
}
// get the first two arrays that should be merged
$merged = array_shift($arrays);
$join = array_shift($arrays);
foreach ($array2 as $key => $value) {
// append to the merged array, don't overwrite numeric keys
if (is_int($key) === true && $mode === static::MERGE_APPEND) {
$merged[] = $value;
if (
static::isAssociative($merged) === false &&
$mode === static::MERGE_REPLACE
) {
$merged = $join;
} else {
foreach ($join as $key => $value) {
// append to the merged array, don't overwrite numeric keys
if (
is_int($key) === true &&
$mode === static::MERGE_APPEND
) {
$merged[] = $value;
// recursively merge the two array values
} elseif (is_array($value) === true && isset($merged[$key]) === true && is_array($merged[$key]) === true) {
$merged[$key] = static::merge($merged[$key], $value, $mode);
// recursively merge the two array values
} elseif (
is_array($value) === true &&
isset($merged[$key]) === true &&
is_array($merged[$key]) === true
) {
$merged[$key] = static::merge($merged[$key], $value, $mode);
// simply overwrite with the value from the second array
} else {
$merged[$key] = $value;
// simply overwrite with the value from the second array
} else {
$merged[$key] = $value;
}
}
if ($mode === static::MERGE_APPEND) {
// the keys don't make sense anymore, reset them
// array_merge() is the simplest way to renumber
// arrays that have both numeric and string keys;
// besides the keys, nothing changes here
$merged = array_merge($merged, []);
}
}
if ($mode === static::MERGE_APPEND) {
// the keys don't make sense anymore, reset them
// array_merge() is the simplest way to renumber
// arrays that have both numeric and string keys;
// besides the keys, nothing changes here
$merged = array_merge($merged, []);
// if more than two arrays need to be merged, add the result
// as first array and the mode to the end and call the method again
if (count($arrays) > 0) {
array_unshift($arrays, $merged);
array_push($arrays, $mode);
return static::merge(...$arrays);
}
return $merged;
@ -230,7 +248,7 @@ class A
* @return array The result array with all values
* from that column.
*/
public static function pluck(array $array, string $key)
public static function pluck(array $array, string $key): array
{
$output = [];
foreach ($array as $a) {
@ -244,10 +262,6 @@ class A
/**
* Prepends the given array
*
* @param array $array
* @param array $prepend
* @return array
*/
public static function prepend(array $array, array $prepend): array
{
@ -337,11 +351,6 @@ class A
/**
* Returns a number of random elements from an array,
* either in original or shuffled order
*
* @param array $array
* @param int $count
* @param bool $shuffle
* @return array
*/
public static function random(array $array, int $count = 1, bool $shuffle = false): array
{
@ -400,10 +409,6 @@ class A
* A simple wrapper around array_map
* with a sane argument order
* @since 3.6.0
*
* @param array $array
* @param callable $map
* @return array
*/
public static function map(array $array, callable $map): array
{
@ -412,11 +417,6 @@ class A
/**
* Move an array item to a new index
*
* @param array $array
* @param int $from
* @param int $to
* @return array
*/
public static function move(array $array, int $from, int $to): array
{
@ -480,10 +480,8 @@ class A
* Normalizes an array into a nested form by converting
* dot notation in keys to nested structures
*
* @param array $array
* @param array $ignore List of keys in dot notation that should
* not be converted to a nested structure
* @return array
*/
public static function nest(array $array, array $ignore = []): array
{
@ -523,10 +521,10 @@ class A
is_array($result[$key]) === true &&
is_array($value) === true
) {
$result[$key] = array_replace_recursive($result[$key], $value);
} else {
$result[$key] = $value;
$value = array_replace_recursive($result[$key], $value);
}
$result[$key] = $value;
}
return $result;
@ -661,8 +659,12 @@ class A
* @param int $decimals The number of decimals to return
* @return float The average value
*/
public static function average(array $array, int $decimals = 0): float
public static function average(array $array, int $decimals = 0): float|null
{
if (empty($array) === true) {
return null;
}
return round((array_sum($array) / sizeof($array)), $decimals);
}
@ -681,11 +683,8 @@ class A
* // 'password' => 'super-secret'
* // ];
* </code>
*
* @param array ...$arrays
* @return array
*/
public static function extend(...$arrays): array
public static function extend(array ...$arrays): array
{
return array_merge_recursive(...$arrays);
}
@ -713,19 +712,15 @@ class A
* }
* ]);
* </code>
*
* @param array $array
* @param array $update
* @return array
*/
public static function update(array $array, array $update): array
{
foreach ($update as $key => $value) {
if (is_a($value, 'Closure') === true) {
$array[$key] = call_user_func($value, static::get($array, $key));
} else {
$array[$key] = $value;
if ($value instanceof Closure) {
$value = call_user_func($value, static::get($array, $key));
}
$array[$key] = $value;
}
return $array;
@ -734,29 +729,24 @@ class A
/**
* Wraps the given value in an array
* if it's not an array yet.
*
* @param mixed|null $array
* @return array
*/
public static function wrap($array = null): array
{
if ($array === null) {
return [];
} elseif (is_array($array) === false) {
return [$array];
} else {
return $array;
}
if (is_array($array) === false) {
return [$array];
}
return $array;
}
/**
* Filter the array using the given callback
* using both value and key
* @since 3.6.5
*
* @param array $array
* @param callable $callback
* @return array
*/
public static function filter(array $array, callable $callback): array
{
@ -766,19 +756,16 @@ class A
/**
* Remove key(s) from an array
* @since 3.6.5
*
* @param array $array
* @param int|string|array $keys
* @return array
*/
public static function without(array $array, $keys): array
public static function without(array $array, int|string|array $keys): array
{
if (is_int($keys) || is_string($keys)) {
$keys = static::wrap($keys);
}
return static::filter($array, function ($value, $key) use ($keys) {
return in_array($key, $keys, true) === false;
});
return static::filter(
$array,
fn ($value, $key) => in_array($key, $keys, true) === false
);
}
}