|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace UnicornFail\Emoji\Extension\Twemoji; |
|
6
|
|
|
|
|
7
|
|
|
use Astrotomic\Twemoji\Twemoji; |
|
8
|
|
|
use League\Configuration\ConfigurationAwareInterface; |
|
9
|
|
|
use League\Configuration\ConfigurationInterface; |
|
10
|
|
|
use UnicornFail\Emoji\Event\DocumentParsedEvent; |
|
11
|
|
|
use UnicornFail\Emoji\Node\Inline\AbstractEmoji; |
|
12
|
|
|
use UnicornFail\Emoji\Node\Inline\EmojiImage; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Replaces emojis with Twemoji images. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class TwemojiProcessor implements ConfigurationAwareInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ConfigurationInterface |
|
21
|
|
|
* |
|
22
|
|
|
* @psalm-readonly-allow-private-mutation |
|
23
|
|
|
*/ |
|
24
|
|
|
private $config; |
|
25
|
|
|
|
|
26
|
|
|
public function setConfiguration(ConfigurationInterface $configuration): void |
|
27
|
|
|
{ |
|
28
|
|
|
$this->config = $configuration; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function __invoke(DocumentParsedEvent $e): void |
|
32
|
|
|
{ |
|
33
|
|
|
$base = (string) $this->config->get('twemoji.base'); |
|
34
|
|
|
|
|
35
|
|
|
/** @var ?int $size */ |
|
36
|
|
|
$size = $this->config->get('twemoji.size'); |
|
37
|
|
|
|
|
38
|
|
|
$type = (string) $this->config->get('twemoji.type'); |
|
39
|
|
|
|
|
40
|
|
|
$walker = $e->getDocument()->walker(); |
|
41
|
|
|
while ($event = $walker->next()) { |
|
42
|
|
|
$node = $event->getNode(); |
|
43
|
|
|
if (! ($node instanceof AbstractEmoji)) { |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$emoji = $node->getEmoji(); |
|
48
|
|
|
$unicode = $emoji->getUnicode(); |
|
49
|
|
|
|
|
50
|
|
|
if (! $unicode) { |
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$twemoji = Twemoji::emoji($unicode)->base($base); |
|
55
|
|
|
|
|
56
|
|
|
if ($type === 'png') { |
|
57
|
|
|
$twemoji->png(); |
|
58
|
|
|
$size = 72; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @var string[] $classes */ |
|
62
|
|
|
$classes = (array) $this->config->get('twemoji.classes'); |
|
63
|
|
|
|
|
64
|
|
|
$shortcode = $emoji->getShortcode(); |
|
65
|
|
|
if ($shortcode !== null) { |
|
66
|
|
|
$classes[] = \str_replace('_', '-', $shortcode); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$image = new EmojiImage($node->getParsedValue(), $emoji, $twemoji->url(), $emoji->annotation ?? $shortcode); |
|
70
|
|
|
$image->addClass(...$classes); |
|
71
|
|
|
|
|
72
|
|
|
if ($size) { |
|
|
|
|
|
|
73
|
|
|
$image->setAttribute('height', (string) $size); |
|
74
|
|
|
$image->setAttribute('width', (string) $size); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$node->replaceWith($image); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: