Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[String] Add support for emoji in AsciiSlugger #47264

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
Aug 17, 2022
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/String/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.2
---

* Add support for emoji in `AsciiSlugger`

5.4
---

Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/String/Slugger/AsciiSlugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -88,6 +90,23 @@ public function getLocale(): string
return $this->defaultLocale;
}

/**
* @param bool|string $emoji true will use the same locale,
* false will disable emoji,
* and a string to use a specific locale
*/
public function withEmoji(bool|string $emoji = true): static
{
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;
}

/**
* {@inheritdoc}
*/
Expand All @@ -103,6 +122,12 @@ public function slug(string $string, string $separator = '-', string $locale = n
$transliterator = (array) $this->createTransliterator($locale);
}

if (\is_string($this->emoji)) {
$transliterator[] = EmojiTransliterator::create("emoji-{$this->emoji}");
} elseif ($this->emoji && null !== $locale) {
$transliterator[] = EmojiTransliterator::create("emoji-{$locale}");
}

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.
Expand Down
57 changes: 54 additions & 3 deletions src/Symfony/Component/String/Tests/Slugger/AsciiSluggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

class AsciiSluggerTest extends TestCase
{
/**
* @dataProvider provideSlugTests
*/
public function testSlug(string $expected, string $string, string $separator = '-', string $locale = null)
{
$slugger = new AsciiSlugger();

$this->assertSame($expected, (string) $slugger->slug($string, $separator, $locale));
}

public function provideSlugTests(): iterable
{
yield ['', ''];
Expand All @@ -37,11 +47,52 @@ public function provideSlugTests(): iterable
yield [\function_exists('transliterator_transliterate') ? 'gh' : '', 'ғ', '-', 'uz_fr']; // Ensure we get the parent locale
}

/** @dataProvider provideSlugTests */
public function testSlug(string $expected, string $string, string $separator = '-', string $locale = null)
/**
* @dataProvider provideSlugEmojiTests
* @requires extension intl
*/
public function testSlugEmoji(string $expected, string $string, ?string $locale, string|bool $emoji = true)
{
$slugger = new AsciiSlugger();
$slugger = $slugger->withEmoji($emoji);

$this->assertSame($expected, (string) $slugger->slug($string, $separator, $locale));
$this->assertSame($expected, (string) $slugger->slug($string, '-', $locale));
}

public function provideSlugEmojiTests(): iterable
{
yield [
'un-chat-qui-sourit-chat-noir-et-un-tete-de-lion-vont-au-parc-national',
'un 😺, 🐈‍⬛, et un 🦁 vont au 🏞️',
'fr',
];
yield [
'a-grinning-cat-black-cat-and-a-lion-go-to-national-park-smiling-face-with-heart-eyes-party-popper-yellow-heart',
'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
'en',
];
yield [
'a-and-a-go-to',
'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
null,
];
yield [
'a-smiley-cat-black-cat-and-a-lion-face-go-to-national-park-heart-eyes-tada-yellow-heart',
'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
null,
'slack',
];
yield [
'a-smiley-cat-black-cat-and-a-lion-go-to-national-park-heart-eyes-tada-yellow-heart',
'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
null,
'github',
];
yield [
'a-smiley-cat-black-cat-and-a-lion-go-to-national-park-heart-eyes-tada-yellow-heart',
'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
'en',
'github',
];
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/String/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"require-dev": {
"symfony/error-handler": "^5.4|^6.0",
"symfony/intl": "^6.2",
"symfony/http-client": "^5.4|^6.0",
"symfony/translation-contracts": "^2.0|^3.0",
"symfony/var-exporter": "^5.4|^6.0"
Expand Down