Upgrade to 3.9.0

This commit is contained in:
Bastian Allgeier
2023-01-17 14:50:16 +01:00
parent 0ebe0c7b16
commit 6e5c9d1f48
132 changed files with 1664 additions and 1254 deletions

View File

@@ -25,17 +25,13 @@ class Str
{
/**
* Language translation table
*
* @var array
*/
public static $language = [];
public static array $language = [];
/**
* Ascii translation table
*
* @var array
*/
public static $ascii = [
public static array $ascii = [
'/°|₀/' => '0',
'/¹|₁/' => '1',
'/²|₂/' => '2',
@@ -117,10 +113,8 @@ class Str
/**
* Default settings for class methods
*
* @var array
*/
public static $defaults = [
public static array $defaults = [
'slug' => [
'separator' => '-',
'allowed' => 'a-z0-9'
@@ -130,9 +124,6 @@ class Str
/**
* Parse accepted values and their quality from an
* accept string like an Accept or Accept-Language header
*
* @param string $input
* @return array
*/
public static function accepted(string $input): array
{
@@ -174,11 +165,6 @@ class Str
/**
* Returns the rest of the string after the given substring or character
*
* @param string $string
* @param string $needle
* @param bool $caseInsensitive
* @return string
*/
public static function after(string $string, string $needle, bool $caseInsensitive = false): string
{
@@ -192,13 +178,9 @@ class Str
}
/**
* Removes the given substring or character only from the start of the string
* Removes the given substring or character
* only from the start of the string
* @since 3.7.0
*
* @param string $string
* @param string $needle
* @param bool $caseInsensitive
* @return string
*/
public static function afterStart(string $string, string $needle, bool $caseInsensitive = false): string
{
@@ -215,9 +197,6 @@ class Str
/**
* Convert a string to 7-bit ASCII.
*
* @param string $string
* @return string
*/
public static function ascii(string $string): string
{
@@ -238,11 +217,6 @@ class Str
/**
* Returns the beginning of a string before the given substring or character
*
* @param string $string
* @param string $needle
* @param bool $caseInsensitive
* @return string
*/
public static function before(string $string, string $needle, bool $caseInsensitive = false): string
{
@@ -258,11 +232,6 @@ class Str
/**
* Removes the given substring or character only from the end of the string
* @since 3.7.0
*
* @param string $string
* @param string $needle
* @param bool $caseInsensitive
* @return string
*/
public static function beforeEnd(string $string, string $needle, bool $caseInsensitive = false): string
{
@@ -279,11 +248,6 @@ class Str
/**
* Returns everything between two strings from the first occurrence of a given string
*
* @param string $string
* @param string $start
* @param string $end
* @return string
*/
public static function between(string $string = null, string $start, string $end): string
{
@@ -294,7 +258,6 @@ class Str
* Converts a string to camel case
*
* @param string $value The string to convert
* @return string
*/
public static function camel(string $value = null): string
{
@@ -303,11 +266,6 @@ class Str
/**
* Checks if a str contains another string
*
* @param string $string
* @param string $needle
* @param bool $caseInsensitive
* @return bool
*/
public static function contains(string $string = null, string $needle, bool $caseInsensitive = false): bool
{
@@ -323,12 +281,9 @@ class Str
* Convert timestamp to date string
* according to locale settings
*
* @param int|null $time
* @param string|\IntlDateFormatter|null $format
* @param string $handler date, intl or strftime
* @return string|int
*/
public static function date(int|null $time = null, $format = null, string $handler = 'date')
public static function date(int|null $time = null, string|IntlDateFormatter $format = null, string $handler = 'date'): string|int|false
{
if (is_null($format) === true) {
return $time;
@@ -365,18 +320,11 @@ class Str
/**
* Converts a string to a different encoding
*
* @param string $string
* @param string $targetEncoding
* @param string $sourceEncoding (optional)
* @return string
*/
public static function convert($string, $targetEncoding, $sourceEncoding = null)
public static function convert(string $string, string $targetEncoding, string $sourceEncoding = null): string
{
// detect the source encoding if not passed as third argument
if ($sourceEncoding === null) {
$sourceEncoding = static::encoding($string);
}
$sourceEncoding ??= static::encoding($string);
// no need to convert if the target encoding is the same
if (strtolower($sourceEncoding) === strtolower($targetEncoding)) {
@@ -388,9 +336,6 @@ class Str
/**
* Encode a string (used for email addresses)
*
* @param string $string
* @return string
*/
public static function encode(string $string): string
{
@@ -407,22 +352,18 @@ class Str
/**
* Tries to detect the string encoding
*
* @param string $string
* @return string
*/
public static function encoding(string $string): string
{
return mb_detect_encoding($string, 'UTF-8, ISO-8859-1, windows-1251', true);
return mb_detect_encoding(
$string,
'UTF-8, ISO-8859-1, windows-1251',
true
);
}
/**
* Checks if a string ends with the passed needle
*
* @param string $string
* @param string $needle
* @param bool $caseInsensitive
* @return bool
*/
public static function endsWith(string $string = null, string $needle, bool $caseInsensitive = false): bool
{
@@ -499,11 +440,8 @@ class Str
/**
* Convert the value to a float with a decimal
* point, no matter what the locale setting is
*
* @param string|int|float $value
* @return string
*/
public static function float($value): string
public static function float(string|int|float|null $value): string
{
// make sure $value is not null
$value ??= '';
@@ -520,11 +458,6 @@ class Str
/**
* Returns the rest of the string starting from the given character
*
* @param string $string
* @param string $needle
* @param bool $caseInsensitive
* @return string
*/
public static function from(string $string, string $needle, bool $caseInsensitive = false): string
{
@@ -542,9 +475,7 @@ class Str
* @since 3.7.0
*
* @param string $string The string to increment
* @param string $separator
* @param int $first Starting number
* @return string
*/
public static function increment(string $string, string $separator = '-', int $first = 1): string
{
@@ -561,9 +492,6 @@ class Str
/**
* Convert a string to kebab case.
*
* @param string $value
* @return string
*/
public static function kebab(string $value = null): string
{
@@ -572,9 +500,6 @@ class Str
/**
* A UTF-8 safe version of strlen()
*
* @param string $string
* @return int
*/
public static function length(string $string = null): int
{
@@ -583,9 +508,6 @@ class Str
/**
* A UTF-8 safe version of strtolower()
*
* @param string $string
* @return string
*/
public static function lower(string $string = null): string
{
@@ -594,10 +516,6 @@ class Str
/**
* Safe ltrim alternative
*
* @param string $string
* @param string $trim
* @return string
*/
public static function ltrim(string $string, string $trim = ' '): string
{
@@ -607,12 +525,8 @@ class Str
/**
* Get a character pool with various possible combinations
*
* @param string|array $type
* @param bool $array
* @return string|array
*/
public static function pool($type, bool $array = true)
public static function pool(string|array $type, bool $array = true): string|array
{
$pool = [];
@@ -638,12 +552,9 @@ class Str
* Returns the position of a needle in a string
* if it can be found
*
* @param string $string
* @param string $needle
* @param bool $caseInsensitive
* @return int|bool
* @throws \Kirby\Exception\InvalidArgumentException for empty $needle
*/
public static function position(string $string = null, string $needle, bool $caseInsensitive = false)
public static function position(string $string = null, string $needle, bool $caseInsensitive = false): int|bool
{
if ($needle === '') {
throw new InvalidArgumentException('The needle must not be empty');
@@ -660,10 +571,6 @@ class Str
/**
* Runs a string query.
* Check out the Query class for more information.
*
* @param string $query
* @param array $data
* @return string|null
*/
public static function query(string $query, array $data = [])
{
@@ -675,15 +582,11 @@ class Str
*
* @param int $length The length of the random string
* @param string $type Pool type (type of allowed characters)
* @return string
*/
public static function random(int $length = null, string $type = 'alphaNum')
public static function random(int $length = null, string $type = 'alphaNum'): string|false
{
if ($length === null) {
$length = random_int(5, 10);
}
$pool = static::pool($type, false);
$length ??= random_int(5, 10);
$pool = static::pool($type, false);
// catch invalid pools
if (!$pool) {
@@ -883,10 +786,17 @@ class Str
*
* @return string The filled-in and partially escaped string
*/
public static function safeTemplate(string $string = null, array $data = [], array $options = []): string
{
$callback = ($options['callback'] ?? null) instanceof Closure ? $options['callback'] : null;
public static function safeTemplate(
string $string = null,
array $data = [],
array $options = []
): string {
$fallback = $options['fallback'] ?? null;
$callback = $options['callback'] ?? null;
if ($callback instanceof Closure === false) {
$callback = null;
}
// replace and escape
$string = static::template($string, $data, [
@@ -957,8 +867,7 @@ class Str
* @author Based on the work of Antal Áron
* @copyright Original Copyright (c) 2017, Antal Áron
* @license https://github.com/antalaron/mb-similar-text/blob/master/LICENSE MIT License
* @param string $first
* @param string $second
*
* @param bool $caseInsensitive If `true`, strings are compared case-insensitively
* @return array matches: Number of matching chars in both strings
* percent: Similarity in percent
@@ -1033,8 +942,12 @@ class Str
* @param int $maxlength The maximum length of the slug
* @return string The safe string
*/
public static function slug(string $string = null, string $separator = null, string $allowed = null, int $maxlength = 128): string
{
public static function slug(
string $string = null,
string $separator = null,
string $allowed = null,
int $maxlength = 128
): string {
$separator ??= static::$defaults['slug']['separator'];
$allowed ??= static::$defaults['slug']['allowed'];
@@ -1063,10 +976,6 @@ class Str
/**
* Convert a string to snake case.
*
* @param string $value
* @param string $delimiter
* @return string
*/
public static function snake(string $value = null, string $delimiter = '_'): string
{
@@ -1088,7 +997,7 @@ class Str
* @param int $length The min length of values.
* @return array An array of found values
*/
public static function split($string, string $separator = ',', int $length = 1): array
public static function split(string|array|null $string, string $separator = ',', int $length = 1): array
{
if (is_array($string) === true) {
return $string;
@@ -1112,11 +1021,6 @@ class Str
/**
* Checks if a string starts with the passed needle
*
* @param string $string
* @param string $needle
* @param bool $caseInsensitive
* @return bool
*/
public static function startsWith(string $string = null, string $needle, bool $caseInsensitive = false): bool
{
@@ -1132,7 +1036,6 @@ class Str
* @since 3.7.0
*
* @param string $value The string to convert
* @return string
*/
public static function studly(string $value = null): string
{
@@ -1141,11 +1044,6 @@ class Str
/**
* A UTF-8 safe version of substr()
*
* @param string $string
* @param int $start
* @param int $length
* @return string
*/
public static function substr(string $string = null, int $start = 0, int $length = null): string
{
@@ -1173,12 +1071,19 @@ class Str
* - end: end placeholder
* @return string The filled-in string
*/
public static function template(string $string = null, array $data = [], array $options = []): string
{
$fallback = $options['fallback'] ?? null;
$callback = ($options['callback'] ?? null) instanceof Closure ? $options['callback'] : null;
public static function template(
string $string = null,
array $data = [],
array $options = []
): string {
$start = (string)($options['start'] ?? '{{');
$end = (string)($options['end'] ?? '}}');
$fallback = $options['fallback'] ?? null;
$callback = $options['callback'] ?? null;
if ($callback instanceof Closure === false) {
$callback = null;
}
// make sure $string is string
$string ??= '';
@@ -1219,9 +1124,6 @@ class Str
/**
* Converts a filesize string with shortcuts
* like M, G or K to an integer value
*
* @param string $size
* @return int
*/
public static function toBytes(string $size): int
{
@@ -1229,26 +1131,18 @@ class Str
$last = strtolower($size[strlen($size)-1] ?? '');
$size = (int)$size;
switch ($last) {
case 'g':
$size *= 1024;
// no break
case 'm':
$size *= 1024;
// no break
case 'k':
$size *= 1024;
}
$size *= match ($last) {
'g' => 1024 * 1024 * 1024,
'm' => 1024 * 1024,
'k' => 1024,
default => 1
};
return $size;
}
/**
* Convert the string to the given type
*
* @param string $string
* @param mixed $type
* @return mixed
*/
public static function toType($string, $type)
{
@@ -1267,10 +1161,6 @@ class Str
/**
* Safe trim alternative
*
* @param string $string
* @param string $trim
* @return string
*/
public static function trim(string $string, string $trim = ' '): string
{
@@ -1279,20 +1169,16 @@ class Str
/**
* A UTF-8 safe version of ucfirst()
*
* @param string $string
* @return string
*/
public static function ucfirst(string $string = null): string
{
return static::upper(static::substr($string, 0, 1)) . static::lower(static::substr($string, 1));
$first = static::substr($string, 0, 1);
$rest = static::substr($string, 1);
return static::upper($first) . static::lower($rest);
}
/**
* A UTF-8 safe version of ucwords()
*
* @param string $string
* @return string
*/
public static function ucwords(string $string = null): string
{
@@ -1308,9 +1194,6 @@ class Str
* // output: some uber crazy stuff
*
* </code>
*
* @param string $string
* @return string The html string
*/
public static function unhtml(string $string = null): string
{
@@ -1319,11 +1202,6 @@ class Str
/**
* Returns the beginning of a string until the given character
*
* @param string $string
* @param string $needle
* @param bool $caseInsensitive
* @return string
*/
public static function until(string $string, string $needle, bool $caseInsensitive = false): string
{
@@ -1338,9 +1216,6 @@ class Str
/**
* A UTF-8 safe version of strotoupper()
*
* @param string $string
* @return string
*/
public static function upper(string $string = null): string
{
@@ -1351,8 +1226,6 @@ class Str
* Creates a compliant v4 UUID
* Taken from: https://github.com/symfony/polyfill
* @since 3.7.0
*
* @return string
*/
public static function uuid(): string
{
@@ -1381,9 +1254,6 @@ class Str
* The widont function makes sure that there are no
* typographical widows at the end of a paragraph
* that's a single word in the last line
*
* @param string $string
* @return string
*/
public static function widont(string $string = null): string
{
@@ -1411,7 +1281,6 @@ class Str
* @param string $string String to wrap
* @param string $before String to prepend
* @param string|null $after String to append (if different from `$before`)
* @return string
*/
public static function wrap(string $string, string $before, string $after = null): string
{