JbbCodeTextVisitor   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 80
rs 10
c 1
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A visitTextNode() 0 4 1
A visitDocumentElement() 0 5 2
A __get() 0 4 2
A visitElementNode() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Plugin\BbcodeParser\src\Lib\jBBCode\Visitors;
14
15
use Cake\View\Helper;
16
use JBBCode\NodeVisitor;
17
use Saito\Markup\MarkupSettings;
18
19
abstract class JbbCodeTextVisitor implements NodeVisitor
20
{
21
22
    protected $_disallowedTags = ['code'];
23
24
    /**
25
     * @var Helper calling CakePHP helper
26
     */
27
    protected $_sHelper;
28
29
    protected $_sOptions;
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function __construct(Helper $Helper, MarkupSettings $_sOptions)
35
    {
36
        $this->_sOptions = $_sOptions;
37
        $this->_sHelper = $Helper;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function __get($name)
44
    {
45
        if (is_object($this->_sHelper->$name)) {
46
            return $this->_sHelper->{$name};
47
        }
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function visitDocumentElement(
54
        \JBBCode\DocumentElement $documentElement
55
    ) {
56
        foreach ($documentElement->getChildren() as $child) {
57
            $child->accept($this);
58
        }
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function visitTextNode(\JBBCode\TextNode $textNode)
65
    {
66
        $textNode->setValue(
67
            $this->_processTextNode($textNode->getValue(), $textNode)
68
        );
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function visitElementNode(\JBBCode\ElementNode $elementNode)
75
    {
76
        $tagName = $elementNode->getTagName();
77
        if (in_array($tagName, $this->_disallowedTags)) {
78
            return;
79
        }
80
81
        /* We only want to visit text nodes within elements if the element's
82
         * code definition allows for its content to be parsed.
83
         */
84
        $isParsedContentNode = $elementNode->getCodeDefinition()->parseContent(
85
        );
86
        if (!$isParsedContentNode) {
87
            return;
88
        }
89
90
        foreach ($elementNode->getChildren() as $child) {
91
            $child->accept($this);
92
        }
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    abstract protected function _processTextNode($text, $textNode);
99
}
100