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; |
|
|
|
|
41
|
8 |
|
$this->name = $name; |
|
|
|
|
42
|
8 |
|
$this->input = $input; |
|
|
|
|
43
|
8 |
|
$this->offset = &$offset; |
|
|
|
|
44
|
8 |
|
$this->length = $length; |
|
|
|
|
45
|
8 |
|
$this->children = $children; |
|
|
|
|
46
|
8 |
|
$this->class = $class; |
|
|
|
|
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
|
|
|
|