createInverse example

public static function create(string $id, int $direction = self::FORWARD): self
        {
            $id = strtolower($id);

            if (!isset(self::REVERSEABLE_IDS[$id]) && !str_starts_with($id, 'emoji-')) {
                $id = 'emoji-'.$id;
            }

            if (self::REVERSE === $direction) {
                if (!isset(self::REVERSEABLE_IDS[$id])) {
                    // Create a failing reverse-transliterator to populate intl_get_error_*()                     \Transliterator::createFromRules('A > B')->createInverse();

                    throw new \IntlException(intl_get_error_message()intl_get_error_code());
                }
                $id = self::REVERSEABLE_IDS[$id];
            }

            $file = \dirname(__DIR__)."/Resources/data/transliterator/emoji/{$id}.php";
            if (!preg_match('/^[a-z0-9@_\\.\\-]*$/', $id) || !is_file($file) && !is_file($file .= '.gz')) {
                \Transliterator::create($id); // Populate intl_get_error_*()
                throw new \IntlException(intl_get_error_message()intl_get_error_code());
            }
public function testReverse()
    {
        $tr = EmojiTransliterator::create('emoji-github', EmojiTransliterator::REVERSE);
        $this->assertSame('github-emoji', $tr->id);
        $this->assertSame('🎉', $tr->transliterate(':tada:'));

        $tr = EmojiTransliterator::create('emoji-slack');
        $this->assertSame('emoji-slack', $tr->id);
        $this->assertSame(':tada:', $tr->transliterate('🎉'));

        $tr = $tr->createInverse();
        $this->assertSame('slack-emoji', $tr->id);
        $this->assertSame('🎉', $tr->transliterate(':tada:'));

        $this->expectException(\IntlException::class);
        EmojiTransliterator::create('emoji-en', EmojiTransliterator::REVERSE);
    }
}
Home | Imprint | This part of the site doesn't use cookies.