AsciiSlugger.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\String\Slugger;
  11. use Symfony\Component\String\AbstractUnicodeString;
  12. use Symfony\Component\String\UnicodeString;
  13. use Symfony\Contracts\Translation\LocaleAwareInterface;
  14. if (!interface_exists(LocaleAwareInterface::class)) {
  15. 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".');
  16. }
  17. /**
  18. * @author Titouan Galopin <galopintitouan@gmail.com>
  19. */
  20. class AsciiSlugger implements SluggerInterface, LocaleAwareInterface
  21. {
  22. private const LOCALE_TO_TRANSLITERATOR_ID = [
  23. 'am' => 'Amharic-Latin',
  24. 'ar' => 'Arabic-Latin',
  25. 'az' => 'Azerbaijani-Latin',
  26. 'be' => 'Belarusian-Latin',
  27. 'bg' => 'Bulgarian-Latin',
  28. 'bn' => 'Bengali-Latin',
  29. 'de' => 'de-ASCII',
  30. 'el' => 'Greek-Latin',
  31. 'fa' => 'Persian-Latin',
  32. 'he' => 'Hebrew-Latin',
  33. 'hy' => 'Armenian-Latin',
  34. 'ka' => 'Georgian-Latin',
  35. 'kk' => 'Kazakh-Latin',
  36. 'ky' => 'Kirghiz-Latin',
  37. 'ko' => 'Korean-Latin',
  38. 'mk' => 'Macedonian-Latin',
  39. 'mn' => 'Mongolian-Latin',
  40. 'or' => 'Oriya-Latin',
  41. 'ps' => 'Pashto-Latin',
  42. 'ru' => 'Russian-Latin',
  43. 'sr' => 'Serbian-Latin',
  44. 'sr_Cyrl' => 'Serbian-Latin',
  45. 'th' => 'Thai-Latin',
  46. 'tk' => 'Turkmen-Latin',
  47. 'uk' => 'Ukrainian-Latin',
  48. 'uz' => 'Uzbek-Latin',
  49. 'zh' => 'Han-Latin',
  50. ];
  51. private $defaultLocale;
  52. private $symbolsMap = [
  53. 'en' => ['@' => 'at', '&' => 'and'],
  54. ];
  55. /**
  56. * Cache of transliterators per locale.
  57. *
  58. * @var \Transliterator[]
  59. */
  60. private $transliterators = [];
  61. public function __construct(string $defaultLocale = null, array $symbolsMap = null)
  62. {
  63. $this->defaultLocale = $defaultLocale;
  64. $this->symbolsMap = $symbolsMap ?? $this->symbolsMap;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function setLocale($locale)
  70. {
  71. $this->defaultLocale = $locale;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getLocale()
  77. {
  78. return $this->defaultLocale;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function slug(string $string, string $separator = '-', string $locale = null): AbstractUnicodeString
  84. {
  85. $locale = $locale ?? $this->defaultLocale;
  86. $transliterator = [];
  87. if ('de' === $locale || 0 === strpos($locale, 'de_')) {
  88. // Use the shortcut for German in UnicodeString::ascii() if possible (faster and no requirement on intl)
  89. $transliterator = ['de-ASCII'];
  90. } elseif (\function_exists('transliterator_transliterate') && $locale) {
  91. $transliterator = (array) $this->createTransliterator($locale);
  92. }
  93. $unicodeString = (new UnicodeString($string))->ascii($transliterator);
  94. if (isset($this->symbolsMap[$locale])) {
  95. foreach ($this->symbolsMap[$locale] as $char => $replace) {
  96. $unicodeString = $unicodeString->replace($char, ' '.$replace.' ');
  97. }
  98. }
  99. return $unicodeString
  100. ->replaceMatches('/[^A-Za-z0-9]++/', $separator)
  101. ->trim($separator)
  102. ;
  103. }
  104. private function createTransliterator(string $locale): ?\Transliterator
  105. {
  106. if (\array_key_exists($locale, $this->transliterators)) {
  107. return $this->transliterators[$locale];
  108. }
  109. // Exact locale supported, cache and return
  110. if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$locale] ?? null) {
  111. return $this->transliterators[$locale] = \Transliterator::create($id.'/BGN') ?? \Transliterator::create($id);
  112. }
  113. // Locale not supported and no parent, fallback to any-latin
  114. if (false === $str = strrchr($locale, '_')) {
  115. return $this->transliterators[$locale] = null;
  116. }
  117. // Try to use the parent locale (ie. try "de" for "de_AT") and cache both locales
  118. $parent = substr($locale, 0, -\strlen($str));
  119. if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$parent] ?? null) {
  120. $transliterator = \Transliterator::create($id.'/BGN') ?? \Transliterator::create($id);
  121. }
  122. return $this->transliterators[$locale] = $this->transliterators[$parent] = $transliterator ?? null;
  123. }
  124. }