Passed
Pull Request — master (#650)
by Daniel
11:47
created

Smilies   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 1
1
<?php
2
3
namespace Coyote\Services\Parser\Parsers;
4
5
use TRegx\CleanRegex\Match\Details\Detail;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_MATCH, expecting T_STRING or '{' on line 5 at column 21
Loading history...
6
use TRegx\CleanRegex\Pattern;
7
8
/**
9
 * Class Smilies
10
 */
11
class Smilies extends Parser implements ParserInterface
12
{
13
    private $smilies = [
14
        ':)'        => 'smile.gif',
15
        ':-)'       => 'smile.gif',
16
        ';)'        => 'wink.gif',
17
        ';-)'       => 'wink.gif',
18
        ':-|'       => 'neutral.gif',
19
        ':D'        => 'laugh.gif',
20
        ':-D'       => 'laugh.gif',
21
        ':('        => 'sad.gif',
22
        ':-('       => 'sad.gif',
23
        ':P'        => 'tongue1.gif',
24
        ':p'        => 'tongue1.gif',
25
        ':-P'       => 'tongue1.gif',
26
        ':-/'       => 'confused.gif',
27
        ':/'        => 'damn.gif',
28
        ':['        => 'mad.gif',
29
        ':-['       => 'mad.gif',
30
        ':|'        => 'zonk.gif',
31
        ':]'        => 'squared.gif',
32
        ':d'        => 'teeth.gif'
33
    ];
34
35
    /**
36
     * @param string $text
37
     * @return string
38
     */
39
    public function parse($text)
40
    {
41
        $text = $this->hashBlock($text, ['code', 'a']);
42
        $text = $this->hashInline($text, 'img');
43
44
        $text = Pattern::template('(?<=^|[\n \>]|\.)(@)')
45
            ->alteration(array_keys($this->smilies))
46
            ->build()
47
            ->replace($text)
48
            ->callback(function (Detail $match) {
49
                $smiley = $match->get(1);
50
                $link = $this->smilies[$smiley];
51
                return '<img class="img-smile" alt="' . $smiley . '" title="' . $smiley . '" src="/img/smilies/' . $link . '">';
52
            });
53
54
        $text = $this->unhash($text);
55
56
        return $text;
57
    }
58
}
59