1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5use dokuwiki\Parsing\Lexer\Lexer;
6
7class Smiley extends AbstractMode
8{
9    protected $smileys = [];
10    protected $pattern = '';
11
12    /**
13     * Smiley constructor.
14     * @param string[] $smileys
15     */
16    public function __construct($smileys)
17    {
18        $this->smileys = $smileys;
19    }
20
21    /** @inheritdoc */
22    public function preConnect()
23    {
24        if (!count($this->smileys) || $this->pattern != '') return;
25
26        $sep = '';
27        foreach ($this->smileys as $smiley) {
28            $this->pattern .= $sep . '(?<=\W|^)' . Lexer::escape($smiley) . '(?=\W|$)';
29            $sep = '|';
30        }
31    }
32
33    /** @inheritdoc */
34    public function connectTo($mode)
35    {
36        if (!count($this->smileys)) return;
37
38        if (strlen($this->pattern) > 0) {
39            $this->Lexer->addSpecialPattern($this->pattern, $mode, 'smiley');
40        }
41    }
42
43    /** @inheritdoc */
44    public function getSort()
45    {
46        return 230;
47    }
48}
49