Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AsciiSlugger.php

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\Slugger

Code

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);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal