8000 feature #36456 [String] Add locale-sensitive map for slugging symbols… · symfony/symfony@260dea0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 260dea0

Browse files
committed
feature #36456 [String] Add locale-sensitive map for slugging symbols (lmasforne)
This PR was merged into the 5.1-dev branch. Discussion ---------- [String] Add locale-sensitive map for slugging symbols | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #36383 | License | MIT By default chars '@' and '&' are respectively replaced by 'at' and 'and' (so limited by enlgish language). I had an $options arguments to 'slug' method to replace chars with your own logic. Commits ------- 1331584 [String] Add locale-sensitive map for slugging symbols
2 parents ac3bd14 + 1331584 commit 260dea0

File tree

8000

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/Symfony/Component/String/Slugger/AsciiSlugger.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface
5151
];
5252

5353
private $defaultLocale;
54+
private $symbolsMap = [
55+
'en' => ['@' => 'at', '&' => 'and'],
56+
];
5457

5558
/**
5659
* Cache of transliterators per locale.
@@ -59,9 +62,10 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface
5962
*/
6063
private $transliterators = [];
6164

62-
public function __construct(string $defaultLocale = null)
65+
public function __construct(string $defaultLocale = null, array $symbolsMap = null)
6366
{
6467
$this->defaultLocale = $defaultLocale;
68+
$this->symbolsMap = $symbolsMap ?? $this->symbolsMap;
6569
}
6670

6771
/**
@@ -95,10 +99,15 @@ public function slug(string $string, string $separator = '-', string $locale = n
9599
$transliterator = (array) $this->createTransliterator($locale);
96100
}
97101

98-
return (new UnicodeString($string))
99-
->ascii($transliterator)
100-
->replace('@', $separator.'at'.$separator)
101-
->replace('&', $separator.'and'.$separator)
102+
$unicodeString = (new UnicodeString($string))->ascii($transliterator);
103+
104+
if (isset($this->symbolsMap[$locale])) {
105+
foreach ($this->symbolsMap[$locale] as $char => $replace) {
106+
$unicodeString = $unicodeString->replace($char, ' '.$replace.' ');
107+
}
108+
}
109+
110+
return $unicodeString
102111
->replaceMatches('/[^A-Za-z0-9]++/', $separator)
103112
->trim($separator)
104113
;

src/Symfony/Component/String/Tests/SluggerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,19 @@ public function testSeparatorWithoutLocale()
4949
$this->assertSame('hello-world', (string) $slugger->slug('hello world'));
5050
$this->assertSame('hello_world', (string) $slugger->slug('hello world', '_'));
5151
}
52+
53+
public function testSlugCharReplacementLocaleConstruct()
54+
{
55+
$slugger = new AsciiSlugger('fr', ['fr' => ['&' => 'et', '@' => 'chez']]);
56+
$slug = (string) $slugger->slug('toi & moi avec cette adresse slug@test.fr', '_');
57+
58+
$this->assertSame('toi_et_moi_avec_cette_adresse_slug_chez_test_fr', $slug);
59+
}
60+
61+
public function testSlugCharReplacementLocaleMethod()
62+
{
63+
$slugger = new AsciiSlugger(null, ['es' => ['&' => 'y', '@' => 'en senal']]);
64+
$slug = (string) $slugger->slug('yo & tu a esta dirección slug@test.es', '_', 'es');
65+
$this->assertSame('yo_y_tu_a_esta_direccion_slug_en_senal_test_es', $slug);
66+
}
5267
}

0 commit comments

Comments
 (0)
0