1
|
|
|
<?php |
2
|
|
|
namespace Dallgoot\Yaml; |
|
|
|
|
3
|
|
|
|
4
|
|
|
use Dallgoot\Yaml\{Types as T, Regex as R}; |
5
|
|
|
use \SplDoublyLinkedList as DLL; |
6
|
|
|
|
7
|
|
|
class Node |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** @var int */ |
|
|
|
|
10
|
|
|
public $indent = -1; |
11
|
|
|
/** @var int */ |
|
|
|
|
12
|
|
|
public $line; |
13
|
|
|
/** @var int */ |
|
|
|
|
14
|
|
|
public $type; |
15
|
|
|
/** @var null|string|boolean */ |
|
|
|
|
16
|
|
|
public $identifier; |
17
|
|
|
/** @var Node|DLL|null|string */ |
|
|
|
|
18
|
|
|
public $value; |
19
|
|
|
|
20
|
|
|
/** @var null|Node */ |
|
|
|
|
21
|
|
|
private $parent; |
|
|
|
|
22
|
|
|
|
23
|
|
|
public function __construct($nodeString = null, $line = null) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->line = $line; |
26
|
|
|
if (is_null($nodeString)) { |
27
|
|
|
$this->type = T::ROOT; |
28
|
|
|
} else { |
29
|
|
|
$this->parse($nodeString); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setParent(Node $node):Node |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this->parent = $node; |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getParent($indent = null):Node |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
if (is_null($indent)) return $this->parent ?? $this; |
|
|
|
|
42
|
|
|
$cursor = $this; |
43
|
|
|
while ($cursor->indent >= $indent) { |
44
|
|
|
$cursor = $cursor->parent; |
45
|
|
|
} |
46
|
|
|
return $cursor; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function add(Node $child):void |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$child->setParent($this); |
52
|
|
|
$current = $this->value; |
53
|
|
|
if (in_array($this->type, T::$LITTERALS)) $child->type = T::SCALAR; |
|
|
|
|
54
|
|
|
if (is_null($current)) { |
55
|
|
|
$this->value = $child; |
56
|
|
|
return; |
57
|
|
|
} elseif ($current instanceof Node) { |
58
|
|
|
$this->value = new DLL(); |
59
|
|
|
$this->value->setIteratorMode(DLL::IT_MODE_KEEP); |
60
|
|
|
$this->value->push($current); |
61
|
|
|
} |
62
|
|
|
$this->value->push($child); |
|
|
|
|
63
|
|
|
//modify type according to child |
64
|
|
|
if ($this->value instanceof DLL && !property_exists($this->value, "type")) { |
65
|
|
|
switch ($child->type) { |
66
|
|
|
case T::KEY: $this->value->type = T::MAPPING;break; |
|
|
|
|
67
|
|
|
case T::ITEM: $this->value->type = T::SEQUENCE;break; |
|
|
|
|
68
|
|
|
case T::SCALAR: $this->value->type = $this->type;break; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getDeepestNode():Node |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$cursor = $this; |
76
|
|
|
while ($cursor->value instanceof Node) { |
77
|
|
|
$cursor = $cursor->value; |
78
|
|
|
} |
79
|
|
|
return $cursor; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* CAUTION : the types assumed here are NOT FINAL : they CAN be adjusted according to parent |
84
|
|
|
*/ |
|
|
|
|
85
|
|
|
public function parse(String $nodeString):Node |
86
|
|
|
{ |
87
|
|
|
$nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement |
88
|
|
|
$this->indent = strspn($nodeValue, ' '); |
89
|
|
|
$nodeValue = ltrim($nodeValue); |
90
|
|
|
if ($nodeValue === '') { |
91
|
|
|
$this->type = T::EMPTY; |
92
|
|
|
// $this->indent = 0; // remove if no bugs |
93
|
|
|
} elseif (substr($nodeValue, 0, 3) === '...') {//TODO: can have something on same line ? |
94
|
|
|
$this->type = T::DOC_END; |
95
|
|
|
} elseif (preg_match(R::KEY, $nodeValue, $matches)) { |
96
|
|
|
$this->onKey($matches); |
97
|
|
|
} else {//NOTE: can be of another type according to parent |
98
|
|
|
list($this->type, $value) = $this->define($nodeValue); |
99
|
|
|
is_object($value) ? $this->add($value) : $this->value = $value; |
100
|
|
|
} |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set the type and value according to first character |
106
|
|
|
* |
107
|
|
|
* @param string $nodeValue The node value |
|
|
|
|
108
|
|
|
* @return array contains [node->type, node->value] |
|
|
|
|
109
|
|
|
*/ |
110
|
|
|
private function define($nodeValue):array |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$v = substr($nodeValue, 1); |
113
|
|
|
if (in_array($nodeValue[0], ['"', "'"])) { |
114
|
|
|
$type = R::isProperlyQuoted($nodeValue) ? T::QUOTED : T::PARTIAL; |
115
|
|
|
return [$type, $nodeValue]; |
116
|
|
|
} |
117
|
|
|
if (in_array($nodeValue[0], ['{', '['])) return $this->onObject($nodeValue); |
|
|
|
|
118
|
|
|
if (in_array($nodeValue[0], ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
|
|
|
119
|
|
|
switch ($nodeValue[0]) { |
120
|
|
|
case '#': return [T::COMMENT, ltrim($v)]; |
|
|
|
|
121
|
|
|
case "-": return $this->onHyphen($nodeValue); |
|
|
|
|
122
|
|
|
case '%': return [T::DIRECTIVE, ltrim($v)]; |
|
|
|
|
123
|
|
|
case '?': return [T::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
|
|
|
124
|
|
|
case ':': return [T::SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
|
|
|
125
|
|
|
case '>': return [T::LITTERAL_FOLDED, null]; |
|
|
|
|
126
|
|
|
case '|': return [T::LITTERAL, null]; |
|
|
|
|
127
|
|
|
default: |
|
|
|
|
128
|
|
|
return [T::SCALAR, $nodeValue]; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function onKey($matches):void |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$this->type = T::KEY; |
135
|
|
|
$this->identifier = trim($matches[1]); |
136
|
|
|
$keyValue = isset($matches[2]) ? trim($matches[2]) : null; |
137
|
|
|
if (!empty($keyValue)) { |
138
|
|
|
$n = new Node($keyValue, $this->line); |
139
|
|
|
$hasComment = strpos($keyValue, ' #'); |
140
|
|
|
if (!is_bool($hasComment)) { |
|
|
|
|
141
|
|
|
$tmpNode = new Node(trim(substr($keyValue, 0, $hasComment)), $this->line); |
142
|
|
|
if ($tmpNode->type !== T::PARTIAL) { |
143
|
|
|
$comment = new Node(trim(substr($keyValue, $hasComment+1)), $this->line); |
144
|
|
|
//TODO: modify "identifier" to specify if fullline comment or not |
145
|
|
|
$this->add($comment); |
146
|
|
|
$n = $tmpNode; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
$n->indent = $this->indent + strlen($this->identifier); |
150
|
|
|
$this->add($n); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function onObject($value):array |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
157
|
|
|
if (json_last_error() === JSON_ERROR_NONE) return [T::JSON, $value]; |
|
|
|
|
158
|
|
|
if (preg_match(R::MAPPING, $value)) return [T::MAPPING_SHORT, $value]; |
|
|
|
|
159
|
|
|
if (preg_match(R::SEQUENCE, $value)) return [T::SEQUENCE_SHORT, $value]; |
|
|
|
|
160
|
|
|
return [T::PARTIAL, $value]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function onHyphen($nodeValue):array |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
if (substr($nodeValue, 0, 3) === '---') { |
166
|
|
|
$rest = trim(substr($nodeValue, 3)); |
167
|
|
|
if (empty($rest)) return [T::DOC_START, null]; |
|
|
|
|
168
|
|
|
$n = new Node($rest, $this->line); |
169
|
|
|
$n->indent = $this->indent + 4; |
170
|
|
|
return [T::DOC_START, $n->setParent($this)]; |
171
|
|
|
} |
172
|
|
|
if (preg_match(R::ITEM, $nodeValue, $matches)) { |
173
|
|
|
if (isset($matches[1]) && !empty(trim($matches[1]))) { |
174
|
|
|
$n = new Node(trim($matches[1]), $this->line); |
175
|
|
|
return [T::ITEM, $n->setParent($this)]; |
176
|
|
|
} |
177
|
|
|
return [T::ITEM, null]; |
178
|
|
|
} |
179
|
|
|
return [T::SCALAR, $nodeValue]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
private function onNodeAction($nodeValue):array |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
// TODO: handle tags like <tag:clarkevans.com,2002:invoice> |
185
|
|
|
$v = substr($nodeValue, 1); |
186
|
|
|
$type = ['!' => T::TAG, '&' => T::REF_DEF, '*' => T::REF_CALL][$nodeValue[0]]; |
187
|
|
|
$pos = strpos($v, ' '); |
188
|
|
|
$this->identifier = is_bool($pos) ? $v : strstr($v, ' ', true); |
|
|
|
|
189
|
|
|
$n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos+1)), $this->line))->setParent($this); |
|
|
|
|
190
|
|
|
return [$type, $n]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getPhpValue() |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$v = $this->value; |
196
|
|
|
if (is_null($v)) return null; |
|
|
|
|
197
|
|
|
switch ($this->type) { |
198
|
|
|
case T::JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
|
|
|
|
199
|
|
|
case T::QUOTED: return substr($v, 1, -1); |
|
|
|
|
200
|
|
|
case T::RAW: return strval($v); |
|
|
|
|
201
|
|
|
case T::REF_CALL://fall through |
|
|
|
|
202
|
|
|
case T::SCALAR: return self::getScalar($v); |
|
|
|
|
203
|
|
|
case T::MAPPING_SHORT: return self::getShortMapping(substr($v, 1, -1)); |
|
|
|
|
204
|
|
|
//TODO : that's not robust enough, improve it |
205
|
|
|
case T::SEQUENCE_SHORT: |
|
|
|
|
206
|
|
|
$f = function ($e) { return self::getScalar(trim($e));}; |
|
|
|
|
207
|
|
|
return array_map($f, explode(",", substr($v, 1, -1))); |
208
|
|
|
default: |
|
|
|
|
209
|
|
|
trigger_error("Error can not get PHP type for ".T::getName($this->type), E_USER_WARNING); |
210
|
|
|
return null; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
private static function getScalar($v) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
$types = ['yes' => true, |
217
|
|
|
'no' => false, |
218
|
|
|
'true' => true, |
219
|
|
|
'false' => false, |
220
|
|
|
'null' => null, |
221
|
|
|
'.inf' => INF, |
222
|
|
|
'-.inf' => -INF, |
223
|
|
|
'.nan' => NAN |
224
|
|
|
]; |
225
|
|
|
if (in_array(strtolower($v), array_keys($types))) return $types[strtolower($v)]; |
|
|
|
|
226
|
|
|
if (R::isDate($v)) return date_create($v); |
|
|
|
|
227
|
|
|
if (R::isNumber($v)) return self::getNumber($v); |
|
|
|
|
228
|
|
|
return strval($v); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
private static function getNumber($v) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
|
|
|
234
|
|
|
if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
|
|
|
235
|
|
|
// if preg_match("/^([\d.]+e[-+]\d{1,2})$/", $v)://fall through |
236
|
|
|
// if preg_match("/^([-+]?(?:\d+|\d*.\d+))$/", $v): |
237
|
|
|
return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
//TODO : that's not robust enough, improve it |
241
|
|
|
private static function getShortMapping($mappingString):object |
|
|
|
|
242
|
|
|
{ |
243
|
|
|
$out = new \StdClass(); |
244
|
|
|
foreach (explode(',', $mappingString) as $value) { |
245
|
|
|
list($keyName, $keyValue) = explode(':', $value); |
246
|
|
|
$out->{trim($keyName)} = self::getScalar(trim($keyValue)); |
247
|
|
|
} |
248
|
|
|
return $out; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function __debugInfo():array |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
$out = ['line' => $this->line, |
254
|
|
|
'indent'=> $this->indent, |
255
|
|
|
'type' => T::getName($this->type), |
256
|
|
|
'value' => $this->value]; |
257
|
|
|
if (!is_null($this->identifier)) $out['type'] .= "($this->identifier)"; |
|
|
|
|
258
|
|
|
return $out; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|