Skip to content

Commit c1df6da

Browse files
[Intl] Fix EmojiTransliterator on PHP 8.2
1 parent 7940cc4 commit c1df6da

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/Symfony/Component/Intl/Tests/Transliterator/EmojiTransliteratorTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@
1818
/**
1919
* @requires extension intl
2020
*/
21-
final class EmojiTransliteratorTest extends TestCase
21+
class EmojiTransliteratorTest extends TestCase
2222
{
23+
protected function setUp(): void
24+
{
25+
if (\PHP_VERSION_ID >= 80200 && !(new \ReflectionProperty(\Transliterator::class, 'id'))->isReadOnly()) {
26+
$this->markTestSkipped('Waiting for https://github.com/php/php-src/pull/9167 to be merged');
27+
}
28+
}
29+
2330
public function provideTransliterateTests(): iterable
2431
{
2532
yield [

src/Symfony/Component/Intl/Transliterator/EmojiTransliterator.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,29 @@
1111

1212
namespace Symfony\Component\Intl\Transliterator;
1313

14-
final class EmojiTransliterator extends \Transliterator
15-
{
16-
private const QUICK_CHECK = "\xE2\xE3\xF0";
14+
if (\PHP_VERSION_ID >= 80200) {
15+
final class EmojiTransliterator extends \Transliterator
16+
{
17+
use EmojiTransliteratorTrait;
18+
19+
private const QUICK_CHECK = "\xE2\xE3\xF0";
20+
21+
public readonly string $id;
22+
}
23+
} else {
24+
final class EmojiTransliterator extends \Transliterator
25+
{
26+
use EmojiTransliteratorTrait;
27+
28+
private const QUICK_CHECK = "\xE2\xE3\xF0";
29+
}
30+
}
1731

32+
/**
33+
* @internal
34+
*/
35+
trait EmojiTransliteratorTrait
36+
{
1837
private array $map;
1938
private \Transliterator $transliterator;
2039

@@ -37,7 +56,14 @@ public static function create(string $id, int $direction = self::FORWARD): ?\Tra
3756
static $maps;
3857

3958
// Create an instance of \Transliterator with a custom id; that's the only way
40-
$instance = unserialize(sprintf('O:%d:"%s":1:{s:2:"id";s:%d:"%s";}', \strlen(self::class), self::class, \strlen($id), $id));
59+
if (\PHP_VERSION_ID >= 80200) {
60+
static $newInstance;
61+
$instance = ($newInstance ??= (new \ReflectionClass(self::class))->newInstanceWithoutConstructor(...))();
62+
$instance->id = $id;
63+
} else {
64+
$instance = unserialize(sprintf('O:%d:"%s":1:{s:2:"id";s:%d:"%s";}', \strlen(self::class), self::class, \strlen($id), $id));
65+
}
66+
4167
$instance->map = $maps[$id] ??= require \dirname(__DIR__)."/Resources/data/transliterator/emoji/{$id}.php";
4268

4369
return $instance;
@@ -95,10 +121,6 @@ public function transliterate(string $string, int $start = 0, int $end = -1): st
95121
return false;
96122
}
97123

98-
if ($cookie === $result) {
99-
return \strlen($string) === strcspn($string, self::QUICK_CHECK) ? $string : strtr($string, $this->map);
100-
}
101-
102124
$parts = explode($cookie, $result);
103125
$start = \strlen($parts[0]);
104126
$length = -\strlen($parts[1]) ?: null;

0 commit comments

Comments
 (0)