Passed
Push — feature/6.x ( 53903f...32797a )
by Schlaefer
05:38 queued 02:15
created

JbbCodeTextVisitor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

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