This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -11,6 +11,7 @@
namespace Symfony\Component\String\Slugger;
use Symfony\Component\Intl\Transliterator\EmojiTransliterator;
use Symfony\Component\String\AbstractUnicodeString;
use Symfony\Component\String\UnicodeString;
use Symfony\Contracts\Translation\LocaleAwareInterface;
@@ -58,6 +59,7 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface
private \Closure|array $symbolsMap = [
'en' => ['@' => 'at', '&' => 'and'],
];
private bool|string $emoji = false;
/**
* Cache of transliterators per locale.
@@ -66,50 +68,63 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface
*/
private array $transliterators = [];
public function __construct(string $defaultLocale = null, array|\Closure $symbolsMap = null)
public function __construct(?string $defaultLocale = null, array|\Closure|null $symbolsMap = null)
{
$this->defaultLocale = $defaultLocale;
$this->symbolsMap = $symbolsMap ?? $this->symbolsMap;
}
/**
* {@inheritdoc}
* @return void
*/
public function setLocale(string $locale)
{
$this->defaultLocale = $locale;
}
/**
* {@inheritdoc}
*/
public function getLocale(): string
{
return $this->defaultLocale;
}
/**
* {@inheritdoc}
* @param bool|string $emoji true will use the same locale,
* false will disable emoji,
* and a string to use a specific locale
*/
public function slug(string $string, string $separator = '-', string $locale = null): AbstractUnicodeString
public function withEmoji(bool|string $emoji = true): static
{
$locale = $locale ?? $this->defaultLocale;
if (false !== $emoji && !class_exists(EmojiTransliterator::class)) {
throw new \LogicException(sprintf('You cannot use the "%s()" method as the "symfony/intl" package is not installed. Try running "composer require symfony/intl".', __METHOD__));
}
$new = clone $this;
$new->emoji = $emoji;
return $new;
}
public function slug(string $string, string $separator = '-', ?string $locale = null): AbstractUnicodeString
{
$locale ??= $this->defaultLocale;
$transliterator = [];
if ($locale && ('de' === $locale || 0 === strpos($locale, 'de_'))) {
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 function ($s) use ($symbolsMap, $locale) {
return $symbolsMap($s, $locale);
});
array_unshift($transliterator, static fn ($s) => $symbolsMap($s, $locale));
}
$unicodeString = (new UnicodeString($string))->ascii($transliterator);
@@ -161,6 +176,25 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface
return $this->transliterators[$locale] = $this->transliterators[$parent] = $transliterator ?? null;
}
private function createEmojiTransliterator(?string $locale): ?EmojiTransliterator
{
if (\is_string($this->emoji)) {
$locale = $this->emoji;
} elseif (!$this->emoji) {
return null;
}
while (null !== $locale) {
try {
return EmojiTransliterator::create("emoji-$locale");
} catch (\IntlException) {
$locale = self::getParentLocale($locale);
}
}
return null;
}
private static function getParentLocale(?string $locale): ?string
{
if (!$locale) {

View File

@@ -23,5 +23,5 @@ interface SluggerInterface
/**
* Creates a slug for the given string and locale, using appropriate transliteration when needed.
*/
public function slug(string $string, string $separator = '-', string $locale = null): AbstractUnicodeString;
public function slug(string $string, string $separator = '-', ?string $locale = null): AbstractUnicodeString;
}