AsciiSlugger.php 4.3 KB

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