1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml; |
4
|
|
|
|
5
|
|
|
use Dallgoot\Yaml as Y; |
6
|
|
|
use Dallgoot\Yaml\Regex as R; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class for node. |
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
final class Node |
12
|
|
|
{ |
13
|
|
|
/** @var int */ |
|
|
|
|
14
|
|
|
public $indent = -1; |
15
|
|
|
/** @var int */ |
|
|
|
|
16
|
|
|
public $line; |
17
|
|
|
/** @var int */ |
|
|
|
|
18
|
|
|
public $type; |
19
|
|
|
/** @var null|string|boolean */ |
|
|
|
|
20
|
|
|
public $identifier; |
21
|
|
|
/** @var Node|NodeList|null|string */ |
|
|
|
|
22
|
|
|
public $value; |
23
|
|
|
|
24
|
|
|
/** @var null|Node */ |
|
|
|
|
25
|
|
|
private $parent; |
|
|
|
|
26
|
|
|
|
27
|
|
|
public function __construct($nodeString = null, $line = null) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$this->line = $line; |
30
|
|
|
if (is_null($nodeString)) { |
31
|
|
|
$this->type = Y\ROOT; |
|
|
|
|
32
|
|
|
} else { |
33
|
|
|
$this->parse($nodeString); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sets the parent of the current Node |
39
|
|
|
* @param Node $node The node |
|
|
|
|
40
|
|
|
* |
41
|
|
|
* @return Node|self The currentNode |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function setParent(Node $node):Node |
44
|
|
|
{ |
45
|
|
|
$this->parent = $node; |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets the ancestor with specified $indent or the direct $parent OR the current Node itself |
51
|
|
|
* |
52
|
|
|
* @param integer $indent The indent |
|
|
|
|
53
|
|
|
* |
54
|
|
|
* @return Node|self The parent. |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
public function getParent(int $indent = null):Node |
57
|
|
|
{ |
58
|
|
|
if (!is_int($indent)) return $this->parent ?? $this; |
|
|
|
|
59
|
|
|
$cursor = $this; |
60
|
|
|
while ($cursor instanceof Node && $cursor->indent >= $indent) { |
61
|
|
|
$cursor = $cursor->parent; |
62
|
|
|
} |
63
|
|
|
return $cursor; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the value for the current Node : |
68
|
|
|
* - if value is null , then value = $child (Node) |
69
|
|
|
* - if value is Node, then value is a NodeList with (previous value AND $child) |
70
|
|
|
* - if value is a NodeList, simply push $child into |
71
|
|
|
* |
72
|
|
|
* @param Node $child The child |
|
|
|
|
73
|
|
|
*/ |
|
|
|
|
74
|
|
|
public function add(Node $child):void |
75
|
|
|
{ |
76
|
|
|
$child->setParent($this); |
77
|
|
|
$current = $this->value; |
78
|
|
|
if (is_null($current)) { |
79
|
|
|
$this->value = $child; |
80
|
|
|
return; |
81
|
|
|
} else { |
82
|
|
|
if ($current instanceof Node) { |
83
|
|
|
$this->value = new NodeList(); |
84
|
|
|
$this->value->type = Y\LITT_FOLDED; |
|
|
|
|
85
|
|
|
$this->value->setIteratorMode(NodeList::IT_MODE_KEEP); |
86
|
|
|
$this->value->push($current); |
87
|
|
|
} |
88
|
|
|
$this->value->push($child); |
|
|
|
|
89
|
|
|
//modify type according to child |
90
|
|
|
switch ($child->type) { |
91
|
|
|
case Y\COMMENT: //fall through |
|
|
|
|
92
|
|
|
case Y\KEY: $this->value->type = Y\MAPPING;break; |
|
|
|
|
93
|
|
|
case Y\ITEM: $this->value->type = Y\SEQUENCE;break; |
|
|
|
|
94
|
|
|
// case Y\COMMENT: $this->value->type = Y\RAW;break; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
// if ($this->type & Y\LITTERALS) $child->type = Y\SCALAR; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getDeepestNode():Node |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$cursor = $this; |
103
|
|
|
while ($cursor->value instanceof Node) { |
104
|
|
|
$cursor = $cursor->value; |
105
|
|
|
} |
106
|
|
|
return $cursor; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Parses the string (assumed to be a line from a valid YAML) |
111
|
|
|
* |
112
|
|
|
* @param string $nodeString The node string |
|
|
|
|
113
|
|
|
* |
114
|
|
|
* @return Node|self ( description_of_the_return_value ) |
|
|
|
|
115
|
|
|
*/ |
116
|
|
|
public function parse(string $nodeString):Node |
117
|
|
|
{ |
118
|
|
|
$nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement |
119
|
|
|
$this->indent = strspn($nodeValue, ' '); |
120
|
|
|
$nodeValue = ltrim($nodeValue); |
121
|
|
|
if ($nodeValue === '') { |
122
|
|
|
$this->type = Y\BLANK; |
|
|
|
|
123
|
|
|
// $this->indent = 0; // remove if no bugs |
124
|
|
|
} elseif (substr($nodeValue, 0, 3) === '...') {//TODO: can have something on same line ? |
125
|
|
|
$this->type = Y\DOC_END; |
|
|
|
|
126
|
|
|
} elseif (preg_match(R::KEY, $nodeValue, $matches)) { |
127
|
|
|
$this->onKey($matches); |
128
|
|
|
} else {//NOTE: can be of another type according to parent |
129
|
|
|
list($this->type, $value) = $this->define($nodeValue); |
130
|
|
|
is_object($value) ? $this->add($value) : $this->value = $value; |
131
|
|
|
} |
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the type and value according to first character |
137
|
|
|
* |
138
|
|
|
* @param string $nodeValue The node value |
|
|
|
|
139
|
|
|
* @return array contains [node->type, node->value] |
|
|
|
|
140
|
|
|
*/ |
141
|
|
|
private function define($nodeValue):array |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$v = substr($nodeValue, 1); |
144
|
|
|
$first = $nodeValue[0]; |
145
|
|
|
if (in_array($first, ['"', "'"])) { |
146
|
|
|
$type = R::isProperlyQuoted($nodeValue) ? Y\QUOTED : Y\PARTIAL; |
|
|
|
|
147
|
|
|
return [$type, $nodeValue]; |
148
|
|
|
} |
149
|
|
|
if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
|
|
|
|
150
|
|
|
if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
|
|
|
151
|
|
|
switch ($first) { |
152
|
|
|
case '#': return [Y\COMMENT, ltrim($v)]; |
|
|
|
|
153
|
|
|
case "-": return $this->onHyphen($nodeValue); |
|
|
|
|
154
|
|
|
case '%': return [Y\DIRECTIVE, ltrim($v)]; |
|
|
|
|
155
|
|
|
case '?': return [Y\SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
|
|
|
156
|
|
|
case ':': return [Y\SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
|
|
|
157
|
|
|
case '>': return [Y\LITT_FOLDED, null]; |
|
|
|
|
158
|
|
|
case '|': return [Y\LITT, null]; |
|
|
|
|
159
|
|
|
default: |
|
|
|
|
160
|
|
|
return [Y\SCALAR, $nodeValue]; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Process when a "key: value" syntax is found in the parsed string |
166
|
|
|
* Note : key is match 1, value is match 2 as per regex from R::KEY |
167
|
|
|
* @param array $matches The matches provided by 'preg_match' function |
|
|
|
|
168
|
|
|
*/ |
|
|
|
|
169
|
|
|
private function onKey(array $matches):void |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$this->type = Y\KEY; |
|
|
|
|
172
|
|
|
$this->identifier = trim($matches[1]); |
173
|
|
|
$keyValue = isset($matches[2]) ? trim($matches[2]) : null; |
174
|
|
|
if (!empty($keyValue)) { |
175
|
|
|
$n = new Node($keyValue, $this->line); |
176
|
|
|
$hasComment = strpos($keyValue, ' #'); |
177
|
|
|
if (!is_bool($hasComment)) { |
|
|
|
|
178
|
|
|
$tmpNode = new Node(trim(substr($keyValue, 0, $hasComment)), $this->line); |
179
|
|
|
if ($tmpNode->type !== Y\PARTIAL) { |
|
|
|
|
180
|
|
|
$comment = new Node(trim(substr($keyValue, $hasComment+1)), $this->line); |
181
|
|
|
//TODO: modify "identifier" to specify if fullline comment or not |
182
|
|
|
$this->add($comment); |
183
|
|
|
$n = $tmpNode; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
$n->indent = $this->indent + strlen($this->identifier); |
187
|
|
|
$this->add($n); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Determines the correct type and value when a short object/array syntax is found |
193
|
|
|
* |
194
|
|
|
* @param string $value The value assumed to start with { or ( or characters |
|
|
|
|
195
|
|
|
* |
196
|
|
|
* @return array array with the type and $value (unchanged for now) |
|
|
|
|
197
|
|
|
* @see self:define |
|
|
|
|
198
|
|
|
*/ |
199
|
|
|
private function onObject($value):array |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
202
|
|
|
if (json_last_error() === JSON_ERROR_NONE) return [Y\JSON, $value]; |
|
|
|
|
203
|
|
|
if (preg_match(R::MAPPING, $value)) return [Y\COMPACT_MAPPING, $value]; |
|
|
|
|
204
|
|
|
if (preg_match(R::SEQUENCE, $value)) return [Y\COMPACT_SEQUENCE, $value]; |
|
|
|
|
205
|
|
|
return [Y\PARTIAL, $value]; |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Determines type and value when an hyphen "-" is found |
210
|
|
|
* |
211
|
|
|
* @param string $nodeValue The node value |
|
|
|
|
212
|
|
|
* |
213
|
|
|
* @return array array with the type and $value |
|
|
|
|
214
|
|
|
*/ |
215
|
|
|
private function onHyphen($nodeValue):array |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
if (substr($nodeValue, 0, 3) === '---') { |
218
|
|
|
$rest = trim(substr($nodeValue, 3)); |
219
|
|
|
if (empty($rest)) return [Y\DOC_START, null]; |
|
|
|
|
220
|
|
|
$n = new Node($rest, $this->line); |
221
|
|
|
$n->indent = $this->indent + 4; |
222
|
|
|
return [Y\DOC_START, $n->setParent($this)]; |
223
|
|
|
} |
224
|
|
|
if (preg_match(R::ITEM, $nodeValue, $matches)) { |
225
|
|
|
if (isset($matches[1]) && !empty(trim($matches[1]))) { |
226
|
|
|
$n = new Node(trim($matches[1]), $this->line); |
227
|
|
|
return [Y\ITEM, $n->setParent($this)]; |
|
|
|
|
228
|
|
|
} |
229
|
|
|
return [Y\ITEM, null]; |
230
|
|
|
} |
231
|
|
|
return [Y\SCALAR, $nodeValue]; |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Determines the type and value according to $nodeValue when one of these characters is found : !,&,* |
236
|
|
|
* |
237
|
|
|
* @param string $nodeValue The node value |
|
|
|
|
238
|
|
|
* |
239
|
|
|
* @return array array with the type and $value |
|
|
|
|
240
|
|
|
* @see self::define |
|
|
|
|
241
|
|
|
*/ |
242
|
|
|
private function onNodeAction($nodeValue):array |
|
|
|
|
243
|
|
|
{ |
244
|
|
|
// TODO: handle tags like <tag:clarkevans.com,2002:invoice> |
245
|
|
|
$v = substr($nodeValue, 1); |
246
|
|
|
$type = ['!' => Y\TAG, '&' => Y\REF_DEF, '*' => Y\REF_CALL][$nodeValue[0]]; |
|
|
|
|
247
|
|
|
$pos = strpos($v, ' '); |
248
|
|
|
$this->identifier = is_bool($pos) ? $v : strstr($v, ' ', true); |
|
|
|
|
249
|
|
|
$n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos+1)), $this->line))->setParent($this); |
|
|
|
|
250
|
|
|
return [$type, $n]; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Returns the correct PHP datatype for the value of the current Node |
255
|
|
|
* (simple value Node assumed -> $value is a scalar) |
256
|
|
|
* |
257
|
|
|
* @return mixed The value as PHP type. |
|
|
|
|
258
|
|
|
*/ |
259
|
|
|
public function getPhpValue() |
260
|
|
|
{ |
261
|
|
|
$v = $this->value; |
262
|
|
|
if (is_null($v)) return null; |
|
|
|
|
263
|
|
|
if ($this->type & (Y\REF_CALL | Y\SCALAR)) return self::getScalar($v); |
|
|
|
|
264
|
|
|
if ($this->type & (Y\COMPACT_MAPPING | Y\COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
|
|
|
265
|
|
|
switch ($this->type) { |
266
|
|
|
case Y\JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
|
|
|
|
267
|
|
|
case Y\QUOTED: return substr($v, 1, -1); |
|
|
|
|
268
|
|
|
case Y\RAW: return strval($v); |
|
|
|
|
269
|
|
|
default: |
|
|
|
|
270
|
|
|
trigger_error("Error can not get PHP type for ".Y\getName($this->type), E_USER_WARNING); |
|
|
|
|
271
|
|
|
return null; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Returns the correct PHP type according to the string value |
277
|
|
|
* |
278
|
|
|
* @param string $v a string value |
|
|
|
|
279
|
|
|
* |
280
|
|
|
* @return mixed The scalar value with appropriate PHP type |
|
|
|
|
281
|
|
|
*/ |
282
|
|
|
private static function getScalar(string $v) |
|
|
|
|
283
|
|
|
{ |
284
|
|
|
$types = ['yes' => true, |
285
|
|
|
'no' => false, |
286
|
|
|
'true' => true, |
287
|
|
|
'false' => false, |
288
|
|
|
'null' => null, |
289
|
|
|
'.inf' => INF, |
290
|
|
|
'-.inf' => -INF, |
291
|
|
|
'.nan' => NAN |
292
|
|
|
]; |
293
|
|
|
if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
|
|
|
|
294
|
|
|
if (R::isDate($v)) return date_create($v); |
|
|
|
|
295
|
|
|
if (R::isNumber($v)) return self::getNumber($v); |
|
|
|
|
296
|
|
|
return strval($v); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Returns the correct PHP type according to the string value |
301
|
|
|
* |
302
|
|
|
* @param string $v a string value |
|
|
|
|
303
|
|
|
* |
304
|
|
|
* @return int|float The scalar value with appropriate PHP type |
|
|
|
|
305
|
|
|
*/ |
306
|
|
|
private static function getNumber(string $v) |
|
|
|
|
307
|
|
|
{ |
308
|
|
|
if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
|
|
|
309
|
|
|
if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
|
|
|
310
|
|
|
// TODO: remove these if not needed |
311
|
|
|
// if preg_match("/^([\d.]+e[-+]\d{1,2})$/", $v)://fall through |
312
|
|
|
// if preg_match("/^([-+]?(?:\d+|\d*.\d+))$/", $v): |
313
|
|
|
return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
|
|
|
|
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
private static function getCompact(string $mappingOrSeqString, int $type):object |
|
|
|
|
317
|
|
|
{ |
318
|
|
|
$out = new Compact(); |
319
|
|
|
if ($type === Y\COMPACT_SEQUENCE) { |
|
|
|
|
320
|
|
|
$f = function ($e) { return self::getScalar(trim($e));}; |
|
|
|
|
321
|
|
|
//TODO : that's not robust enough, improve it |
322
|
|
|
foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) { |
323
|
|
|
$out[$key] = $value; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
if ($type === Y\COMPACT_MAPPING) { |
|
|
|
|
327
|
|
|
//TODO : that's not robust enough, improve it |
328
|
|
|
foreach (explode(',', $mappingOrSeqString) as $value) { |
329
|
|
|
list($keyName, $keyValue) = explode(':', $value); |
330
|
|
|
$out->{trim($keyName)} = self::getScalar(trim($keyValue)); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
return $out; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* PHP internal function for debugging purpose : simplify output provided by 'var_dump' |
338
|
|
|
* |
339
|
|
|
* @return array the Node properties and respective values displayed by 'var_dump' |
|
|
|
|
340
|
|
|
*/ |
341
|
|
|
public function __debugInfo():array |
342
|
|
|
{ |
343
|
|
|
return ['line' => $this->line, |
344
|
|
|
'indent'=> $this->indent, |
345
|
|
|
'type' => Y::getName($this->type).($this->identifier ? "($this->identifier)" : ''), |
346
|
|
|
'value' => $this->value]; |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|