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

@@ -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.
*