1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace League\Emoji\Extension\Twemoji; |
6
|
|
|
|
7
|
|
|
use League\Configuration\ConfigurationAwareInterface; |
8
|
|
|
use League\Configuration\ConfigurationInterface; |
9
|
|
|
use League\Emoji\EmojiConverterInterface; |
10
|
|
|
use League\Emoji\Event\DocumentParsedEvent; |
11
|
|
|
use League\Emoji\Node\Emoji; |
12
|
|
|
use League\Emoji\Node\Image; |
13
|
|
|
use League\Emoji\Util\HtmlElement; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Replaces emojis with Twemoji images. |
17
|
|
|
*/ |
18
|
|
|
final class TwemojiProcessor implements ConfigurationAwareInterface |
19
|
|
|
{ |
20
|
|
|
public const CONVERSION_TYPE = 'twemoji'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ConfigurationInterface |
24
|
|
|
* |
25
|
|
|
* @psalm-readonly-allow-private-mutation |
26
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
27
|
|
|
*/ |
28
|
|
|
private $config; |
29
|
|
|
|
30
|
|
|
public function __invoke(DocumentParsedEvent $e): void |
31
|
|
|
{ |
32
|
|
|
$urlBase = (string) $this->config->get('twemoji.urlBase'); |
33
|
|
|
|
34
|
|
|
$classPrefix = (string) $this->config->get('twemoji.classPrefix'); |
35
|
|
|
|
36
|
|
|
/** @var int|float|string|null $size */ |
37
|
|
|
$size = $this->config->get('twemoji.size'); |
38
|
|
|
|
39
|
|
|
$inline = (bool) $this->config->get('twemoji.inline'); |
40
|
|
|
|
41
|
|
|
$type = (string) $this->config->get('twemoji.type'); |
42
|
|
|
|
43
|
|
|
foreach ($e->getDocument()->getNodes() as $node) { |
44
|
|
|
if (! ($node instanceof Emoji) || $node->hexcode === null) { |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$parsedType = $node->getParsedType(); |
49
|
|
|
$configPath = 'convert.' . (EmojiConverterInterface::TYPES[$parsedType] ?? ''); |
50
|
|
|
$conversionType = null; |
51
|
|
|
|
52
|
|
|
if ($this->config->exists($configPath)) { |
53
|
|
|
$conversionType = (string) ($this->config->get($configPath) ?? ''); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Only convert types that are set to "twemoji". |
57
|
|
|
if ($conversionType !== self::CONVERSION_TYPE) { |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$url = \sprintf( |
62
|
|
|
'%s/%s/%s.%s', |
63
|
|
|
$urlBase, |
64
|
|
|
$type === 'png' ? '72x72' : 'svg', |
65
|
|
|
\strtolower($node->hexcode), |
66
|
|
|
$type |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$image = new Image($node->getParsedValue(), $node, $url, $node->annotation, $node->annotation); |
70
|
|
|
|
71
|
|
|
/** @var string[] $classes */ |
72
|
|
|
$classes = (array) $this->config->get('twemoji.classes'); |
73
|
|
|
$image->addClass(...$classes); |
74
|
|
|
|
75
|
|
|
if ($node->annotation !== null) { |
76
|
|
|
$image->addClass(HtmlElement::cleanCssIdentifier($classPrefix |
77
|
|
|
? $classPrefix . $node->annotation |
78
|
|
|
: $node->annotation)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Ensure image isn't massive and relative to its surroundings by inlining it. |
82
|
|
|
if ($inline && $size === null) { |
83
|
|
|
$image->setAttribute('style', 'width: 1em; height: 1em; vertical-align: middle;'); |
84
|
|
|
} elseif ($inline && $size !== null) { |
85
|
|
|
if (! \is_string($size)) { |
86
|
|
|
$size .= 'em'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$image->setAttribute('style', \sprintf('width: %s; height: %s; vertical-align: middle;', $size, $size)); |
90
|
|
|
} elseif ($size !== null) { |
91
|
|
|
$image->setAttribute('height', (string) $size); |
92
|
|
|
$image->setAttribute('width', (string) $size); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$node->replaceWith($image); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function setConfiguration(ConfigurationInterface $configuration): void |
100
|
|
|
{ |
101
|
|
|
$this->config = $configuration; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|