Token   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 60
dl 0
loc 138
ccs 57
cts 57
cp 1
rs 10
c 1
b 1
f 0
wmc 18

8 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 15 4
A __construct() 0 9 1
A jsonSerialize() 0 3 1
A createXmlNode() 0 18 5
A __get() 0 13 3
A toXml() 0 7 1
A toArray() 0 17 2
A __toString() 0 3 1
1
<?php
2
3
namespace Vanderlee\Comprehend\Core;
4
5
use DOMDocument;
6
use DOMElement;
7
use DOMException;
8
use Exception;
9
use JsonSerializable;
10
11
/**
12
 * Class Token.
13
 *
14
 *
15
 * @property-read Token[] $children
16
 * @property-read string $class
17
 * @property-read string $group
18
 * @property-read string $input
19
 * @property-read int $length
20
 * @property-read int $offset
21
 * @property-read string $name
22
 * @property-read string $text
23
 */
24
class Token implements JsonSerializable
25
{
26
    private $group;
27
    private $name;
28
    private $input;
29
    private $offset;
30
    private $length;
31
    private $class;
32
33
    /**
34
     * @var Token[]
35
     */
36
    private $children;
37
38 8
    public function __construct($group, $name, $input, int $offset, $length, $children = [], $class = null)
39
    {
40 8
        $this->group = $group;
0 ignored issues
show
Bug introduced by
The property group is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
41 8
        $this->name = $name;
0 ignored issues
show
Bug introduced by
The property name is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
42 8
        $this->input = $input;
0 ignored issues
show
Bug introduced by
The property input is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
43 8
        $this->offset = &$offset;
0 ignored issues
show
Bug introduced by
The property offset is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
44 8
        $this->length = $length;
0 ignored issues
show
Bug introduced by
The property length is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
45 8
        $this->children = $children;
0 ignored issues
show
Bug introduced by
The property children is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
46 8
        $this->class = $class;
0 ignored issues
show
Bug introduced by
The property class is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
47 8
    }
48
49
    /**
50
     * @throws Exception
51
     */
52 8
    public function __get($name)
53
    {
54
        switch ($name) {
55 8
            case 'text':
56 6
                return substr($this->input, $this->offset, $this->length);
57
58
            default:
59 3
                if (property_exists($this, $name)) {
60 2
                    return $this->$name;
61
                }
62
        }
63
64 1
        throw new Exception('Undefined property `' . $name . '`');
65
    }
66
67 1
    private function toString($depth = 0)
68
    {
69 1
        $signature = ($this->group
70 1
                ? $this->group . '::'
71 1
                : '')
72 1
            . ($this->name
73 1
                ?: $this->class);
74
75 1
        $output = str_repeat('  ', $depth) . $signature . ' (`' . $this->text . '`)';
76
77 1
        foreach ($this->children as $child) {
78 1
            $output .= PHP_EOL . $child->toString($depth + 1);
79
        }
80
81 1
        return $output;
82
    }
83
84 1
    public function __toString()
85
    {
86 1
        return $this->toString();
87
    }
88
89
    /**
90
     * @return DOMDocument
91
     * @throws DOMException
92
     */
93 1
    public function toXml()
94
    {
95 1
        $document = new DOMDocument();
96 1
        $document->appendChild($this->createXmlNode($document));
97 1
        $document->normalizeDocument();
98
99 1
        return $document;
100
    }
101
102
    /**
103
     * Create an XML node of this token.
104
     *
105
     * @param DOMDocument $document
106
     *
107
     * @return DOMElement
108
     *
109
     * @throws DOMException
110
     */
111 1
    private function createXmlNode(DOMDocument $document)
112
    {
113 1
        $value = $this->children
114 1
            ? null
115 1
            : $this->text;
116 1
        $name = preg_replace('/[^-_a-zA-Z0-9]/', '_', $this->name
117 1
            ?: $this->class);
118 1
        $group = preg_replace('/[^-_a-zA-Z0-9]/', '_', $this->group);
119
120 1
        $element = $this->group
121 1
            ? $document->createElementNS($this->group, $group . ':' . $name, $value)
122 1
            : $document->createElement($name, $value);
123
124 1
        foreach ($this->children as $child) {
125 1
            $element->appendChild($child->createXmlNode($document));
126
        }
127
128 1
        return $element;
129
    }
130
131
    /**
132
     * Convert object to JSON for json_encode
133
     * Implements /JsonSerializable interface.
134
     */
135 1
    public function jsonSerialize()
136
    {
137 1
        return $this->toArray();
138
    }
139
140
    /**
141
     * Output this node as an array.
142
     *
143
     * @return array
144
     */
145 3
    public function toArray()
146
    {
147
        $array = [
148 3
            'group' => $this->group,
149 3
            'name' => $this->name,
150 3
            'text' => $this->text,
151 3
            'offset' => $this->offset,
152 3
            'length' => $this->length,
153 3
            'class' => $this->class,
154
            'children' => [],
155
        ];
156
157 3
        foreach ($this->children as $child) {
158 2
            $array['children'][] = $child->toArray();
159
        }
160
161 3
        return $array;
162
    }
163
}
164