Upgrade to 3.8.1

This commit is contained in:
Bastian Allgeier
2022-10-18 14:11:15 +02:00
parent 94b2a32baf
commit 9c93e01c3a
71 changed files with 633 additions and 5705 deletions

View File

@@ -156,47 +156,65 @@ class A
/**
* Merges arrays recursively
*
* @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
* 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(array $array1, array $array2, int $mode = A::MERGE_APPEND): array
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;
// get the first two arrays that should be merged
$merged = array_shift($arrays);
$join = array_shift($arrays);
if (
static::isAssociative($array1) === false &&
static::isAssociative($merged) === false &&
$mode === static::MERGE_REPLACE
) {
return $array2;
}
$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;
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;
// 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;

View File

@@ -232,7 +232,7 @@ class Component
throw new Exception('Component definition ' . $definition . ' does not exist');
}
static::$types[$type] = $definition = F::load($definition);
static::$types[$type] = $definition = F::load($definition, allowOutput: false);
}
return $definition;
@@ -254,7 +254,11 @@ class Component
if (isset($definition['extends']) === true) {
// extend other definitions
$options = array_replace_recursive(static::defaults(), static::load($definition['extends']), $definition);
$options = array_replace_recursive(
static::defaults(),
static::load($definition['extends']),
$definition
);
} else {
// inject defaults
$options = array_replace_recursive(static::defaults(), $definition);
@@ -266,10 +270,14 @@ class Component
if (isset(static::$mixins[$mixin]) === true) {
if (is_string(static::$mixins[$mixin]) === true) {
// resolve a path to a mixin on demand
static::$mixins[$mixin] = include static::$mixins[$mixin];
static::$mixins[$mixin] = F::load(static::$mixins[$mixin], allowOutput: false);
}
$options = array_replace_recursive(static::$mixins[$mixin], $options);
$options = array_replace_recursive(
static::$mixins[$mixin],
$options
);
}
}
}

View File

@@ -281,10 +281,10 @@ class Html extends Xml
*/
public static function gist(string $url, string|null $file = null, array $attr = []): string
{
if ($file === null) {
$src = $url . '.js';
} else {
$src = $url . '.js?file=' . $file;
$src = $url . '.js';
if ($file !== null) {
$src .= '?file=' . $file;
}
return static::tag('script', '', array_merge($attr, ['src' => $src]));

View File

@@ -117,11 +117,13 @@ class Locale
}
return $convertedLocale;
} elseif (is_string($locale)) {
return [LC_ALL => $locale];
} else {
throw new InvalidArgumentException('Locale must be string or array');
}
if (is_string($locale) === true) {
return [LC_ALL => $locale];
}
throw new InvalidArgumentException('Locale must be string or array');
}
/**

View File

@@ -165,12 +165,12 @@ class Query
$args = array_map('self::parameter', $args);
return compact('method', 'args');
} else {
return [
'method' => $part,
'args' => []
];
}
return [
'method' => $part,
'args' => []
];
}
/**

View File

@@ -31,10 +31,10 @@ class Silo
{
if (is_array($key) === true) {
return static::$data = array_merge(static::$data, $key);
} else {
static::$data[$key] = $value;
return static::$data;
}
static::$data[$key] = $value;
return static::$data;
}
/**

View File

@@ -182,9 +182,9 @@ class Xml
if ($head === true) {
return '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL . $result;
} else {
return $result;
}
return $result;
}
/**
@@ -345,13 +345,12 @@ class Xml
// we didn't find any XML children above, only use the string value
$element = (string)$element;
if (count($array) > 0) {
$array['@value'] = $element;
return $array;
} else {
if (count($array) === 0) {
return $element;
}
$array['@value'] = $element;
return $array;
}
}