|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Emoji; |
| 13 | + |
| 14 | +use Symfony\Component\Emoji\Util\GzipStreamWrapper; |
| 15 | + |
| 16 | +if (!class_exists(\Transliterator::class)) { |
| 17 | + throw new \LogicException(sprintf('You cannot use the "%s\EmojiTransliterator" class as the "intl" extension is not installed. See https://php.net/intl.', __NAMESPACE__)); |
| 18 | +} |
| 19 | + |
| 20 | +final class EmojiTransliterator extends \Transliterator |
| 21 | +{ |
| 22 | + private const QUICK_CHECK = "\xA9\xAE\xE2\xE3\xF0"; |
| 23 | + private const REVERSEABLE_IDS = [ |
| 24 | + 'emoji-github' => 'github-emoji', |
| 25 | + 'emoji-slack' => 'slack-emoji', |
| 26 | + 'github-emoji' => 'emoji-github', |
| 27 | + 'slack-emoji' => 'emoji-slack', |
| 28 | + ]; |
| 29 | + |
| 30 | + public readonly string $id; |
| 31 | + |
| 32 | + private array $map; |
| 33 | + |
| 34 | + private string $quickCheck; |
| 35 | + |
| 36 | + private \Transliterator $transliterator; |
| 37 | + |
| 38 | + public static function create(string $id, int $direction = self::FORWARD): self |
| 39 | + { |
| 40 | + $id = strtolower($id); |
| 41 | + |
| 42 | + if (!isset(self::REVERSEABLE_IDS[$id]) && !str_starts_with($id, 'emoji-')) { |
| 43 | + $id = 'emoji-'.$id; |
| 44 | + } |
| 45 | + |
| 46 | + if (self::REVERSE === $direction) { |
| 47 | + if (!isset(self::REVERSEABLE_IDS[$id])) { |
| 48 | + // Create a failing reverse-transliterator to populate intl_get_error_*() |
| 49 | + \Transliterator::createFromRules('A > B')->createInverse(); |
| 50 | + |
| 51 | + throw new \IntlException(intl_get_error_message(), intl_get_error_code()); |
| 52 | + } |
| 53 | + $id = self::REVERSEABLE_IDS[$id]; |
| 54 | + } |
| 55 | + |
| 56 | + $file = __DIR__."/Resources/data/{$id}.php"; |
| 57 | + if (!preg_match('/^[a-z0-9@_\\.\\-]*$/', $id) || !is_file($file) && !is_file($file .= '.gz')) { |
| 58 | + \Transliterator::create($id); // Populate intl_get_error_*() |
| 59 | + |
| 60 | + throw new \IntlException(intl_get_error_message(), intl_get_error_code()); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @var array<string, array> $maps |
| 65 | + */ |
| 66 | + static $maps; |
| 67 | + |
| 68 | + // Create an instance of \Transliterator with a custom id; that's the only way |
| 69 | + static $newInstance; |
| 70 | + $instance = ($newInstance ??= (new \ReflectionClass(self::class))->newInstanceWithoutConstructor(...))(); |
| 71 | + $instance->id = $id; |
| 72 | + $instance->map = $maps[$id] ??= str_ends_with($file, '.gz') ? GzipStreamWrapper::require($file) : require $file; |
| 73 | + |
| 74 | + return $instance; |
| 75 | + } |
| 76 | + |
| 77 | + public function createInverse(): self |
| 78 | + { |
| 79 | + return self::create($this->id, \Transliterator::REVERSE); |
| 80 | + } |
| 81 | + |
| 82 | + public function getErrorCode(): int|false |
| 83 | + { |
| 84 | + return $this->transliterator?->getErrorCode() ?? 0; |
| 85 | + } |
| 86 | + |
| 87 | + public function getErrorMessage(): string|false |
| 88 | + { |
| 89 | + return $this->transliterator?->getErrorMessage() ?? false; |
| 90 | + } |
| 91 | + |
| 92 | + public static function listIDs(): array |
| 93 | + { |
| 94 | + static $ids = []; |
| 95 | + |
| 96 | + if ($ids) { |
| 97 | + return $ids; |
| 98 | + } |
| 99 | + |
| 100 | + foreach (scandir(__DIR__.'/Resources/data/') as $file) { |
| 101 | + if (str_ends_with($file, '.php.gz')) { |
| 102 | + $ids[] = substr($file, 0, -7); |
| 103 | + } elseif (str_ends_with($file, '.php')) { |
| 104 | + $ids[] = substr($file, 0, -4); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return $ids; |
| 109 | + } |
| 110 | + |
| 111 | + public function transliterate(string $string, int $start = 0, int $end = -1): string|false |
| 112 | + { |
| 113 | + $this->quickCheck ??= str_starts_with(array_key_first($this->map), ':') ? ':' : self::QUICK_CHECK; |
| 114 | + |
| 115 | + if (0 === $start && -1 === $end && preg_match('//u', $string)) { |
| 116 | + return \strlen($string) === strcspn($string, $this->quickCheck) ? $string : strtr($string, $this->map); |
| 117 | + } |
| 118 | + |
| 119 | + // Here we rely on intl to validate the $string, $start and $end arguments |
| 120 | + // and to slice the string. Slicing is done by replacing the part if $string |
| 121 | + // between $start and $end by a unique cookie that can be reliably used to |
| 122 | + // identify which part of $string should be transliterated. |
| 123 | + |
| 124 | + static $cookie; |
| 125 | + static $transliterator; |
| 126 | + |
| 127 | + $cookie ??= hash('xxh128', random_bytes(8)); |
| 128 | + $this->transliterator ??= clone $transliterator ??= \Transliterator::createFromRules('[:any:]* > '.$cookie); |
| 129 | + |
| 130 | + if (false === $result = $this->transliterator->transliterate($string, $start, $end)) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + $parts = explode($cookie, $result); |
| 135 | + $start = \strlen($parts[0]); |
| 136 | + $length = -\strlen($parts[1]) ?: null; |
| 137 | + $string = substr($string, $start, $length); |
| 138 | + |
| 139 | + return $parts[0].(\strlen($string) === strcspn($string, $this->quickCheck) ? $string : strtr($string, $this->map)).$parts[1]; |
| 140 | + } |
| 141 | +} |
0 commit comments