Upgrade to 3.5.6

This commit is contained in:
Bastian Allgeier
2021-06-08 09:57:59 +02:00
parent fb57845df1
commit 5358f8885c
65 changed files with 506 additions and 223 deletions

View File

@@ -31,6 +31,27 @@ class A
return $array + $append;
}
/**
* Recursively loops through the array and
* resolves any item defined as `Closure`,
* 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')) {
$item = $item(...$args);
}
});
return $array;
}
/**
* Gets an element of an array by key
*

View File

@@ -1135,14 +1135,17 @@ class Collection extends Iterator implements Countable
}
/**
* Returns an non-associative array
* with all values
* Returns a non-associative array
* with all values. If a mapping Closure is passed,
* all values are processed by the Closure.
*
* @param Closure|null $map
* @return array
*/
public function values(): array
public function values(Closure $map = null): array
{
return array_values($this->data);
$data = $map === null ? $this->data : array_map($map, $this->data);
return array_values($data);
}
/**

View File

@@ -2,10 +2,12 @@
namespace Kirby\Toolkit;
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
/**
* PHP locale handling
* @since 3.5.0
*
* @package Kirby Toolkit
* @author Lukas Bestle <lukas@getkirby.com>
@@ -15,6 +17,14 @@ use Kirby\Exception\InvalidArgumentException;
*/
class Locale
{
/**
* List of all locale constants supported by PHP
*/
const LOCALE_CONSTANTS = [
'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY',
'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'
];
/**
* Converts a normalized locale array to an array with the
* locale constants replaced with their string representations
@@ -24,20 +34,7 @@ class Locale
*/
public static function export(array $locale): array
{
// list of all possible constant names
$constantNames = [
'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY',
'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'
];
// build an associative array with the locales
// that are actually supported on this system
$constants = [];
foreach ($constantNames as $name) {
if (defined($name) === true) {
$constants[constant($name)] = $name;
}
}
$constants = static::supportedConstants(true);
// replace the keys in the locale data array with the locale names
$return = [];
@@ -55,6 +52,54 @@ class Locale
return $return;
}
/**
* Returns the current locale value for
* a specified or for all locale categories
* @since 3.5.6
*
* @param int|string $category Locale category constant or constant name
* @return array|string Associative array if `LC_ALL` was passed (default), otherwise string
*
* @throws \Kirby\Exception\Exception If the locale cannot be determined
* @throws \Kirby\Exception\InvalidArgumentException If the provided locale category is invalid
*/
public static function get($category = LC_ALL)
{
$normalizedCategory = static::normalizeConstant($category);
if (is_int($normalizedCategory) !== true) {
throw new InvalidArgumentException('Invalid locale category "' . $category . '"');
}
if ($normalizedCategory !== LC_ALL) {
// `setlocale(..., 0)` actually *gets* the locale
$locale = setlocale($normalizedCategory, 0);
if (is_string($locale) !== true) {
throw new Exception('Could not determine locale for category "' . $category . '"');
}
return $locale;
}
// no specific `$category` was passed, make a list of all locales
$array = [];
foreach (static::supportedConstants() as $constant => $name) {
// `setlocale(..., 0)` actually *gets* the locale
$array[$constant] = setlocale($constant, '0');
}
// if all values are the same, we can use `LC_ALL`
// instead of a long array with all constants
if (count(array_unique($array)) === 1) {
return [
LC_ALL => array_shift($array)
];
}
return $array;
}
/**
* Converts a locale string or an array with constant or
* string keys to a normalized constant => value array
@@ -68,11 +113,7 @@ class Locale
// replace string constant keys with the constant values
$convertedLocale = [];
foreach ($locale as $key => $value) {
if (is_string($key) === true && Str::startsWith($key, 'LC_') === true) {
$key = constant($key);
}
$convertedLocale[$key] = $value;
$convertedLocale[static::normalizeConstant($key)] = $value;
}
return $convertedLocale;
@@ -98,4 +139,45 @@ class Locale
setlocale($key, $value);
}
}
/**
* Tries to convert an `LC_*` constant name
* to its constant value
*
* @param int|string $constant
* @return int|string
*/
protected static function normalizeConstant($constant)
{
if (is_string($constant) === true && Str::startsWith($constant, 'LC_') === true) {
return constant($constant);
}
// already an int or we cannot convert it safely
return $constant;
}
/**
* Builds an associative array with the locales
* that are actually supported on this system
*
* @param bool $withAll If set to `true`, `LC_ALL` is returned as well
* @return array
*/
protected static function supportedConstants(bool $withAll = false): array
{
$names = static::LOCALE_CONSTANTS;
if ($withAll === true) {
array_unshift($names, 'LC_ALL');
}
$constants = [];
foreach ($names as $name) {
if (defined($name) === true) {
$constants[constant($name)] = $name;
}
}
return $constants;
}
}

View File

@@ -805,6 +805,7 @@ class Str
/**
* Calculates the similarity between two strings with multibyte support
* @since 3.5.2
*
* @author Based on the work of Antal Áron
* @copyright Original Copyright (c) 2017, Antal Áron