Skip to content

Commit

Permalink
feature #36456 [String] Add locale-sensitive map for slugging symbols…
Browse files Browse the repository at this point in the history
… (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
  • Loading branch information
fabpot committed Apr 24, 2020
2 parents ac3bd14 + 1331584 commit 260dea0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Symfony/Component/String/Slugger/AsciiSlugger.php
Expand Up @@ -51,6 +51,9 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface
];

private $defaultLocale;
private $symbolsMap = [
'en' => ['@' => 'at', '&' => 'and'],
];

/**
* Cache of transliterators per locale.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -95,10 +99,15 @@ public function slug(string $string, string $separator = '-', string $locale = n
$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)
;
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/String/Tests/SluggerTest.php
Expand Up @@ -49,4 +49,19 @@ public function testSeparatorWithoutLocale()
$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);
}
}

0 comments on commit 260dea0

Please sign in to comment.