Upgrade to 3.8.0
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use Closure;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use IntlDateFormatter;
|
||||
use Kirby\Cms\Helpers;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
@@ -326,14 +326,14 @@ class Str
|
||||
* @param string $handler date, intl or strftime
|
||||
* @return string|int
|
||||
*/
|
||||
public static function date(?int $time = null, $format = null, string $handler = 'date')
|
||||
public static function date(int|null $time = null, $format = null, string $handler = 'date')
|
||||
{
|
||||
if (is_null($format) === true) {
|
||||
return $time;
|
||||
}
|
||||
|
||||
// $format is an IntlDateFormatter instance
|
||||
if (is_a($format, 'IntlDateFormatter') === true) {
|
||||
if ($format instanceof IntlDateFormatter) {
|
||||
return $format->format($time ?? time());
|
||||
}
|
||||
|
||||
@@ -469,7 +469,12 @@ class Str
|
||||
public static function excerpt($string, $chars = 140, $strip = true, $rep = ' …')
|
||||
{
|
||||
if ($strip === true) {
|
||||
$string = strip_tags(str_replace('<', ' <', $string));
|
||||
// ensure that opening tags are preceded by a space, so that
|
||||
// when tags are skipped we can be sure that words stay separate
|
||||
$string = preg_replace('#\s*<([^\/])#', ' <${1}', $string);
|
||||
|
||||
// in strip mode, we always return plain text
|
||||
$string = strip_tags($string);
|
||||
}
|
||||
|
||||
// replace line breaks with spaces
|
||||
@@ -552,21 +557,6 @@ class Str
|
||||
return $string . $separator . $first;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given string is a URL
|
||||
*
|
||||
* @param string|null $string
|
||||
* @return bool
|
||||
* @deprecated 3.6.0 use `Kirby\Toolkit\V::url()` instead
|
||||
* @todo Remove in 3.8.0
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function isURL(?string $string = null): bool
|
||||
{
|
||||
Helpers::deprecated('Toolkit\Str::isUrl() has been deprecated and will be removed in Kirby 3.8.0. Use Toolkit\V::url() instead.');
|
||||
return filter_var($string, FILTER_VALIDATE_URL) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string to kebab case.
|
||||
*
|
||||
@@ -629,23 +619,14 @@ class Str
|
||||
$pool = array_merge($pool, static::pool($t));
|
||||
}
|
||||
} else {
|
||||
switch (strtolower($type)) {
|
||||
case 'alphalower':
|
||||
$pool = range('a', 'z');
|
||||
break;
|
||||
case 'alphaupper':
|
||||
$pool = range('A', 'Z');
|
||||
break;
|
||||
case 'alpha':
|
||||
$pool = static::pool(['alphaLower', 'alphaUpper']);
|
||||
break;
|
||||
case 'num':
|
||||
$pool = range(0, 9);
|
||||
break;
|
||||
case 'alphanum':
|
||||
$pool = static::pool(['alpha', 'num']);
|
||||
break;
|
||||
}
|
||||
$pool = match (strtolower($type)) {
|
||||
'alphalower' => range('a', 'z'),
|
||||
'alphaupper' => range('A', 'Z'),
|
||||
'alpha' => static::pool(['alphaLower', 'alphaUpper']),
|
||||
'num' => range(0, 9),
|
||||
'alphanum' => static::pool(['alpha', 'num']),
|
||||
default => $pool
|
||||
};
|
||||
}
|
||||
|
||||
return $array ? $pool : implode('', $pool);
|
||||
@@ -735,19 +716,20 @@ class Str
|
||||
* defaults to no limit
|
||||
* @return string|array String with replaced values;
|
||||
* if $string is an array, array of strings
|
||||
* @psalm-return ($string is array ? array : string)
|
||||
*/
|
||||
public static function replace($string, $search, $replace, $limit = -1)
|
||||
{
|
||||
// convert Kirby collections to arrays
|
||||
if (is_a($string, 'Kirby\Toolkit\Collection') === true) {
|
||||
if ($string instanceof Collection) {
|
||||
$string = $string->toArray();
|
||||
}
|
||||
|
||||
if (is_a($search, 'Kirby\Toolkit\Collection') === true) {
|
||||
if ($search instanceof Collection) {
|
||||
$search = $search->toArray();
|
||||
}
|
||||
|
||||
if (is_a($replace, 'Kirby\Toolkit\Collection') === true) {
|
||||
if ($replace instanceof Collection) {
|
||||
$replace = $replace->toArray();
|
||||
}
|
||||
|
||||
@@ -901,8 +883,8 @@ class Str
|
||||
*/
|
||||
public static function safeTemplate(string $string = null, array $data = [], array $options = []): string
|
||||
{
|
||||
$callback = is_a(($options['callback'] ?? null), 'Closure') === true ? $options['callback'] : null;
|
||||
$fallback = $options['fallback'] ?? '';
|
||||
$callback = ($options['callback'] ?? null) instanceof Closure ? $options['callback'] : null;
|
||||
$fallback = $options['fallback'] ?? null;
|
||||
|
||||
// replace and escape
|
||||
$string = static::template($string, $data, [
|
||||
@@ -1099,7 +1081,7 @@ class Str
|
||||
* and it has a built-in way to skip values
|
||||
* which are too short.
|
||||
*
|
||||
* @param string $string The string to split
|
||||
* @param string|array|null $string The string to split
|
||||
* @param string $separator The string to split by
|
||||
* @param int $length The min length of values.
|
||||
* @return array An array of found values
|
||||
@@ -1192,7 +1174,7 @@ class Str
|
||||
public static function template(string $string = null, array $data = [], array $options = []): string
|
||||
{
|
||||
$fallback = $options['fallback'] ?? null;
|
||||
$callback = is_a(($options['callback'] ?? null), 'Closure') === true ? $options['callback'] : null;
|
||||
$callback = ($options['callback'] ?? null) instanceof Closure ? $options['callback'] : null;
|
||||
$start = (string)($options['start'] ?? '{{');
|
||||
$end = (string)($options['end'] ?? '}}');
|
||||
|
||||
@@ -1206,7 +1188,7 @@ class Str
|
||||
if (strpos($query, '.') !== false) {
|
||||
try {
|
||||
$result = (new Query($match[1], $data))->result();
|
||||
} catch (Exception $e) {
|
||||
} catch (Exception) {
|
||||
$result = null;
|
||||
}
|
||||
} else {
|
||||
@@ -1220,7 +1202,14 @@ class Str
|
||||
|
||||
// callback on result if given
|
||||
if ($callback !== null) {
|
||||
$result = $callback((string)$result, $query, $data);
|
||||
$callbackResult = $callback((string)$result, $query, $data);
|
||||
|
||||
if ($result === null && $callbackResult === '') {
|
||||
// the empty string came just from string casting,
|
||||
// keep the null value and ignore the callback result
|
||||
} else {
|
||||
$result = $callbackResult;
|
||||
}
|
||||
}
|
||||
|
||||
// if we still don't have a result, keep the original placeholder
|
||||
@@ -1268,21 +1257,13 @@ class Str
|
||||
$type = gettype($type);
|
||||
}
|
||||
|
||||
switch ($type) {
|
||||
case 'array':
|
||||
return (array)$string;
|
||||
case 'bool':
|
||||
case 'boolean':
|
||||
return filter_var($string, FILTER_VALIDATE_BOOLEAN);
|
||||
case 'double':
|
||||
case 'float':
|
||||
return (float)$string;
|
||||
case 'int':
|
||||
case 'integer':
|
||||
return (int)$string;
|
||||
}
|
||||
|
||||
return (string)$string;
|
||||
return match ($type) {
|
||||
'array' => (array)$string,
|
||||
'bool', 'boolean' => filter_var($string, FILTER_VALIDATE_BOOLEAN),
|
||||
'double', 'float' => (float)$string,
|
||||
'int', 'integer' => (int)$string,
|
||||
default => (string)$string
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user