Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TwigBridge] Add
emojify
twig filter
- Loading branch information
Showing
8 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.1 | ||
--- | ||
|
||
* Add `emoji` twig filter | ||
|
||
7.0 | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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 __construct() | ||
{ | ||
if (!class_exists(EmojiTransliterator::class)) { | ||
throw new \LogicException('You cannot use the "emoji" filter as the "Emoji" component is not installed. Try running "composer require symfony/emoji".'); | ||
} | ||
} | ||
|
||
public function getFilters(): array | ||
{ | ||
return [ | ||
new TwigFilter('emoji', $this->emoji(...)), | ||
]; | ||
} | ||
|
||
public function emoji(string $string, string $catalog = 'slack'): string | ||
{ | ||
try { | ||
$tr = self::$transliterators[$catalog] ??= EmojiTransliterator::create($catalog, EmojiTransliterator::REVERSE); | ||
} catch (\IntlException $e) { | ||
throw new \LogicException(sprintf('The emoji catalog "%s" is not available.', $catalog), previous: $e); | ||
} | ||
|
||
return (string) $tr->transliterate($string); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Bridge/Twig/Tests/Extension/EmojiExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ["🅰️", ":a:"] | ||
* ["🅰️", ":a:", "slack"] | ||
* ["🅰", ":a:", "github"] | ||
*/ | ||
public function testEmoji(string $expected, string $string, ?string $catalog = null) | ||
{ | ||
$extension = new EmojiExtension(); | ||
if ($catalog) { | ||
$this->assertSame($expected, $extension->emoji($string, $catalog)); | ||
} else { | ||
$this->assertSame($expected, $extension->emoji($string)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters