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