Upgrade to 3.9.2

This commit is contained in:
Bastian Allgeier
2023-03-08 12:24:06 +01:00
parent c58864a585
commit 3b0b4feb16
44 changed files with 672 additions and 201 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
@@ -161,6 +162,33 @@ class A
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;
@@ -404,15 +432,14 @@ 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
{
$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;