1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace UnicornFail\Emoji\Extension\Twemoji; |
6
|
|
|
|
7
|
|
|
use League\Configuration\ConfigurationAwareInterface; |
8
|
|
|
use League\Configuration\ConfigurationInterface; |
9
|
|
|
use UnicornFail\Emoji\EmojiConverterInterface; |
10
|
|
|
use UnicornFail\Emoji\Event\DocumentParsedEvent; |
11
|
|
|
use UnicornFail\Emoji\Node\Emoji; |
12
|
|
|
use UnicornFail\Emoji\Node\Image; |
13
|
|
|
use UnicornFail\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
|
|
|
$walker = $e->getDocument()->walker(); |
44
|
|
|
while ($event = $walker->next()) { |
45
|
|
|
$node = $event->getNode(); |
46
|
|
|
if (! ($node instanceof Emoji) || $node->hexcode === null) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$parsedType = $node->getParsedType(); |
51
|
|
|
$configPath = 'convert.' . (EmojiConverterInterface::TYPES[$parsedType] ?? ''); |
52
|
|
|
$conversionType = null; |
53
|
|
|
|
54
|
|
|
if ($this->config->exists($configPath)) { |
55
|
|
|
$conversionType = (string) ($this->config->get($configPath) ?? ''); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Only convert types that are set to "twemoji". |
59
|
|
|
if ($conversionType !== self::CONVERSION_TYPE) { |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$url = \sprintf( |
64
|
|
|
'%s/%s/%s.%s', |
65
|
|
|
$urlBase, |
66
|
|
|
$type === 'png' ? '72x72' : 'svg', |
67
|
|
|
\strtolower($node->hexcode), |
68
|
|
|
$type |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$image = new Image($node->getParsedValue(), $node, $url, $node->annotation, $node->annotation); |
72
|
|
|
|
73
|
|
|
/** @var string[] $classes */ |
74
|
|
|
$classes = (array) $this->config->get('twemoji.classes'); |
75
|
|
|
$image->addClass(...$classes); |
76
|
|
|
|
77
|
|
|
if ($node->annotation !== null) { |
78
|
|
|
$image->addClass(HtmlElement::cleanCssIdentifier($classPrefix |
79
|
|
|
? $classPrefix . $node->annotation |
80
|
|
|
: $node->annotation)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Ensure image isn't massive and relative to its surroundings by inlining it. |
84
|
|
|
if ($inline && $size === null) { |
85
|
|
|
$image->attributes->set('style', 'width: 1em; height: 1em; vertical-align: middle;'); |
86
|
|
|
} elseif ($inline && $size !== null) { |
87
|
|
|
if (! \is_string($size)) { |
88
|
|
|
$size .= 'em'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$image->attributes->set('style', \sprintf('width: %s; height: %s; vertical-align: middle;', $size, $size)); |
92
|
|
|
} elseif ($size !== null) { |
93
|
|
|
$image->attributes->set('height', (string) $size); |
94
|
|
|
$image->attributes->set('width', (string) $size); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$node->replaceWith($image); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function setConfiguration(ConfigurationInterface $configuration): void |
102
|
|
|
{ |
103
|
|
|
$this->config = $configuration; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|