PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.5
WindPress – Tailwind CSS integration for WordPress v3.0.5
3.2.90 3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 All 144 releases
windpress / vendor / symfony / string / Slugger / AsciiSlugger.php

AsciiSlugger.php in WindPress – Tailwind CSS integration for WordPress 3.0.5, at vendor/symfony/string/Slugger/AsciiSlugger.php

129 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 namespace WindPressPackages\Symfony\Component\String\Slugger;
12
13 use WindPressPackages\Symfony\Component\String\AbstractUnicodeString;
14 use WindPressPackages\Symfony\Component\String\UnicodeString;
15 use WindPressPackages\Symfony\Contracts\Translation\LocaleAwareInterface;
16 if (!\interface_exists(LocaleAwareInterface::class)) {
17 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".');
18 }
19 /**
20 * @author Titouan Galopin <galopintitouan@gmail.com>
21 */
22 class AsciiSlugger implements SluggerInterface, LocaleAwareInterface
23 {
24 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'];
25 private $defaultLocale;
26 private $symbolsMap = ['en' => ['@' => 'at', '&' => 'and']];
27 /**
28 * Cache of transliterators per locale.
29 *
30 * @var \Transliterator[]
31 */
32 private $transliterators = [];
33 /**
34 * @param array|\Closure|null $symbolsMap
35 */
36 public function __construct(?string $defaultLocale = null, $symbolsMap = null)
37 {
38 if (null !== $symbolsMap && !\is_array($symbolsMap) && !$symbolsMap instanceof \Closure) {
39 throw new \TypeError(\sprintf('Argument 2 passed to "%s()" must be array, Closure or null, "%s" given.', __METHOD__, \gettype($symbolsMap)));
40 }
41 $this->defaultLocale = $defaultLocale;
42 $this->symbolsMap = $symbolsMap ?? $this->symbolsMap;
43 }
44 /**
45 * {@inheritdoc}
46 */
47 public function setLocale($locale)
48 {
49 $this->defaultLocale = $locale;
50 }
51 /**
52 * {@inheritdoc}
53 */
54 public function getLocale()
55 {
56 return $this->defaultLocale;
57 }
58 /**
59 * {@inheritdoc}
60 */
61 public function slug(string $string, string $separator = '-', ?string $locale = null) : AbstractUnicodeString
62 {
63 $locale = $locale ?? $this->defaultLocale;
64 $transliterator = [];
65 if ($locale && ('de' === $locale || 0 === \strpos($locale, 'de_'))) {
66 // Use the shortcut for German in UnicodeString::ascii() if possible (faster and no requirement on intl)
67 $transliterator = ['de-ASCII'];
68 } elseif (\function_exists('transliterator_transliterate') && $locale) {
69 $transliterator = (array) $this->createTransliterator($locale);
70 }
71 if ($this->symbolsMap instanceof \Closure) {
72 // If the symbols map is passed as a closure, there is no need to fallback to the parent locale
73 // as the closure can just provide substitutions for all locales of interest.
74 $symbolsMap = $this->symbolsMap;
75 \array_unshift($transliterator, static function ($s) use($symbolsMap, $locale) {
76 return $symbolsMap($s, $locale);
77 });
78 }
79 $unicodeString = (new UnicodeString($string))->ascii($transliterator);
80 if (\is_array($this->symbolsMap)) {
81 $map = null;
82 if (isset($this->symbolsMap[$locale])) {
83 $map = $this->symbolsMap[$locale];
84 } else {
85 $parent = self::getParentLocale($locale);
86 if ($parent && isset($this->symbolsMap[$parent])) {
87 $map = $this->symbolsMap[$parent];
88 }
89 }
90 if ($map) {
91 foreach ($map as $char => $replace) {
92 $unicodeString = $unicodeString->replace($char, ' ' . $replace . ' ');
93 }
94 }
95 }
96 return $unicodeString->replaceMatches('/[^A-Za-z0-9]++/', $separator)->trim($separator);
97 }
98 private function createTransliterator(string $locale) : ?\Transliterator
99 {
100 if (\array_key_exists($locale, $this->transliterators)) {
101 return $this->transliterators[$locale];
102 }
103 // Exact locale supported, cache and return
104 if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$locale] ?? null) {
105 return $this->transliterators[$locale] = \Transliterator::create($id . '/BGN') ?? \Transliterator::create($id);
106 }
107 // Locale not supported and no parent, fallback to any-latin
108 if (!($parent = self::getParentLocale($locale))) {
109 return $this->transliterators[$locale] = null;
110 }
111 // Try to use the parent locale (ie. try "de" for "de_AT") and cache both locales
112 if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$parent] ?? null) {
113 $transliterator = \Transliterator::create($id . '/BGN') ?? \Transliterator::create($id);
114 }
115 return $this->transliterators[$locale] = $this->transliterators[$parent] = $transliterator ?? null;
116 }
117 private static function getParentLocale(?string $locale) : ?string
118 {
119 if (!$locale) {
120 return null;
121 }
122 if (\false === ($str = \strrchr($locale, '_'))) {
123 // no parent locale
124 return null;
125 }
126 return \substr($locale, 0, -\strlen($str));
127 }
128 }
129