Upgrade to 3.5.2

This commit is contained in:
Bastian Allgeier
2021-02-09 16:55:18 +01:00
parent 99c36fa137
commit b507926ad1
53 changed files with 350 additions and 355 deletions

View File

@@ -597,7 +597,7 @@ class A
*/
public static function isAssociative(array $array): bool
{
return ctype_digit(implode(null, array_keys($array))) === false;
return ctype_digit(implode('', array_keys($array))) === false;
}
/**

View File

@@ -252,7 +252,7 @@ class Collection extends Iterator implements Countable
$split = $args[1] ?? false;
// filter by custom filter function
if (is_callable($field) === true) {
if (is_string($field) === false && is_callable($field) === true) {
$collection = clone $this;
$collection->data = array_filter($this->data, $field);

View File

@@ -525,9 +525,12 @@ class F
* Converts an integer size into a human readable format
*
* @param mixed $size The file size or a file path
* @return string|int
* @param string|null|false $locale Locale for number formatting,
* `null` for the current locale,
* `false` to disable number formatting
* @return string
*/
public static function niceSize($size): string
public static function niceSize($size, $locale = null): string
{
// file mode
if (is_string($size) === true && file_exists($size) === true) {
@@ -539,11 +542,18 @@ class F
// avoid errors for invalid sizes
if ($size <= 0) {
return '0 KB';
return '0 KB';
}
// the math magic
return round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . static::$units[$i];
$size = round($size / pow(1024, ($unit = floor(log($size, 1024)))), 2);
// format the number if requested
if ($locale !== false) {
$size = I18n::formatNumber($size, $locale);
}
return $size . ' ' . static::$units[$unit];
}
/**

View File

@@ -129,7 +129,7 @@ class Iterator implements IteratorAggregate
* Tries to find the index number for the given element
*
* @param mixed $needle the element to search for
* @return string|false the name of the key or false
* @return int|false the index (int) of the element or false
*/
public function indexOf($needle)
{

View File

@@ -196,7 +196,7 @@ class Pagination
return 0;
}
return ceil($this->total() / $this->limit());
return (int)ceil($this->total() / $this->limit());
}
/**

View File

@@ -410,7 +410,7 @@ class Str
*/
public static function isURL(string $string = null): bool
{
return filter_var($string, FILTER_VALIDATE_URL);
return filter_var($string, FILTER_VALIDATE_URL) !== false;
}
/**
@@ -803,6 +803,78 @@ class Str
return static::short($string, $maxlength, false);
}
/**
* Calculates the similarity between two strings with multibyte support
*
* @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
*/
public static function similarity(string $first, string $second, bool $caseInsensitive = false): array
{
$matches = 0;
$percent = 0.0;
if ($caseInsensitive === true) {
$first = static::lower($first);
$second = static::lower($second);
}
if (static::length($first) + static::length($second) > 0) {
$pos1 = $pos2 = $max = 0;
$len1 = static::length($first);
$len2 = static::length($second);
for ($p = 0; $p < $len1; ++$p) {
for ($q = 0; $q < $len2; ++$q) {
for (
$l = 0;
($p + $l < $len1) && ($q + $l < $len2) &&
static::substr($first, $p + $l, 1) === static::substr($second, $q + $l, 1);
++$l
) {
// nothing to do
}
if ($l > $max) {
$max = $l;
$pos1 = $p;
$pos2 = $q;
}
}
}
$matches = $max;
if ($matches) {
if ($pos1 && $pos2) {
$similarity = static::similarity(
static::substr($first, 0, $pos1),
static::substr($second, 0, $pos2)
);
$matches += $similarity['matches'];
}
if (($pos1 + $max < $len1) && ($pos2 + $max < $len2)) {
$similarity = static::similarity(
static::substr($first, $pos1 + $max, $len1 - $pos1 - $max),
static::substr($second, $pos2 + $max, $len2 - $pos2 - $max)
);
$matches += $similarity['matches'];
}
}
$percent = ($matches * 200.0) / ($len1 + $len2);
}
return compact('matches', 'percent');
}
/**
* Convert a string to snake case.
*