|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace League\Emoji\Lexer; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Lexer\AbstractLexer; |
|
8
|
|
|
use League\Emoji\Emojibase\EmojibaseRegexInterface; |
|
9
|
|
|
use League\Emoji\Environment\EnvironmentInterface; |
|
10
|
|
|
use League\Emoji\Exception\UnexpectedEncodingException; |
|
11
|
|
|
|
|
12
|
|
|
class EmojiLexer extends AbstractLexer implements EmojibaseRegexInterface |
|
13
|
|
|
{ |
|
14
|
|
|
public const T_TEXT = 0; |
|
15
|
|
|
|
|
16
|
|
|
public const T_EMOTICON = 1; |
|
17
|
|
|
|
|
18
|
|
|
public const T_HTML_ENTITY = 2; |
|
19
|
|
|
|
|
20
|
|
|
public const T_SHORTCODE = 3; |
|
21
|
|
|
|
|
22
|
|
|
public const T_UNICODE = 4; |
|
23
|
|
|
|
|
24
|
|
|
public const TYPES = [ |
|
25
|
|
|
self::T_TEXT, |
|
26
|
|
|
self::T_EMOTICON, |
|
27
|
|
|
self::T_HTML_ENTITY, |
|
28
|
|
|
self::T_SHORTCODE, |
|
29
|
|
|
self::T_UNICODE, |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** @var EnvironmentInterface */ |
|
33
|
|
|
private $environment; |
|
34
|
|
|
|
|
35
|
642 |
|
public function __construct(EnvironmentInterface $environment) |
|
36
|
|
|
{ |
|
37
|
642 |
|
$this->environment = $environment; |
|
38
|
642 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
* |
|
43
|
|
|
* @return string[] |
|
44
|
|
|
*/ |
|
45
|
240 |
|
protected function getCatchablePatterns() |
|
46
|
|
|
{ |
|
47
|
240 |
|
$config = $this->environment->getConfiguration(); |
|
48
|
|
|
|
|
49
|
240 |
|
$patterns = []; |
|
50
|
|
|
|
|
51
|
240 |
|
if ($config->get('convert.unicode')) { |
|
52
|
240 |
|
$patterns[] = self::CODEPOINT_EMOJI_LOOSE_REGEX; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
240 |
|
if ($config->get('convert.htmlEntity')) { |
|
56
|
240 |
|
$patterns[] = self::HTML_ENTITY_REGEX; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
240 |
|
if ($config->get('convert.shortcode')) { |
|
60
|
240 |
|
$patterns[] = $this->environment->getRuntimeDataset()->isNative() |
|
61
|
21 |
|
? self::SHORTCODE_NATIVE_REGEX |
|
62
|
219 |
|
: self::SHORTCODE_REGEX; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
240 |
|
if ($config->get('convert.emoticon')) { |
|
66
|
240 |
|
$patterns[] = self::EMOTICON_REGEX; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
240 |
|
return static::cleanPatterns($patterns); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string[] $patterns |
|
74
|
|
|
* |
|
75
|
|
|
* @return string[] |
|
76
|
|
|
*/ |
|
77
|
240 |
|
protected static function cleanPatterns(array $patterns): array |
|
78
|
|
|
{ |
|
79
|
|
|
// Some regex patterns from the constants include the delimiter and modifiers. Because the |
|
80
|
|
|
// lexer joins these expressions together as an OR group (|), they must be removed. |
|
81
|
240 |
|
foreach ($patterns as &$pattern) { |
|
82
|
240 |
|
$pattern = \trim(\rtrim($pattern, 'imsxeADSUXJu'), '/'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
240 |
|
return $patterns; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritDoc} |
|
90
|
|
|
* |
|
91
|
|
|
* @return string[] |
|
92
|
|
|
*/ |
|
93
|
240 |
|
protected function getNonCatchablePatterns() |
|
94
|
|
|
{ |
|
95
|
240 |
|
return []; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritDoc} |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $value |
|
102
|
|
|
* |
|
103
|
|
|
* @return int |
|
104
|
|
|
* |
|
105
|
|
|
* @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection |
|
106
|
|
|
*/ |
|
107
|
237 |
|
protected function getType(&$value) |
|
108
|
|
|
{ |
|
109
|
237 |
|
if (\preg_match($this->environment->getRuntimeDataset()->isNative() ? self::SHORTCODE_NATIVE_REGEX : self::SHORTCODE_REGEX, $value)) { |
|
110
|
75 |
|
return self::T_SHORTCODE; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
225 |
|
if (\preg_match(self::EMOTICON_REGEX, $value)) { |
|
114
|
36 |
|
return self::T_EMOTICON; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
225 |
|
if (\preg_match(self::CODEPOINT_EMOJI_LOOSE_REGEX, $value)) { |
|
118
|
60 |
|
return self::T_UNICODE; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
222 |
|
if (\preg_match(self::HTML_ENTITY_REGEX, $value)) { |
|
122
|
33 |
|
return self::T_HTML_ENTITY; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
222 |
|
return self::T_TEXT; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritDoc} |
|
130
|
|
|
*/ |
|
131
|
243 |
|
public function setInput($input): void |
|
132
|
|
|
{ |
|
133
|
243 |
|
if (! \mb_check_encoding($input, 'UTF-8')) { |
|
134
|
3 |
|
throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
240 |
|
parent::setInput($input); |
|
138
|
240 |
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|