8000 [String] allow translit rules to be given as closure by nicolas-grekas · Pull Request #38198 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[String] allow translit rules to be given as closure #38198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Symfony/Component/String/AbstractUnicodeString.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static function fromCodePoints(int ...$codes): self
*
* Install the intl extension for best results.
*
* @param string[]|\Transliterator[] $rules See "*-Latin" rules from Transliterator::listIDs()
* @param string[]|\Transliterator[]|\Closure[] $rules See "*-Latin" rules from Transliterator::listIDs()
*/
public function ascii(array $rules = []): self
{
Expand Down Expand Up @@ -107,6 +107,8 @@ public function ascii(array $rules = []): self

if ($rule instanceof \Transliterator) {
$s = $rule->transliterate($s);
} elseif ($rule instanceof \Closure) {
$s = $rule($s);
} elseif ($rule) {
if ('nfd' === $rule = strtolower($rule)) {
normalizer_is_normalized($s, self::NFD) ?: $s = normalizer_normalize($s, self::NFD);
Expand Down
18 changes: 16 additions & 2 deletions src/Symfony/Component/String/Slugger/AsciiSlugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface
*/
private $transliterators = [];

public function __construct(string $defaultLocale = null, array $symbolsMap = null)
/**
* @param array|\Closure|null $symbolsMap
*/
public function __construct(string $defaultLocale = null, $symbolsMap = null)
{
if (null !== $symbolsMap && !\is_array($symbolsMap) && !$symbolsMap instanceof \Closure) {
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be array, Closure or null, "%s" given.', __METHOD__, \gettype($symbolMap)));
}

$this->defaultLocale = $defaultLocale;
$this->symbolsMap = $symbolsMap ?? $this->symbolsMap;
}
Expand Down Expand Up @@ -103,9 +110,16 @@ public function slug(string $string, string $separator = '-', string $locale = n
$transliterator = (array) $this->createTransliterator($locale);
}

if ($this->symbolsMap instanceof \Closure) {
$symbolsMap = $this->symbolsMap;
array_unshift($transliterator, static function ($s) use ($symbolsMap, $locale) {
return $symbolsMap($s, $locale);
});
}

$unicodeString = (new UnicodeString($string))->ascii($transliterator);

if (isset($this->symbolsMap[$locale])) {
if (\is_array($this->symbolsMap) && isset($this->symbolsMap[$locale])) {
foreach ($this->symbolsMap[$locale] as $char => $replace) {
$unicodeString = $unicodeString->replace($char, ' '.$replace.' ');
}
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public function testAscii()
$this->assertSame('Dieser Wert sollte groesser oder gleich', (string) $s->ascii(['de-ASCII']));
}

public function testAsciiClosureRule()
{
$rule = function ($c) {
return str_replace('ö', 'OE', $c);
};

$s = static::createFromString('Dieser Wert sollte größer oder gleich');
$this->assertSame('Dieser Wert sollte grOEsser oder gleich', (string) $s->ascii([$rule]));
}

public function provideCreateFromCodePoint(): array
{
return [
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/String/Tests/SluggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,15 @@ public function testSlugCharReplacementLocaleMethod()
$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);
}

public function testSlugClosure()
{
$slugger = new AsciiSlugger(null, function ($s, $locale) {
$this->assertSame('foo', $locale);

return str_replace('❤️', 'love', $s);
});

$this->assertSame('love', (string) $slugger->slug('❤️', '-', 'foo'));
}
}
0