1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace UnicornFail\Emoji\Parser; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Lexer\AbstractLexer; |
8
|
|
|
use UnicornFail\Emoji\Emojibase\RegexInterface; |
9
|
|
|
use UnicornFail\Emoji\Environment\EmojiEnvironmentInterface; |
10
|
|
|
|
11
|
|
|
class Lexer extends AbstractLexer implements RegexInterface |
12
|
|
|
{ |
13
|
|
|
public const T_TEXT = 0; |
14
|
|
|
|
15
|
|
|
public const T_EMOTICON = 1; |
16
|
|
|
|
17
|
|
|
public const T_HTML_ENTITY = 2; |
18
|
|
|
|
19
|
|
|
public const T_SHORTCODE = 3; |
20
|
|
|
|
21
|
|
|
public const T_UNICODE = 4; |
22
|
|
|
|
23
|
|
|
public const TEXT = 'text'; |
24
|
|
|
public const EMOTICON = 'emoticon'; |
25
|
|
|
public const HTML_ENTITY = 'html_entity'; |
26
|
|
|
public const SHORTCODE = 'shortcode'; |
27
|
|
|
public const UNICODE = 'unicode'; |
28
|
|
|
|
29
|
|
|
public const TYPES = [ |
30
|
|
|
self::T_TEXT => self::TEXT, |
31
|
|
|
self::T_EMOTICON => self::EMOTICON, |
32
|
|
|
self::T_HTML_ENTITY => self::HTML_ENTITY, |
33
|
|
|
self::T_SHORTCODE => self::SHORTCODE, |
34
|
|
|
self::T_UNICODE => self::UNICODE, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** @var EmojiEnvironmentInterface */ |
38
|
|
|
private $environment; |
39
|
|
|
|
40
|
|
|
public function __construct(EmojiEnvironmentInterface $environment) |
41
|
|
|
{ |
42
|
|
|
$this->environment = $environment; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
* |
48
|
|
|
* @return string[] |
49
|
|
|
*/ |
50
|
|
|
protected function getCatchablePatterns() |
51
|
|
|
{ |
52
|
|
|
$patterns = [ |
53
|
|
|
self::CODEPOINT_EMOJI_LOOSE_REGEX, |
54
|
|
|
self::HTML_ENTITY_REGEX, |
55
|
|
|
$this->environment->getConfiguration()->get('native') ? self::SHORTCODE_NATIVE_REGEX : self::SHORTCODE_REGEX, |
56
|
|
|
]; |
57
|
|
|
if ($this->environment->getConfiguration()->get('convertEmoticons')) { |
58
|
|
|
$patterns[] = self::EMOTICON_REGEX; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Some regex patterns from the constants include the delimiter and modifiers. Because the |
62
|
|
|
// lexer joins these expressions together as an OR group (|), they must be removed. |
63
|
|
|
foreach ($patterns as &$pattern) { |
64
|
|
|
$pattern = \trim(\rtrim($pattern, 'imsxeADSUXJu'), '/'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $patterns; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
* |
73
|
|
|
* @return string[] |
74
|
|
|
*/ |
75
|
|
|
protected function getNonCatchablePatterns() |
76
|
|
|
{ |
77
|
|
|
return []; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
* |
85
|
|
|
* @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection |
86
|
|
|
*/ |
87
|
|
|
protected function getType(&$value) |
88
|
|
|
{ |
89
|
|
|
if (\preg_match(self::HTML_ENTITY_REGEX, $value)) { |
90
|
|
|
return self::T_HTML_ENTITY; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// @phpstan-ignore-next-line |
94
|
|
|
if (\preg_match($this->environment->getConfiguration()->get('native') ? self::SHORTCODE_NATIVE_REGEX : self::SHORTCODE_REGEX, $value)) { |
95
|
|
|
return self::T_SHORTCODE; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (\preg_match(self::EMOTICON_REGEX, $value)) { |
99
|
|
|
return self::T_EMOTICON; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (\preg_match(self::CODEPOINT_EMOJI_LOOSE_REGEX, $value)) { |
103
|
|
|
return self::T_UNICODE; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return self::T_TEXT; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|