xref: /dokuwiki/inc/Parsing/ParserMode/Entity.php (revision 504c13e8df88563c11b3720b317991bc38835a35)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5use dokuwiki\Parsing\Handler;
6use dokuwiki\Parsing\Lexer\Lexer;
7
8class Entity extends AbstractMode
9{
10    protected $entities = [];
11    protected $pattern = '';
12
13    /**
14     * Entity constructor.
15     * @param string[] $entities
16     */
17    public function __construct($entities)
18    {
19        $this->entities = $entities;
20    }
21
22    /** @inheritdoc */
23    public function getSort()
24    {
25        return 260;
26    }
27
28    /** @inheritdoc */
29    public function preConnect()
30    {
31        if (!count($this->entities) || $this->pattern != '') return;
32
33        $sep = '';
34        foreach ($this->entities as $entity) {
35            $this->pattern .= $sep . Lexer::escape($entity);
36            $sep = '|';
37        }
38    }
39
40    /** @inheritdoc */
41    public function connectTo($mode)
42    {
43        if (!count($this->entities)) return;
44
45        if ((string) $this->pattern !== '') {
46            $this->Lexer->addSpecialPattern($this->pattern, $mode, 'entity');
47        }
48    }
49
50    /** @inheritdoc */
51    public function handle($match, $state, $pos, Handler $handler)
52    {
53        $handler->addCall('entity', [$match], $pos);
54        return true;
55    }
56}
57