Passed
Push — main ( b205bb...1d3d49 )
by Mark
12:04
created

EmojiCoreProcessor::getEmojiContent()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 6
nop 1
dl 0
loc 26
ccs 12
cts 12
cp 1
crap 6
rs 9.1111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Emoji\Extension;
6
7
use League\Configuration\ConfigurationAwareInterface;
8
use League\Configuration\ConfigurationInterface;
9
use League\Emoji\EmojiConverterInterface;
10
use League\Emoji\Emojibase\EmojibaseDatasetInterface;
11
use League\Emoji\Event\DocumentParsedEvent;
12
use League\Emoji\Lexer\EmojiLexer;
13
use League\Emoji\Node\Emoji;
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 240
    public function __invoke(DocumentParsedEvent $e): void
29
    {
30
        // Ensure emoji content is set to the correct conversion type.
31 240
        foreach ($e->getDocument()->getNodes() as $node) {
32 237
            if (! ($node instanceof Emoji)) {
33 222
                continue;
34
            }
35
36 99
            $content = $this->getEmojiContent($node);
37 99
            if ($content !== null) {
38 99
                $node->setContent($content);
39
            }
40
        }
41 240
    }
42
43 99
    protected function getConversionType(Emoji $emoji): ?int
44
    {
45 99
        $type       = $emoji->getParsedType();
46 99
        $configPath = 'convert.' . (EmojiConverterInterface::TYPES[$type] ?? '');
47
48 99
        if ($this->config->exists($configPath) && ($configType = (string) ($this->config->get($configPath) ?? ''))) {
49 99
            $index = \array_search($configType, EmojiConverterInterface::TYPES, true);
50 99
            if (\is_int($index)) {
51 87
                return $index;
52
            }
53
        }
54
55 18
        return null;
56
    }
57
58 99
    protected function getEmojiContent(Emoji $emoji): ?string
59
    {
60 99
        $content = null;
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 99
        switch ($this->getConversionType($emoji)) {
65
            case EmojiLexer::T_EMOTICON:
66 3
                $content = $emoji->emoticon;
67 3
                break;
68
69
            case EmojiLexer::T_HTML_ENTITY:
70 6
                $content = $emoji->htmlEntity;
71 6
                break;
72
73
            case EmojiLexer::T_SHORTCODE:
74 36
                $content = $emoji->getShortcode((array) $this->config->get('exclude/shortcodes'), true);
75 36
                break;
76
77
            case EmojiLexer::T_TEXT:
78
            case EmojiLexer::T_UNICODE:
79 63
                $content = $this->getEmojiUnicode($emoji);
80 63
                break;
81
        }
82
83 99
        return $content;
84
    }
85
86 63
    protected function getEmojiUnicode(Emoji $emoji): ?string
87
    {
88 63
        if (($this->config->get('presentation') ?? $emoji->type) === EmojibaseDatasetInterface::TEXT && $emoji->text) {
89 6
            return $emoji->text;
90
        }
91
92 57
        return $emoji->emoji ?? $emoji->unicode;
93
    }
94
95 651
    public function setConfiguration(ConfigurationInterface $configuration): void
96
    {
97 651
        $this->config = $configuration;
98 651
    }
99
}
100