From 1331584fa1cbd7e9f34be3c31209264e3276920c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Masforn=C3=A9?= Date: Wed, 15 Apr 2020 09:16:33 +0200 Subject: [PATCH] [String] Add locale-sensitive map for slugging symbols --- .../Component/String/Slugger/AsciiSlugger.php | 19 ++++++++++++++----- .../Component/String/Tests/SluggerTest.php | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/String/Slugger/AsciiSlugger.php b/src/Symfony/Component/String/Slugger/AsciiSlugger.php index 583cafdf98..0fb4c9b64e 100644 --- a/src/Symfony/Component/String/Slugger/AsciiSlugger.php +++ b/src/Symfony/Component/String/Slugger/AsciiSlugger.php @@ -51,6 +51,9 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface ]; private $defaultLocale; + private $symbolsMap = [ + 'en' => ['@' => 'at', '&' => 'and'], + ]; /** * Cache of transliterators per locale. @@ -59,9 +62,10 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface */ private $transliterators = []; - public function __construct(string $defaultLocale = null) + public function __construct(string $defaultLocale = null, array $symbolsMap = null) { $this->defaultLocale = $defaultLocale; + $this->symbolsMap = $symbolsMap ?? $this->symbolsMap; } /** @@ -95,10 +99,15 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface $transliterator = (array) $this->createTransliterator($locale); } - return (new UnicodeString($string)) - ->ascii($transliterator) - ->replace('@', $separator.'at'.$separator) - ->replace('&', $separator.'and'.$separator) + $unicodeString = (new UnicodeString($string))->ascii($transliterator); + + if (isset($this->symbolsMap[$locale])) { + foreach ($this->symbolsMap[$locale] as $char => $replace) { + $unicodeString = $unicodeString->replace($char, ' '.$replace.' '); + } + } + + return $unicodeString ->replaceMatches('/[^A-Za-z0-9]++/', $separator) ->trim($separator) ; diff --git a/src/Symfony/Component/String/Tests/SluggerTest.php b/src/Symfony/Component/String/Tests/SluggerTest.php index 0ef3de1cf9..1290759b68 100644 --- a/src/Symfony/Component/String/Tests/SluggerTest.php +++ b/src/Symfony/Component/String/Tests/SluggerTest.php @@ -49,4 +49,19 @@ class SluggerTest extends TestCase $this->assertSame('hello-world', (string) $slugger->slug('hello world')); $this->assertSame('hello_world', (string) $slugger->slug('hello world', '_')); } + + public function testSlugCharReplacementLocaleConstruct() + { + $slugger = new AsciiSlugger('fr', ['fr' => ['&' => 'et', '@' => 'chez']]); + $slug = (string) $slugger->slug('toi & moi avec cette adresse slug@test.fr', '_'); + + $this->assertSame('toi_et_moi_avec_cette_adresse_slug_chez_test_fr', $slug); + } + + public function testSlugCharReplacementLocaleMethod() + { + $slugger = new AsciiSlugger(null, ['es' => ['&' => 'y', '@' => 'en senal']]); + $slug = (string) $slugger->slug('yo & tu a esta dirección slug@test.es', '_', 'es'); + $this->assertSame('yo_y_tu_a_esta_direccion_slug_en_senal_test_es', $slug); + } }