Passed
Push — master ( aba266...2d9b02 )
by Adam
09:17
created

Smilies::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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