function AsciiSlugger::slug
Overrides SluggerInterface::slug
File
-
vendor/
symfony/ string/ Slugger/ AsciiSlugger.php, line 104
Class
- AsciiSlugger
- @author Titouan Galopin <galopintitouan@gmail.com>
Namespace
Symfony\Component\String\SluggerCode
public function slug(string $string, string $separator = '-', ?string $locale = null) : AbstractUnicodeString {
$locale ??= $this->defaultLocale;
$transliterator = [];
if ($locale && ('de' === $locale || str_starts_with($locale, 'de_'))) {
// Use the shortcut for German in UnicodeString::ascii() if possible (faster and no requirement on intl)
$transliterator = [
'de-ASCII',
];
}
elseif (\function_exists('transliterator_transliterate') && $locale) {
$transliterator = (array) $this->createTransliterator($locale);
}
if ($emojiTransliterator = $this->createEmojiTransliterator($locale)) {
$transliterator[] = $emojiTransliterator;
}
if ($this->symbolsMap instanceof \Closure) {
// If the symbols map is passed as a closure, there is no need to fallback to the parent locale
// as the closure can just provide substitutions for all locales of interest.
$symbolsMap = $this->symbolsMap;
array_unshift($transliterator, static fn($s) => $symbolsMap($s, $locale));
}
$unicodeString = (new UnicodeString($string))->ascii($transliterator);
if (\is_array($this->symbolsMap)) {
$map = null;
if (isset($this->symbolsMap[$locale])) {
$map = $this->symbolsMap[$locale];
}
else {
$parent = self::getParentLocale($locale);
if ($parent && isset($this->symbolsMap[$parent])) {
$map = $this->symbolsMap[$parent];
}
}
if ($map) {
foreach ($map as $char => $replace) {
$unicodeString = $unicodeString->replace($char, ' ' . $replace . ' ');
}
}
}
return $unicodeString->replaceMatches('/[^A-Za-z0-9]++/', $separator)
->trim($separator);
}