|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace UnicornFail\Emoji\Extension; |
|
6
|
|
|
|
|
7
|
|
|
use League\Configuration\ConfigurationAwareInterface; |
|
8
|
|
|
use League\Configuration\ConfigurationInterface; |
|
9
|
|
|
use UnicornFail\Emoji\Event\DocumentParsedEvent; |
|
10
|
|
|
use UnicornFail\Emoji\Node\Inline\AbstractEmoji; |
|
11
|
|
|
use UnicornFail\Emoji\Parser\Lexer; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Processes all parsed Emoji nodes and set the various configurations. |
|
15
|
|
|
*/ |
|
16
|
|
|
final class EmojiCoreProcessor implements ConfigurationAwareInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ConfigurationInterface |
|
20
|
|
|
* |
|
21
|
|
|
* @psalm-readonly-allow-private-mutation |
|
22
|
|
|
*/ |
|
23
|
|
|
private $config; |
|
24
|
|
|
|
|
25
|
|
|
public function setConfiguration(ConfigurationInterface $configuration): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->config = $configuration; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function __invoke(DocumentParsedEvent $e): void |
|
31
|
|
|
{ |
|
32
|
|
|
$convertTo = (string) ($this->config->get('convertTo') ?? Lexer::UNICODE); |
|
33
|
|
|
|
|
34
|
|
|
/** @var string[] $excludedShortcodes */ |
|
35
|
|
|
$excludedShortcodes = $this->config->get('exclude.shortcodes'); |
|
36
|
|
|
|
|
37
|
|
|
/** @var ?int $presentation */ |
|
38
|
|
|
$presentation = $this->config->get('presentation'); |
|
39
|
|
|
|
|
40
|
|
|
// Ensure tokens are set to the correct stringable type. |
|
41
|
|
|
$walker = $e->getDocument()->walker(); |
|
42
|
|
|
while ($event = $walker->next()) { |
|
43
|
|
|
if (! $event->isEntering()) { |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$emoji = $event->getNode(); |
|
48
|
|
|
if (! ($emoji instanceof AbstractEmoji)) { |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$emoji->setConvertTo($convertTo); |
|
53
|
|
|
$emoji->setExcludedShortcodes($excludedShortcodes); |
|
54
|
|
|
|
|
55
|
|
|
if ($presentation !== null) { |
|
56
|
|
|
$emoji->setPresentationMode($presentation); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|