EmojiParser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 35
dl 0
loc 102
rs 10
c 1
b 0
f 1
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMatchDefinition() 0 3 1
B parse() 0 50 7
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Markdown\Emoji;
6
7
use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
8
use League\CommonMark\Parser\Inline\InlineParserInterface;
9
use League\CommonMark\Parser\InlineParserContext;
10
use League\CommonMark\Parser\Inline\InlineParserMatch;
11
12
class EmojiParser implements InlineParserInterface
13
{
14
    /**
15
     * The emoji mappings.
16
     *
17
     * @var array
18
     */
19
    protected $map;
20
21
    /**
22
     * The emoji extension.
23
     *
24
     * @var string
25
     */
26
    protected $ext = '.png';
27
28
    /**
29
     * The emoji path directory.
30
     *
31
     * @var string
32
     */
33
    protected $path;
34
35
    /**
36
     * Create a new emoji parser instance.
37
     *
38
     * @return void
39
     */
40
    public function __construct()
41
    {
42
        $this->map = include __DIR__ . "/EmojiSheet.php";
43
        $this->path = config('app.url') . '/images/emojis/';
44
    }
45
46
    /**
47
     * Get the characters that must be matched.
48
     *
49
     * @return array
50
     */
51
    public function getMatchDefinition(): InlineParserMatch
52
    {
53
        return InlineParserMatch::string(':');
54
    }
55
56
    /**
57
     * Parse a line and determine if it contains an emoji. If it does,
58
     * then we do the necessary.
59
     *
60
     * @param \League\CommonMark\InlineParserContext $inlineContext
0 ignored issues
show
Bug introduced by
The type League\CommonMark\InlineParserContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
61
     *
62
     * @return bool
63
     */
64
    public function parse(InlineParserContext $inlineContext): bool
65
    {
66
        $cursor = $inlineContext->getCursor();
67
68
        $previous = $cursor->peek(-1);
69
        if ($previous !== null && $previous !== ' ') {
70
            return false;
71
        }
72
73
        $saved = $cursor->saveState();
74
75
        $cursor->advance();
76
77
        $handle = $cursor->match('/^[a-z0-9\+\-_]+:/');
78
79
        if (!$handle) {
80
            $cursor->restoreState($saved);
81
82
            return false;
83
        }
84
85
        $next = $cursor->peek(0);
86
87
        if ($next !== null && $next !== ' ') {
88
            $cursor->restoreState($saved);
89
90
            return false;
91
        }
92
93
        $key = mb_substr($handle, 0, -1);
94
95
        if (!in_array($key, $this->map)) {
96
            $cursor->restoreState($saved);
97
98
            return false;
99
        }
100
101
        $fileName = $this->path . $key . $this->ext;
102
103
        $inline = new Image($fileName, $key);
104
        $inline->data['attributes'] = [
105
            'class' => 'emoji',
106
            'data-emoji' => $key,
107
            'width' => '24',
108
            'height' => '24',
109
            'title' => ':' . $key . ':'
110
        ];
111
        $inlineContext->getContainer()->appendChild($inline);
112
113
        return true;
114
    }
115
}
116