Skip to content

Commit

Permalink
[TwigBridge] Add emojify twig filter
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Mar 28, 2024
1 parent fc97603 commit 643b9d2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Add `emojify` twig filter

7.0
---

Expand Down
42 changes: 42 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/EmojiExtension.php
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Extension;

use Symfony\Component\Emoji\EmojiTransliterator;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
final class EmojiExtension extends AbstractExtension
{
private static array $transliterators = [];

public function getFilters(): array
{
return [
new TwigFilter('emojify', $this->emojify(...)),
];
}

public function emojify(string $string, string $catalog = 'slack'): string
{
if (!in_array($catalog, $catalogs = ['slack', 'github'], true)) {
throw new \InvalidArgumentException(sprintf('The catalog "%s" is not supported. Try one among "%s".', $catalog, implode('", "', $catalogs)));
}

$tr = self::$transliterators[$catalog] ??= EmojiTransliterator::create('emoji-'.$catalog, EmojiTransliterator::REVERSE);

return (string) $tr->transliterate($string);
}
}
33 changes: 33 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Extension/EmojiExtensionTest.php
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Tests\Extension;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Extension\EmojiExtension;

class EmojiExtensionTest extends TestCase
{
/**
* @testWith ["😂", ":joy:"]
* ["😂", ":joy:", "slack"]
* ["😂", ":joy:", "github"]
*/
public function testEmojify(string $expected, string $string, ?string $catalog = null): void
{
$extension = new EmojiExtension();
if ($catalog){
$this->assertSame($expected, $extension->emojify($string, $catalog));
} else {
$this->assertSame($expected, $extension->emojify($string));
}
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/composer.json
Expand Up @@ -27,6 +27,7 @@
"symfony/asset": "^6.4|^7.0",
"symfony/asset-mapper": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/emoji": "^7.1",
"symfony/finder": "^6.4|^7.0",
"symfony/form": "^6.4|^7.0",
"symfony/html-sanitizer": "^6.4|^7.0",
Expand Down

0 comments on commit 643b9d2

Please sign in to comment.