| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Polyfill\Php85; |
| 13 |
|
| 14 |
/** |
| 15 |
* @author Pierre Ambroise <[email protected]> |
| 16 |
* @author Alexander Schranz <[email protected]> |
| 17 |
* |
| 18 |
* @internal |
| 19 |
*/ |
| 20 |
final class Php85 |
| 21 |
{ |
| 22 |
public static function get_error_handler(): ?callable |
| 23 |
{ |
| 24 |
$handler = set_error_handler(null); |
| 25 |
restore_error_handler(); |
| 26 |
|
| 27 |
return $handler; |
| 28 |
} |
| 29 |
|
| 30 |
public static function get_exception_handler(): ?callable |
| 31 |
{ |
| 32 |
$handler = set_exception_handler(null); |
| 33 |
restore_exception_handler(); |
| 34 |
|
| 35 |
return $handler; |
| 36 |
} |
| 37 |
|
| 38 |
public static function array_first(array $array) |
| 39 |
{ |
| 40 |
foreach ($array as $value) { |
| 41 |
return $value; |
| 42 |
} |
| 43 |
|
| 44 |
return null; |
| 45 |
} |
| 46 |
|
| 47 |
public static function array_last(array $array) |
| 48 |
{ |
| 49 |
return $array ? current(\array_slice($array, -1)) : null; |
| 50 |
} |
| 51 |
|
| 52 |
private const RTL_SCRIPTS = [ |
| 53 |
'Adlm' => true, 'Arab' => true, 'Armi' => true, 'Hebr' => true, |
| 54 |
'Mand' => true, 'Mani' => true, 'Mend' => true, 'Nkoo' => true, |
| 55 |
'Orkh' => true, 'Phnx' => true, 'Rohg' => true, 'Samr' => true, |
| 56 |
'Syrc' => true, 'Thaa' => true, 'Yezi' => true, |
| 57 |
]; |
| 58 |
|
| 59 |
private const LANG_TO_SCRIPT = [ |
| 60 |
'ar' => 'Arab', |
| 61 |
'ckb' => 'Arab', |
| 62 |
'dv' => 'Thaa', |
| 63 |
'fa' => 'Arab', |
| 64 |
'he' => 'Hebr', |
| 65 |
'ku' => 'Arab', |
| 66 |
'nqo' => 'Nkoo', |
| 67 |
'ps' => 'Arab', |
| 68 |
'sd' => 'Arab', |
| 69 |
'ug' => 'Arab', |
| 70 |
'ur' => 'Arab', |
| 71 |
'yi' => 'Hebr', |
| 72 |
]; |
| 73 |
|
| 74 |
public static function locale_is_right_to_left(string $locale): bool |
| 75 |
{ |
| 76 |
if ('' === $locale) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
$parts = preg_split('/[_-]/', $locale); |
| 81 |
$language = strtolower($parts[0]); |
| 82 |
|
| 83 |
foreach ($parts as $part) { |
| 84 |
if (4 === \strlen($part) && ctype_alpha($part)) { |
| 85 |
return isset(self::RTL_SCRIPTS[ucfirst(strtolower($part))]); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return isset(self::LANG_TO_SCRIPT[$language]) && isset(self::RTL_SCRIPTS[self::LANG_TO_SCRIPT[$language]]); |
| 90 |
} |
| 91 |
|
| 92 |
public static function grapheme_levenshtein(string $s1, string $s2, int $insertion_cost = 1, int $replacement_cost = 1, int $deletion_cost = 1) |
| 93 |
{ |
| 94 |
if (!preg_match('//u', $s1) || !preg_match('//u', $s2)) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
if (0 > $insertion_cost || 0 > $replacement_cost || 0 > $deletion_cost) { |
| 99 |
throw new \ValueError('grapheme_levenshtein(): Argument #3 ($insertion_cost), #4 ($replacement_cost), and #5 ($deletion_cost) must be greater than or equal to 0'); |
| 100 |
} |
| 101 |
|
| 102 |
$regex = ((float) \PCRE_VERSION < 10 ? (float) \PCRE_VERSION >= 8.32 : (float) \PCRE_VERSION >= 10.39) |
| 103 |
? '\X' |
| 104 |
: '(?:\r\n|(?:[ -~\x{200C}\x{200D}]|[ᆨ-ᇹ]+|[ᄀ-ᅟ]*(?:[가개갸걔거게겨계고과괘괴교구궈궤귀규그긔기까깨꺄꺠꺼께껴꼐꼬꽈꽤꾀꾜꾸꿔꿰뀌뀨끄끠끼나내냐냬너네녀녜노놔놰뇌뇨누눠눼뉘뉴느늬니다대댜댸더데뎌뎨도돠돼되됴두둬뒈뒤듀드듸디따때땨떄떠떼뗘뗴또똬뙈뙤뚀뚜뚸뛔뛰뜌뜨띄띠라래랴럐러레려례로롸뢔뢰료루뤄뤠뤼류르릐리마매먀먜머메며몌모뫄뫠뫼묘무뭐뭬뮈뮤므믜미바배뱌뱨버베벼볘보봐봬뵈뵤부붜붸뷔뷰브븨비빠빼뺘뺴뻐뻬뼈뼤뽀뽜뽸뾔뾰뿌뿨쀄쀠쀼쁘쁴삐사새샤섀서세셔셰소솨쇄쇠쇼수숴쉐쉬슈스싀시싸쌔쌰썌써쎄쎠쎼쏘쏴쐐쐬쑈쑤쒀쒜쒸쓔쓰씌씨아애야얘어에여예오와왜외요우워웨위유으의이자재쟈쟤저제져졔조좌좨죄죠주줘줴쥐쥬즈즤지짜째쨔쨰쩌쩨쪄쪠쪼쫘쫴쬐쬬쭈쭤쮀쮜쮸쯔쯰찌차채챠챼처체쳐쳬초촤쵀최쵸추춰췌취츄츠츼치카캐캬컈커케켜켸코콰쾌쾨쿄쿠쿼퀘퀴큐크킈키타태탸턔터테텨톄토톼퇘퇴툐투퉈퉤튀튜트틔티파패퍄퍠퍼페펴폐포퐈퐤푀표푸풔풰퓌퓨프픠피하해햐햬허헤혀혜호화홰회효후훠훼휘휴흐희히]?[ᅠ-ᆢ]+|[가-힣])[ᆨ-ᇹ]*|[ᄀ-ᅟ]+|[^\p{Cc}\p{Cf}\p{Zl}\p{Zp}])[\p{Mn}\p{Me}\x{09BE}\x{09D7}\x{0B3E}\x{0B57}\x{0BBE}\x{0BD7}\x{0CC2}\x{0CD5}\x{0CD6}\x{0D3E}\x{0D57}\x{0DCF}\x{0DDF}\x{200C}\x{200D}\x{1D165}\x{1D16E}-\x{1D172}]*|[\p{Cc}\p{Cf}\p{Zl}\p{Zp}])'; |
| 105 |
|
| 106 |
preg_match_all('/'.$regex.'/u', $s1, $s1); |
| 107 |
preg_match_all('/'.$regex.'/u', $s2, $s2); |
| 108 |
|
| 109 |
$s1 = $s1[0]; |
| 110 |
$s2 = $s2[0]; |
| 111 |
$l1 = \count($s1); |
| 112 |
$l2 = \count($s2); |
| 113 |
|
| 114 |
if (0 === $l1) { |
| 115 |
return $l2 * $insertion_cost; |
| 116 |
} |
| 117 |
if (0 === $l2) { |
| 118 |
return $l1 * $deletion_cost; |
| 119 |
} |
| 120 |
|
| 121 |
$dp = array_fill(0, $l1 + 1, array_fill(0, $l2 + 1, 0)); |
| 122 |
|
| 123 |
for ($i = 1; $i <= $l1; ++$i) { |
| 124 |
$dp[$i][0] = $dp[$i - 1][0] + $deletion_cost; |
| 125 |
} |
| 126 |
for ($j = 1; $j <= $l2; ++$j) { |
| 127 |
$dp[0][$j] = $dp[0][$j - 1] + $insertion_cost; |
| 128 |
} |
| 129 |
|
| 130 |
for ($i = 1; $i <= $l1; ++$i) { |
| 131 |
for ($j = 1; $j <= $l2; ++$j) { |
| 132 |
$cost = ($s1[$i - 1] === $s2[$j - 1]) ? 0 : $replacement_cost; |
| 133 |
$dp[$i][$j] = min($dp[$i - 1][$j] + $deletion_cost, $dp[$i][$j - 1] + $insertion_cost, $dp[$i - 1][$j - 1] + $cost); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return $dp[$l1][$l2]; |
| 138 |
} |
| 139 |
} |