1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml; |
4
|
|
|
|
5
|
|
|
use Dallgoot\Yaml as Y; |
6
|
|
|
|
7
|
|
|
/** |
|
|
|
|
8
|
|
|
* |
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
final class Builder |
11
|
|
|
{ |
12
|
|
|
private static $_root; |
13
|
|
|
private static $_debug; |
14
|
|
|
|
15
|
|
|
const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d"; |
16
|
|
|
const INVALID_DOCUMENT = self::class.": DOCUMENT %d can NOT be a mapping AND a sequence"; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
private static function build(object $node, &$parent = null) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
|
|
|
22
|
|
|
return self::buildNode($node, $parent); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private static function buildNodeList(NodeList $node, &$parent) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$type = property_exists($node, "type") ? $node->type : null; |
28
|
|
|
if ($type & (Y\RAW | Y\LITTERALS)) { |
|
|
|
|
29
|
|
|
return self::litteral($node, $type); |
30
|
|
|
} |
31
|
|
|
$p = $parent; |
32
|
|
|
switch ($type) { |
33
|
|
|
case Y\MAPPING: //fall through |
|
|
|
|
34
|
|
|
case Y\SET: $p = new \StdClass;break; |
|
|
|
|
35
|
|
|
case Y\SEQUENCE: $p = [];break; |
|
|
|
|
36
|
|
|
// case Y\KEY: $p = $parent;break; |
37
|
|
|
} |
38
|
|
|
$out = null; |
39
|
|
|
foreach ($node as $child) { |
40
|
|
|
$result = self::build($child, $p); |
41
|
|
|
if (!is_null($result)) { |
42
|
|
|
if (is_string($result)) { |
43
|
|
|
$out .= $result.' '; |
44
|
|
|
} else { |
45
|
|
|
return $result; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
return is_null($out) ? $p : rtrim($out); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private static function buildNode(Node $node, &$parent) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
list($line, $type, $value, $identifier) = [$node->line, $node->type, $node->value, $node->identifier]; |
55
|
|
|
switch ($type) { |
56
|
|
|
case Y\COMMENT: self::$_root->addComment($line, $value);return; |
|
|
|
|
57
|
|
|
case Y\DIRECTIVE: return;//TODO |
|
|
|
|
58
|
|
|
case Y\ITEM: self::buildItem($value, $parent);return; |
|
|
|
|
59
|
|
|
case Y\KEY: self::buildKey($node, $parent);return; |
|
|
|
|
60
|
|
|
case Y\REF_DEF: //fall through |
|
|
|
|
61
|
|
|
case Y\REF_CALL://TODO: self::build returns what ? |
|
|
|
|
62
|
|
|
$tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
63
|
|
|
if ($type === Y\REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
|
|
|
64
|
|
|
return self::$_root->getReference($identifier); |
65
|
|
|
case Y\SET_KEY: |
|
|
|
|
66
|
|
|
$key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
|
|
|
|
67
|
|
|
if (empty($key)) |
|
|
|
|
68
|
|
|
throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
69
|
|
|
$parent->{$key} = null; |
70
|
|
|
return; |
71
|
|
|
case Y\SET_VALUE: |
|
|
|
|
72
|
|
|
$prop = array_keys(get_object_vars($parent)); |
73
|
|
|
$key = end($prop); |
74
|
|
|
if (property_exists($value, "type") && ($value->type & (Y\ITEM | Y\MAPPING))) { |
|
|
|
|
75
|
|
|
$p = $value->type === Y\ITEM ? [] : new \StdClass; |
76
|
|
|
self::build($value, $p); |
77
|
|
|
} else { |
|
|
|
|
78
|
|
|
$p = self::build($value, $parent->{$key}); |
79
|
|
|
} |
|
|
|
|
80
|
|
|
$parent->{$key} = $p; |
81
|
|
|
return; |
82
|
|
|
case Y\TAG: |
|
|
|
|
83
|
|
|
if ($parent === self::$_root) { |
|
|
|
|
84
|
|
|
$parent->addTag($identifier);return; |
85
|
|
|
} else {//TODO: have somewhere a list of common tags and their treatment |
|
|
|
|
86
|
|
|
if (in_array($identifier, ['!binary', '!str'])) { |
|
|
|
|
87
|
|
|
if ($value->value instanceof NodeList) $value->value->type = Y\RAW; |
|
|
|
|
88
|
|
|
else $value->type = Y\RAW; |
|
|
|
|
89
|
|
|
} |
|
|
|
|
90
|
|
|
$val = is_null($value) ? null : self::build($value, $node); |
|
|
|
|
91
|
|
|
return new Tag($identifier, $val); |
92
|
|
|
} |
|
|
|
|
93
|
|
|
default: |
|
|
|
|
94
|
|
|
return is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Builds a key and set the property + value to the parent given |
100
|
|
|
* |
101
|
|
|
* @param Node $node The node |
|
|
|
|
102
|
|
|
* @param object|array $parent The parent |
|
|
|
|
103
|
|
|
* |
104
|
|
|
* @throws \ParseError if Key has no name(identifier) |
105
|
|
|
*/ |
|
|
|
|
106
|
|
|
private static function buildKey($node, &$parent):void |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
list($identifier, $value) = [$node->identifier, $node->value]; |
109
|
|
|
if (is_null($identifier)) { |
110
|
|
|
throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $node->line)); |
111
|
|
|
} else { |
112
|
|
|
if ($value instanceof Node && ($value->type & (Y\KEY | Y\ITEM))) { |
|
|
|
|
113
|
|
|
$parent->{$identifier} = $value->type & Y\KEY ? new \StdClass : []; |
114
|
|
|
self::build($value, $parent->{$identifier}); |
115
|
|
|
} elseif (is_object($value)) { |
116
|
|
|
$parent->{$identifier} = self::build($value, $parent->{$identifier}); |
117
|
|
|
} else { |
118
|
|
|
$parent->{$identifier} = $node->getPhpValue(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private static function buildItem($value, &$parent):void |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
if(!is_array($parent) && !($parent instanceof \ArrayIterator)) { |
|
|
|
|
126
|
|
|
throw new \Exception("parent must be an Iterable not ".(is_object($parent) ? get_class($parent) : gettype($parent)), 1); |
127
|
|
|
} |
128
|
|
|
if ($value instanceof Node && $value->type === Y\KEY) { |
|
|
|
|
129
|
|
|
$parent[$value->identifier] = self::build($value->value, $parent[$value->identifier]); |
|
|
|
|
130
|
|
|
} else { |
131
|
|
|
$index = count($parent); |
132
|
|
|
$parent[$index] = self::build($value, $parent[$index]); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
|
|
|
|
137
|
|
|
* Builds a file. check multiple documents & split if more than one documents |
138
|
|
|
* |
139
|
|
|
* @param Node $_root The root node |
|
|
|
|
140
|
|
|
* @param int $debug the level of debugging requested |
|
|
|
|
141
|
|
|
* |
142
|
|
|
* @return array|YamlObject list of documents or juste one. |
143
|
|
|
*/ |
144
|
|
|
public static function buildContent(Node $_root, int $_debug) |
145
|
|
|
{ |
146
|
|
|
self::$_debug = $_debug; |
147
|
|
|
$totalDocStart = 0; |
148
|
|
|
$documents = []; |
149
|
|
|
if ($_root->value instanceof Node) { |
150
|
|
|
$q = new NodeList; |
151
|
|
|
$q->push($_root->value); |
152
|
|
|
return [self::buildDocument($q, 0)]; |
153
|
|
|
} |
154
|
|
|
$_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
|
|
|
|
155
|
|
|
foreach ($_root->value as $child) { |
156
|
|
|
if ($child->type & Y\DOC_START) $totalDocStart++; |
|
|
|
|
157
|
|
|
//if 0 or 1 DOC_START = we are still in first document |
158
|
|
|
$currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
159
|
|
|
if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
|
|
|
160
|
|
|
$documents[$currentDoc]->push($child); |
161
|
|
|
} |
162
|
|
|
// $_debug >= 2 && var_dump($documents);//var_dump($documents);die("documents"); |
163
|
|
|
$content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
164
|
|
|
return count($content) === 1 ? $content[0] : $content; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
private static function buildDocument(NodeList $list, int $key):YamlObject |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
self::$_root = new YamlObject(); |
170
|
|
|
$childTypes = self::getChildrenTypes($list); |
171
|
|
|
$isMapping = count(array_intersect($childTypes, [Y\KEY, Y\MAPPING])) > 0; |
|
|
|
|
172
|
|
|
$isSequence = in_array(Y\ITEM, $childTypes); |
|
|
|
|
173
|
|
|
$isSet = in_array(Y\SET_VALUE, $childTypes); |
|
|
|
|
174
|
|
|
if ($isMapping && $isSequence) { |
175
|
|
|
throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
176
|
|
|
} else { |
177
|
|
|
switch (true) { |
178
|
|
|
case $isSequence: $list->type = Y\SEQUENCE;break; |
|
|
|
|
179
|
|
|
case $isSet: $list->type = Y\SET;break; |
|
|
|
|
180
|
|
|
case $isMapping://fall through |
|
|
|
|
181
|
|
|
default: $list->type = Y\MAPPING; |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
// self::$_debug >= 3 && var_dump(self::$_root, $list); |
185
|
|
|
$string = ''; |
186
|
|
|
foreach ($list as $child) { |
187
|
|
|
$result = self::build($child, self::$_root); |
188
|
|
|
if (is_string($result)) { |
189
|
|
|
$string .= $result.' '; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
if (!empty($string)) { |
193
|
|
|
self::$_root->setText(rtrim($string)); |
194
|
|
|
} |
195
|
|
|
return self::$_root; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
private static function litteral(NodeList $children, $type):string |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$children->rewind(); |
201
|
|
|
$refIndent = $children->current()->indent; |
202
|
|
|
$separator = $type === Y\RAW ? '' : "\n"; |
|
|
|
|
203
|
|
|
$action = function ($c) { return $c->value; }; |
|
|
|
|
204
|
|
|
if ($type & Y\LITT_FOLDED) { |
|
|
|
|
205
|
|
|
$separator = ' '; |
206
|
|
|
$action = function ($c) use ($refIndent) { |
207
|
|
|
return $c->indent > $refIndent || ($c->type & Y\BLANK) ? "\n".$c->value : $c->value; |
|
|
|
|
208
|
|
|
}; |
209
|
|
|
} |
210
|
|
|
$tmp = []; |
211
|
|
|
$children->rewind(); |
212
|
|
|
foreach ($children as $child) { |
213
|
|
|
$tmp[] = $action($child); |
214
|
|
|
} |
215
|
|
|
return implode($separator, $tmp); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
private static function getChildrenTypes(NodeList $children):array |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
$types = []; |
221
|
|
|
foreach ($children as $child) { |
222
|
|
|
$types[] = $child->type; |
223
|
|
|
} |
224
|
|
|
return array_unique($types); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|