Test Setup Failed
Pull Request — latest (#3)
by Mark
34:19
created

EmojiLexer::getNonCatchablePatterns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji\Lexer;
6
7
use Doctrine\Common\Lexer\AbstractLexer;
8
use UnicornFail\Emoji\Emojibase\EmojibaseRegexInterface;
9
use UnicornFail\Emoji\Environment\EnvironmentInterface;
10
use UnicornFail\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
    public function __construct(EnvironmentInterface $environment)
36
    {
37
        $this->environment = $environment;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     *
43
     * @return string[]
44
     */
45
    protected function getCatchablePatterns()
46
    {
47
        $config = $this->environment->getConfiguration();
48
49
        $patterns = [];
50
51
        if ($config->get('convert.unicode')) {
52
            $patterns[] = self::CODEPOINT_EMOJI_LOOSE_REGEX;
53
        }
54
55
        if ($config->get('convert.htmlEntity')) {
56
            $patterns[] = self::HTML_ENTITY_REGEX;
57
        }
58
59
        if ($config->get('convert.shortcode')) {
60
            $patterns[] = $this->environment->getRuntimeDataset()->isNative()
61
                ? self::SHORTCODE_NATIVE_REGEX
62
                : self::SHORTCODE_REGEX;
63
        }
64
65
        if ($config->get('convert.emoticon')) {
66
            $patterns[] = self::EMOTICON_REGEX;
67
        }
68
69
        return static::cleanPatterns($patterns);
70
    }
71
72
    /**
73
     * @param string[] $patterns
74
     *
75
     * @return string[]
76
     */
77
    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
        foreach ($patterns as &$pattern) {
82
            $pattern = \trim(\rtrim($pattern, 'imsxeADSUXJu'), '/');
83
        }
84
85
        return $patterns;
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     *
91
     * @return string[]
92
     */
93
    protected function getNonCatchablePatterns()
94
    {
95
        return [];
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     *
101
     * @return int
102
     *
103
     * @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection
104
     */
105
    protected function getType(&$value)
106
    {
107
        if (\preg_match($this->environment->getRuntimeDataset()->isNative() ? self::SHORTCODE_NATIVE_REGEX : self::SHORTCODE_REGEX, $value)) {
108
            return self::T_SHORTCODE;
109
        }
110
111
        if (\preg_match(self::EMOTICON_REGEX, $value)) {
112
            return self::T_EMOTICON;
113
        }
114
115
        if (\preg_match(self::CODEPOINT_EMOJI_LOOSE_REGEX, $value)) {
116
            return self::T_UNICODE;
117
        }
118
119
        if (\preg_match(self::HTML_ENTITY_REGEX, $value)) {
120
            return self::T_HTML_ENTITY;
121
        }
122
123
        return self::T_TEXT;
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129
    public function setInput($input): void
130
    {
131
        if (! \mb_check_encoding($input, 'UTF-8')) {
132
            throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected');
133
        }
134
135
        parent::setInput($input);
136
    }
137
}
138