AsciiSlugger.php
100 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\String\Slugger; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\String\AbstractUnicodeString; |
| 5 | use MailPoetVendor\Symfony\Component\String\UnicodeString; |
| 6 | use MailPoetVendor\Symfony\Contracts\Translation\LocaleAwareInterface; |
| 7 | if (!\interface_exists(LocaleAwareInterface::class)) { |
| 8 | throw new \LogicException('You cannot use the "Symfony\\Component\\String\\Slugger\\AsciiSlugger" as the "symfony/translation-contracts" package is not installed. Try running "composer require symfony/translation-contracts".'); |
| 9 | } |
| 10 | class AsciiSlugger implements SluggerInterface, LocaleAwareInterface |
| 11 | { |
| 12 | private const LOCALE_TO_TRANSLITERATOR_ID = ['am' => 'Amharic-Latin', 'ar' => 'Arabic-Latin', 'az' => 'Azerbaijani-Latin', 'be' => 'Belarusian-Latin', 'bg' => 'Bulgarian-Latin', 'bn' => 'Bengali-Latin', 'de' => 'de-ASCII', 'el' => 'Greek-Latin', 'fa' => 'Persian-Latin', 'he' => 'Hebrew-Latin', 'hy' => 'Armenian-Latin', 'ka' => 'Georgian-Latin', 'kk' => 'Kazakh-Latin', 'ky' => 'Kirghiz-Latin', 'ko' => 'Korean-Latin', 'mk' => 'Macedonian-Latin', 'mn' => 'Mongolian-Latin', 'or' => 'Oriya-Latin', 'ps' => 'Pashto-Latin', 'ru' => 'Russian-Latin', 'sr' => 'Serbian-Latin', 'sr_Cyrl' => 'Serbian-Latin', 'th' => 'Thai-Latin', 'tk' => 'Turkmen-Latin', 'uk' => 'Ukrainian-Latin', 'uz' => 'Uzbek-Latin', 'zh' => 'Han-Latin']; |
| 13 | private $defaultLocale; |
| 14 | private $symbolsMap = ['en' => ['@' => 'at', '&' => 'and']]; |
| 15 | private $transliterators = []; |
| 16 | public function __construct(?string $defaultLocale = null, $symbolsMap = null) |
| 17 | { |
| 18 | if (null !== $symbolsMap && !\is_array($symbolsMap) && !$symbolsMap instanceof \Closure) { |
| 19 | throw new \TypeError(\sprintf('Argument 2 passed to "%s()" must be array, Closure or null, "%s" given.', __METHOD__, \gettype($symbolsMap))); |
| 20 | } |
| 21 | $this->defaultLocale = $defaultLocale; |
| 22 | $this->symbolsMap = $symbolsMap ?? $this->symbolsMap; |
| 23 | } |
| 24 | public function setLocale($locale) |
| 25 | { |
| 26 | $this->defaultLocale = $locale; |
| 27 | } |
| 28 | public function getLocale() |
| 29 | { |
| 30 | return $this->defaultLocale; |
| 31 | } |
| 32 | public function slug(string $string, string $separator = '-', ?string $locale = null) : AbstractUnicodeString |
| 33 | { |
| 34 | $locale = $locale ?? $this->defaultLocale; |
| 35 | $transliterator = []; |
| 36 | if ($locale && ('de' === $locale || 0 === \strpos($locale, 'de_'))) { |
| 37 | // Use the shortcut for German in UnicodeString::ascii() if possible (faster and no requirement on intl) |
| 38 | $transliterator = ['de-ASCII']; |
| 39 | } elseif (\function_exists('transliterator_transliterate') && $locale) { |
| 40 | $transliterator = (array) $this->createTransliterator($locale); |
| 41 | } |
| 42 | if ($this->symbolsMap instanceof \Closure) { |
| 43 | // If the symbols map is passed as a closure, there is no need to fallback to the parent locale |
| 44 | // as the closure can just provide substitutions for all locales of interest. |
| 45 | $symbolsMap = $this->symbolsMap; |
| 46 | \array_unshift($transliterator, static function ($s) use($symbolsMap, $locale) { |
| 47 | return $symbolsMap($s, $locale); |
| 48 | }); |
| 49 | } |
| 50 | $unicodeString = (new UnicodeString($string))->ascii($transliterator); |
| 51 | if (\is_array($this->symbolsMap)) { |
| 52 | $map = null; |
| 53 | if (isset($this->symbolsMap[$locale])) { |
| 54 | $map = $this->symbolsMap[$locale]; |
| 55 | } else { |
| 56 | $parent = self::getParentLocale($locale); |
| 57 | if ($parent && isset($this->symbolsMap[$parent])) { |
| 58 | $map = $this->symbolsMap[$parent]; |
| 59 | } |
| 60 | } |
| 61 | if ($map) { |
| 62 | foreach ($map as $char => $replace) { |
| 63 | $unicodeString = $unicodeString->replace($char, ' ' . $replace . ' '); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return $unicodeString->replaceMatches('/[^A-Za-z0-9]++/', $separator)->trim($separator); |
| 68 | } |
| 69 | private function createTransliterator(string $locale) : ?\Transliterator |
| 70 | { |
| 71 | if (\array_key_exists($locale, $this->transliterators)) { |
| 72 | return $this->transliterators[$locale]; |
| 73 | } |
| 74 | // Exact locale supported, cache and return |
| 75 | if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$locale] ?? null) { |
| 76 | return $this->transliterators[$locale] = \Transliterator::create($id . '/BGN') ?? \Transliterator::create($id); |
| 77 | } |
| 78 | // Locale not supported and no parent, fallback to any-latin |
| 79 | if (!($parent = self::getParentLocale($locale))) { |
| 80 | return $this->transliterators[$locale] = null; |
| 81 | } |
| 82 | // Try to use the parent locale (ie. try "de" for "de_AT") and cache both locales |
| 83 | if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$parent] ?? null) { |
| 84 | $transliterator = \Transliterator::create($id . '/BGN') ?? \Transliterator::create($id); |
| 85 | } |
| 86 | return $this->transliterators[$locale] = $this->transliterators[$parent] = $transliterator ?? null; |
| 87 | } |
| 88 | private static function getParentLocale(?string $locale) : ?string |
| 89 | { |
| 90 | if (!$locale) { |
| 91 | return null; |
| 92 | } |
| 93 | if (\false === ($str = \strrchr($locale, '_'))) { |
| 94 | // no parent locale |
| 95 | return null; |
| 96 | } |
| 97 | return \substr($locale, 0, -\strlen($str)); |
| 98 | } |
| 99 | } |
| 100 |