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\EmojiConverterInterface; |
10
|
|
|
use UnicornFail\Emoji\Emojibase\EmojibaseDatasetInterface; |
11
|
|
|
use UnicornFail\Emoji\Event\DocumentParsedEvent; |
12
|
|
|
use UnicornFail\Emoji\Node\Emoji; |
13
|
|
|
use UnicornFail\Emoji\Parser\Lexer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Processes all parsed Emoji nodes and set the various configurations. |
17
|
|
|
*/ |
18
|
|
|
final class EmojiCoreProcessor implements ConfigurationAwareInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ConfigurationInterface |
22
|
|
|
* |
23
|
|
|
* @psalm-readonly-allow-private-mutation |
24
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
25
|
|
|
*/ |
26
|
|
|
private $config; |
27
|
|
|
|
28
|
|
|
public function __invoke(DocumentParsedEvent $e): void |
29
|
|
|
{ |
30
|
|
|
/** @var string[] $excludedShortcodes */ |
31
|
|
|
$excludedShortcodes = $this->config->get('exclude/shortcodes'); |
32
|
|
|
|
33
|
|
|
/** @var ?int $presentation */ |
34
|
|
|
$presentation = $this->config->get('presentation'); |
35
|
|
|
|
36
|
|
|
// Ensure emojis are set to the correct stringable type. |
37
|
|
|
$walker = $e->getDocument()->walker(); |
38
|
|
|
while ($event = $walker->next()) { |
39
|
|
|
if (! $event->isEntering()) { |
40
|
|
|
continue; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$node = $event->getNode(); |
44
|
|
|
if (! ($node instanceof Emoji)) { |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$literal = null; |
49
|
|
|
$type = $node->getParsedType(); |
50
|
|
|
$configPath = 'convert.' . (EmojiConverterInterface::TYPES[$type] ?? ''); |
51
|
|
|
|
52
|
|
|
/** @var ?int $conversionType */ |
53
|
|
|
$conversionType = null; |
54
|
|
|
|
55
|
|
|
if ($this->config->exists($configPath) && ($configType = (string) ($this->config->get($configPath) ?? ''))) { |
56
|
|
|
$index = \array_search($configType, EmojiConverterInterface::TYPES, true); |
57
|
|
|
if (\is_int($index)) { |
58
|
|
|
$conversionType = $index; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// If the conversion type isn't one of the core Lexer:TYPES, then do nothing. |
63
|
|
|
// It should be handled by a different extension/processor. |
64
|
|
|
if ($conversionType === null) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
switch ($conversionType) { |
69
|
|
|
case Lexer::T_EMOTICON: |
70
|
|
|
$literal = $node->emoticon; |
71
|
|
|
break; |
72
|
|
|
|
73
|
|
|
case Lexer::T_HTML_ENTITY: |
74
|
|
|
$literal = $node->htmlEntity; |
75
|
|
|
break; |
76
|
|
|
|
77
|
|
|
case Lexer::T_SHORTCODE: |
78
|
|
|
if ($shortcode = $node->getShortcode($excludedShortcodes, true)) { |
79
|
|
|
$literal = $shortcode; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
break; |
83
|
|
|
|
84
|
|
|
case Lexer::T_TEXT: |
85
|
|
|
case Lexer::T_UNICODE: |
86
|
|
|
if (($presentation ?? $node->type) === EmojibaseDatasetInterface::TEXT && $node->text) { |
87
|
|
|
$literal = $node->text; |
88
|
|
|
} else { |
89
|
|
|
$literal = $node->emoji ?? $node->unicode; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($literal !== null) { |
96
|
|
|
$node->setLiteral($literal); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function setConfiguration(ConfigurationInterface $configuration): void |
102
|
|
|
{ |
103
|
|
|
$this->config = $configuration; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|