Str.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. <?php
  2. namespace Illuminate\Support;
  3. use Ramsey\Uuid\Uuid;
  4. use Ramsey\Uuid\UuidFactory;
  5. use Illuminate\Support\Traits\Macroable;
  6. use Ramsey\Uuid\Generator\CombGenerator;
  7. use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
  8. class Str
  9. {
  10. use Macroable;
  11. /**
  12. * The cache of snake-cased words.
  13. *
  14. * @var array
  15. */
  16. protected static $snakeCache = [];
  17. /**
  18. * The cache of camel-cased words.
  19. *
  20. * @var array
  21. */
  22. protected static $camelCache = [];
  23. /**
  24. * The cache of studly-cased words.
  25. *
  26. * @var array
  27. */
  28. protected static $studlyCache = [];
  29. /**
  30. * Return the remainder of a string after a given value.
  31. *
  32. * @param string $subject
  33. * @param string $search
  34. * @return string
  35. */
  36. public static function after($subject, $search)
  37. {
  38. return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
  39. }
  40. /**
  41. * Transliterate a UTF-8 value to ASCII.
  42. *
  43. * @param string $value
  44. * @param string $language
  45. * @return string
  46. */
  47. public static function ascii($value, $language = 'en')
  48. {
  49. $languageSpecific = static::languageSpecificCharsArray($language);
  50. if (! is_null($languageSpecific)) {
  51. $value = str_replace($languageSpecific[0], $languageSpecific[1], $value);
  52. }
  53. foreach (static::charsArray() as $key => $val) {
  54. $value = str_replace($val, $key, $value);
  55. }
  56. return preg_replace('/[^\x20-\x7E]/u', '', $value);
  57. }
  58. /**
  59. * Get the portion of a string before a given value.
  60. *
  61. * @param string $subject
  62. * @param string $search
  63. * @return string
  64. */
  65. public static function before($subject, $search)
  66. {
  67. return $search === '' ? $subject : explode($search, $subject)[0];
  68. }
  69. /**
  70. * Convert a value to camel case.
  71. *
  72. * @param string $value
  73. * @return string
  74. */
  75. public static function camel($value)
  76. {
  77. if (isset(static::$camelCache[$value])) {
  78. return static::$camelCache[$value];
  79. }
  80. return static::$camelCache[$value] = lcfirst(static::studly($value));
  81. }
  82. /**
  83. * Determine if a given string contains a given substring.
  84. *
  85. * @param string $haystack
  86. * @param string|array $needles
  87. * @return bool
  88. */
  89. public static function contains($haystack, $needles)
  90. {
  91. foreach ((array) $needles as $needle) {
  92. if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. /**
  99. * Determine if a given string ends with a given substring.
  100. *
  101. * @param string $haystack
  102. * @param string|array $needles
  103. * @return bool
  104. */
  105. public static function endsWith($haystack, $needles)
  106. {
  107. foreach ((array) $needles as $needle) {
  108. if (substr($haystack, -strlen($needle)) === (string) $needle) {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. /**
  115. * Cap a string with a single instance of a given value.
  116. *
  117. * @param string $value
  118. * @param string $cap
  119. * @return string
  120. */
  121. public static function finish($value, $cap)
  122. {
  123. $quoted = preg_quote($cap, '/');
  124. return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap;
  125. }
  126. /**
  127. * Determine if a given string matches a given pattern.
  128. *
  129. * @param string|array $pattern
  130. * @param string $value
  131. * @return bool
  132. */
  133. public static function is($pattern, $value)
  134. {
  135. $patterns = Arr::wrap($pattern);
  136. if (empty($patterns)) {
  137. return false;
  138. }
  139. foreach ($patterns as $pattern) {
  140. // If the given value is an exact match we can of course return true right
  141. // from the beginning. Otherwise, we will translate asterisks and do an
  142. // actual pattern match against the two strings to see if they match.
  143. if ($pattern == $value) {
  144. return true;
  145. }
  146. $pattern = preg_quote($pattern, '#');
  147. // Asterisks are translated into zero-or-more regular expression wildcards
  148. // to make it convenient to check if the strings starts with the given
  149. // pattern such as "library/*", making any string check convenient.
  150. $pattern = str_replace('\*', '.*', $pattern);
  151. if (preg_match('#^'.$pattern.'\z#u', $value) === 1) {
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. /**
  158. * Convert a string to kebab case.
  159. *
  160. * @param string $value
  161. * @return string
  162. */
  163. public static function kebab($value)
  164. {
  165. return static::snake($value, '-');
  166. }
  167. /**
  168. * Return the length of the given string.
  169. *
  170. * @param string $value
  171. * @param string $encoding
  172. * @return int
  173. */
  174. public static function length($value, $encoding = null)
  175. {
  176. if ($encoding) {
  177. return mb_strlen($value, $encoding);
  178. }
  179. return mb_strlen($value);
  180. }
  181. /**
  182. * Limit the number of characters in a string.
  183. *
  184. * @param string $value
  185. * @param int $limit
  186. * @param string $end
  187. * @return string
  188. */
  189. public static function limit($value, $limit = 100, $end = '...')
  190. {
  191. if (mb_strwidth($value, 'UTF-8') <= $limit) {
  192. return $value;
  193. }
  194. return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
  195. }
  196. /**
  197. * Convert the given string to lower-case.
  198. *
  199. * @param string $value
  200. * @return string
  201. */
  202. public static function lower($value)
  203. {
  204. return mb_strtolower($value, 'UTF-8');
  205. }
  206. /**
  207. * Limit the number of words in a string.
  208. *
  209. * @param string $value
  210. * @param int $words
  211. * @param string $end
  212. * @return string
  213. */
  214. public static function words($value, $words = 100, $end = '...')
  215. {
  216. preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
  217. if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) {
  218. return $value;
  219. }
  220. return rtrim($matches[0]).$end;
  221. }
  222. /**
  223. * Parse a Class@method style callback into class and method.
  224. *
  225. * @param string $callback
  226. * @param string|null $default
  227. * @return array
  228. */
  229. public static function parseCallback($callback, $default = null)
  230. {
  231. return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default];
  232. }
  233. /**
  234. * Get the plural form of an English word.
  235. *
  236. * @param string $value
  237. * @param int $count
  238. * @return string
  239. */
  240. public static function plural($value, $count = 2)
  241. {
  242. return Pluralizer::plural($value, $count);
  243. }
  244. /**
  245. * Generate a more truly "random" alpha-numeric string.
  246. *
  247. * @param int $length
  248. * @return string
  249. */
  250. public static function random($length = 16)
  251. {
  252. $string = '';
  253. while (($len = strlen($string)) < $length) {
  254. $size = $length - $len;
  255. $bytes = random_bytes($size);
  256. $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
  257. }
  258. return $string;
  259. }
  260. /**
  261. * Replace a given value in the string sequentially with an array.
  262. *
  263. * @param string $search
  264. * @param array $replace
  265. * @param string $subject
  266. * @return string
  267. */
  268. public static function replaceArray($search, array $replace, $subject)
  269. {
  270. foreach ($replace as $value) {
  271. $subject = static::replaceFirst($search, $value, $subject);
  272. }
  273. return $subject;
  274. }
  275. /**
  276. * Replace the first occurrence of a given value in the string.
  277. *
  278. * @param string $search
  279. * @param string $replace
  280. * @param string $subject
  281. * @return string
  282. */
  283. public static function replaceFirst($search, $replace, $subject)
  284. {
  285. if ($search == '') {
  286. return $subject;
  287. }
  288. $position = strpos($subject, $search);
  289. if ($position !== false) {
  290. return substr_replace($subject, $replace, $position, strlen($search));
  291. }
  292. return $subject;
  293. }
  294. /**
  295. * Replace the last occurrence of a given value in the string.
  296. *
  297. * @param string $search
  298. * @param string $replace
  299. * @param string $subject
  300. * @return string
  301. */
  302. public static function replaceLast($search, $replace, $subject)
  303. {
  304. $position = strrpos($subject, $search);
  305. if ($position !== false) {
  306. return substr_replace($subject, $replace, $position, strlen($search));
  307. }
  308. return $subject;
  309. }
  310. /**
  311. * Begin a string with a single instance of a given value.
  312. *
  313. * @param string $value
  314. * @param string $prefix
  315. * @return string
  316. */
  317. public static function start($value, $prefix)
  318. {
  319. $quoted = preg_quote($prefix, '/');
  320. return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value);
  321. }
  322. /**
  323. * Convert the given string to upper-case.
  324. *
  325. * @param string $value
  326. * @return string
  327. */
  328. public static function upper($value)
  329. {
  330. return mb_strtoupper($value, 'UTF-8');
  331. }
  332. /**
  333. * Convert the given string to title case.
  334. *
  335. * @param string $value
  336. * @return string
  337. */
  338. public static function title($value)
  339. {
  340. return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
  341. }
  342. /**
  343. * Get the singular form of an English word.
  344. *
  345. * @param string $value
  346. * @return string
  347. */
  348. public static function singular($value)
  349. {
  350. return Pluralizer::singular($value);
  351. }
  352. /**
  353. * Generate a URL friendly "slug" from a given string.
  354. *
  355. * @param string $title
  356. * @param string $separator
  357. * @param string $language
  358. * @return string
  359. */
  360. public static function slug($title, $separator = '-', $language = 'en')
  361. {
  362. $title = static::ascii($title, $language);
  363. // Convert all dashes/underscores into separator
  364. $flip = $separator == '-' ? '_' : '-';
  365. $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
  366. // Replace @ with the word 'at'
  367. $title = str_replace('@', $separator.'at'.$separator, $title);
  368. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  369. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
  370. // Replace all separator characters and whitespace by a single separator
  371. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  372. return trim($title, $separator);
  373. }
  374. /**
  375. * Convert a string to snake case.
  376. *
  377. * @param string $value
  378. * @param string $delimiter
  379. * @return string
  380. */
  381. public static function snake($value, $delimiter = '_')
  382. {
  383. $key = $value;
  384. if (isset(static::$snakeCache[$key][$delimiter])) {
  385. return static::$snakeCache[$key][$delimiter];
  386. }
  387. if (! ctype_lower($value)) {
  388. $value = preg_replace('/\s+/u', '', ucwords($value));
  389. $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
  390. }
  391. return static::$snakeCache[$key][$delimiter] = $value;
  392. }
  393. /**
  394. * Determine if a given string starts with a given substring.
  395. *
  396. * @param string $haystack
  397. * @param string|array $needles
  398. * @return bool
  399. */
  400. public static function startsWith($haystack, $needles)
  401. {
  402. foreach ((array) $needles as $needle) {
  403. if ($needle !== '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
  404. return true;
  405. }
  406. }
  407. return false;
  408. }
  409. /**
  410. * Convert a value to studly caps case.
  411. *
  412. * @param string $value
  413. * @return string
  414. */
  415. public static function studly($value)
  416. {
  417. $key = $value;
  418. if (isset(static::$studlyCache[$key])) {
  419. return static::$studlyCache[$key];
  420. }
  421. $value = ucwords(str_replace(['-', '_'], ' ', $value));
  422. return static::$studlyCache[$key] = str_replace(' ', '', $value);
  423. }
  424. /**
  425. * Returns the portion of string specified by the start and length parameters.
  426. *
  427. * @param string $string
  428. * @param int $start
  429. * @param int|null $length
  430. * @return string
  431. */
  432. public static function substr($string, $start, $length = null)
  433. {
  434. return mb_substr($string, $start, $length, 'UTF-8');
  435. }
  436. /**
  437. * Make a string's first character uppercase.
  438. *
  439. * @param string $string
  440. * @return string
  441. */
  442. public static function ucfirst($string)
  443. {
  444. return static::upper(static::substr($string, 0, 1)).static::substr($string, 1);
  445. }
  446. /**
  447. * Generate a UUID (version 4).
  448. *
  449. * @return \Ramsey\Uuid\UuidInterface
  450. */
  451. public static function uuid()
  452. {
  453. return Uuid::uuid4();
  454. }
  455. /**
  456. * Generate a time-ordered UUID (version 4).
  457. *
  458. * @return \Ramsey\Uuid\UuidInterface
  459. */
  460. public static function orderedUuid()
  461. {
  462. $factory = new UuidFactory;
  463. $factory->setRandomGenerator(new CombGenerator(
  464. $factory->getRandomGenerator(),
  465. $factory->getNumberConverter()
  466. ));
  467. $factory->setCodec(new TimestampFirstCombCodec(
  468. $factory->getUuidBuilder()
  469. ));
  470. return $factory->uuid4();
  471. }
  472. /**
  473. * Returns the replacements for the ascii method.
  474. *
  475. * Note: Adapted from Stringy\Stringy.
  476. *
  477. * @see https://github.com/danielstjules/Stringy/blob/3.1.0/LICENSE.txt
  478. *
  479. * @return array
  480. */
  481. protected static function charsArray()
  482. {
  483. static $charsArray;
  484. if (isset($charsArray)) {
  485. return $charsArray;
  486. }
  487. return $charsArray = [
  488. '0' => ['°', '₀', '۰', '0'],
  489. '1' => ['¹', '₁', '۱', '1'],
  490. '2' => ['²', '₂', '۲', '2'],
  491. '3' => ['³', '₃', '۳', '3'],
  492. '4' => ['⁴', '₄', '۴', '٤', '4'],
  493. '5' => ['⁵', '₅', '۵', '٥', '5'],
  494. '6' => ['⁶', '₆', '۶', '٦', '6'],
  495. '7' => ['⁷', '₇', '۷', '7'],
  496. '8' => ['⁸', '₈', '۸', '8'],
  497. '9' => ['⁹', '₉', '۹', '9'],
  498. 'a' => ['à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', 'အ', 'ာ', 'ါ', 'ǻ', 'ǎ', 'ª', 'ა', 'अ', 'ا', 'a', 'ä'],
  499. 'b' => ['б', 'β', 'ب', 'ဗ', 'ბ', 'b'],
  500. 'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ', 'c'],
  501. 'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ', 'd'],
  502. 'e' => ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э', 'є', 'ə', 'ဧ', 'ေ', 'ဲ', 'ე', 'ए', 'إ', 'ئ', 'e'],
  503. 'f' => ['ф', 'φ', 'ف', 'ƒ', 'ფ', 'f'],
  504. 'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ', 'g'],
  505. 'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ', 'h'],
  506. 'i' => ['í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į', 'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і', 'ї', 'и', 'ဣ', 'ိ', 'ီ', 'ည်', 'ǐ', 'ი', 'इ', 'ی', 'i'],
  507. 'j' => ['ĵ', 'ј', 'Ј', 'ჯ', 'ج', 'j'],
  508. 'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ', 'ک', 'k'],
  509. 'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ', 'l'],
  510. 'm' => ['м', 'μ', 'م', 'မ', 'მ', 'm'],
  511. 'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န', 'ნ', 'n'],
  512. 'o' => ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό', 'о', 'و', 'θ', 'ို', 'ǒ', 'ǿ', 'º', 'ო', 'ओ', 'o', 'ö'],
  513. 'p' => ['п', 'π', 'ပ', 'პ', 'پ', 'p'],
  514. 'q' => ['ყ', 'q'],
  515. 'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ', 'r'],
  516. 's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ', 'ſ', 'ს', 's'],
  517. 't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', 'ဋ', 'တ', 'ŧ', 'თ', 'ტ', 't'],
  518. 'u' => ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у', 'ဉ', 'ု', 'ူ', 'ǔ', 'ǖ', 'ǘ', 'ǚ', 'ǜ', 'უ', 'उ', 'u', 'ў', 'ü'],
  519. 'v' => ['в', 'ვ', 'ϐ', 'v'],
  520. 'w' => ['ŵ', 'ω', 'ώ', 'ဝ', 'ွ', 'w'],
  521. 'x' => ['χ', 'ξ', 'x'],
  522. 'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي', 'ယ', 'y'],
  523. 'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ', 'z'],
  524. 'aa' => ['ع', 'आ', 'آ'],
  525. 'ae' => ['æ', 'ǽ'],
  526. 'ai' => ['ऐ'],
  527. 'ch' => ['ч', 'ჩ', 'ჭ', 'چ'],
  528. 'dj' => ['ђ', 'đ'],
  529. 'dz' => ['џ', 'ძ'],
  530. 'ei' => ['ऍ'],
  531. 'gh' => ['غ', 'ღ'],
  532. 'ii' => ['ई'],
  533. 'ij' => ['ij'],
  534. 'kh' => ['х', 'خ', 'ხ'],
  535. 'lj' => ['љ'],
  536. 'nj' => ['њ'],
  537. 'oe' => ['ö', 'œ', 'ؤ'],
  538. 'oi' => ['ऑ'],
  539. 'oii' => ['ऒ'],
  540. 'ps' => ['ψ'],
  541. 'sh' => ['ш', 'შ', 'ش'],
  542. 'shch' => ['щ'],
  543. 'ss' => ['ß'],
  544. 'sx' => ['ŝ'],
  545. 'th' => ['þ', 'ϑ', 'ث', 'ذ', 'ظ'],
  546. 'ts' => ['ц', 'ც', 'წ'],
  547. 'ue' => ['ü'],
  548. 'uu' => ['ऊ'],
  549. 'ya' => ['я'],
  550. 'yu' => ['ю'],
  551. 'zh' => ['ж', 'ჟ', 'ژ'],
  552. '(c)' => ['©'],
  553. 'A' => ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Å', 'Ā', 'Ą', 'Α', 'Ά', 'Ἀ', 'Ἁ', 'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', '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
  585. 'Ij' => ['IJ'],
  586. 'Jx' => ['Ĵ'],
  587. 'Kh' => ['Х'],
  588. 'Lj' => ['Љ'],
  589. 'Nj' => ['Њ'],
  590. 'Oe' => ['Œ'],
  591. 'Ps' => ['Ψ'],
  592. 'Sh' => ['Ш'],
  593. 'Shch' => ['Щ'],
  594. 'Ss' => ['ẞ'],
  595. 'Th' => ['Þ'],
  596. 'Ts' => ['Ц'],
  597. 'Ya' => ['Я'],
  598. 'Yu' => ['Ю'],
  599. 'Zh' => ['Ж'],
  600. ' ' => ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x82", "\xE2\x80\x83", "\xE2\x80\x84", "\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87", "\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A", "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80", "\xEF\xBE\xA0"],
  601. ];
  602. }
  603. /**
  604. * Returns the language specific replacements for the ascii method.
  605. *
  606. * Note: Adapted from Stringy\Stringy.
  607. *
  608. * @see https://github.com/danielstjules/Stringy/blob/3.1.0/LICENSE.txt
  609. *
  610. * @param string $language
  611. * @return array|null
  612. */
  613. protected static function languageSpecificCharsArray($language)
  614. {
  615. static $languageSpecific;
  616. if (! isset($languageSpecific)) {
  617. $languageSpecific = [
  618. 'bg' => [
  619. ['х', 'Х', 'щ', 'Щ', 'ъ', 'Ъ', 'ь', 'Ь'],
  620. ['h', 'H', 'sht', 'SHT', 'a', 'А', 'y', 'Y'],
  621. ],
  622. 'de' => [
  623. ['ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü'],
  624. ['ae', 'oe', 'ue', 'AE', 'OE', 'UE'],
  625. ],
  626. ];
  627. }
  628. return $languageSpecific[$language] ?? null;
  629. }
  630. }