Upgrade to 3.9.3

This commit is contained in:
Bastian Allgeier
2023-03-30 15:17:37 +02:00
parent 20e971adb4
commit cd495f054e
47 changed files with 3386 additions and 2384 deletions

View File

@@ -48,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
*
@@ -150,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
*/
@@ -309,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
*
@@ -345,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
*

View File

@@ -522,6 +522,49 @@ class Str
return preg_replace('!^(' . preg_quote($trim) . ')+!', '', $string);
}
/**
* Match string against a regular expression and return matches
*
* @param string $string The string to match
* @param string $pattern The regular expression
* @param int $flags Optional flags for PHP `preg_match()`
* @param int $offset Positional offset in the string to start the search
* @return array|null The matches or null if no match was found
*/
public static function match(string $string, string $pattern, int $flags = 0, int $offset = 0): ?array
{
$result = preg_match($pattern, $string, $matches, $flags, $offset);
return ($result === 1) ? $matches : null;
}
/**
* Check whether a string matches a regular expression
*
* @param string $string The string to match
* @param string $pattern The regular expression
* @param int $flags Optional flags for PHP `preg_match()`
* @param int $offset Positional offset in the string to start the search
* @return bool True if the string matches the pattern
*/
public static function matches(string $string, string $pattern, int $flags = 0, int $offset = 0): bool
{
return static::match($string, $pattern, $flags, $offset) !== null;
}
/**
* Match string against a regular expression and return all matches
*
* @param string $string The string to match
* @param string $pattern The regular expression
* @param int $flags Optional flags for PHP `preg_match_all()`
* @param int $offset Positional offset in the string to start the search
* @return array|null The matches or null if no match was found
*/
public static function matchAll(string $string, string $pattern, int $flags = 0, int $offset = 0): ?array
{
$result = preg_match_all($pattern, $string, $matches, $flags, $offset);
return ($result > 0) ? $matches : null;
}
/**
* Get a character pool with various possible combinations