|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Constructs the result (YamlObject or array) according to every Node and respecting value |
|
7
|
|
|
* |
|
8
|
|
|
* @author Stéphane Rebai <[email protected]> |
|
9
|
|
|
* @license Apache 2.0 |
|
10
|
|
|
* @link TODO : url to specific online doc |
|
11
|
|
|
*/ |
|
12
|
|
|
final class Builder |
|
13
|
|
|
{ |
|
14
|
|
|
public static $_root; |
|
15
|
|
|
private static $_debug; |
|
16
|
|
|
|
|
17
|
|
|
const INVALID_DOCUMENT = "DOCUMENT %d is invalid,"; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Builds a file. check multiple documents & split if more than one documents |
|
21
|
|
|
* |
|
22
|
|
|
* @param NodeRoot $root The root node : Node with Node->type === YAML::ROOT |
|
23
|
|
|
* @param int $_debug the level of debugging requested |
|
24
|
|
|
* |
|
25
|
|
|
* @return array|YamlObject list of documents or just one. |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function buildContent(NodeRoot $root, int $_debug) |
|
28
|
|
|
{ |
|
29
|
|
|
self::$_debug = $_debug; |
|
30
|
|
|
$documents = []; |
|
31
|
|
|
$buffer = new NodeList(); |
|
32
|
|
|
foreach ($root->getValue() as $child) { |
|
33
|
|
|
if ($child instanceof NodeDocEnd && $child !== $root->value->top()) { |
|
|
|
|
|
|
34
|
|
|
$buffer->push($child); |
|
35
|
|
|
$documents[] = self::buildDocument($buffer, count($documents)); |
|
36
|
|
|
$buffer = new NodeList(); |
|
37
|
|
|
continue; |
|
38
|
|
|
} elseif ($child instanceof NodeDocStart && $buffer->count() > 0 && $buffer->hasContent()) { |
|
39
|
|
|
$documents[] = self::buildDocument($buffer, count($documents)); |
|
40
|
|
|
$buffer = new NodeList($child); |
|
41
|
|
|
continue; |
|
42
|
|
|
} |
|
43
|
|
|
$buffer->push($child); |
|
44
|
|
|
} |
|
45
|
|
|
try { |
|
46
|
|
|
$documents[] = self::buildDocument($buffer, count($documents)); |
|
47
|
|
|
} catch (\Exception|\Error|\ParseError $e) { |
|
48
|
|
|
throw new \Exception($e->getMessage(), 1, $e); |
|
49
|
|
|
} |
|
50
|
|
|
return count($documents) === 1 ? $documents[0] : $documents; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Builds the tree of Node for this document (as NodeList) |
|
55
|
|
|
* |
|
56
|
|
|
* @param NodeList $list the list of nodes that constitutes the current document |
|
57
|
|
|
* @param int $docNum the index (starts @ 0) of this document in the whole YAML content provided to self::buildContent |
|
58
|
|
|
* |
|
59
|
|
|
* @return YamlObject the YAML document as an object |
|
60
|
|
|
*/ |
|
61
|
|
|
private static function buildDocument(NodeList $list, int $docNum):YamlObject |
|
62
|
|
|
{ |
|
63
|
|
|
self::$_root = new YamlObject; |
|
64
|
|
|
try { |
|
65
|
|
|
$out = self::buildNodeList($list, self::$_root); |
|
66
|
|
|
if (is_string($out)) { |
|
67
|
|
|
$out = self::$_root->setText($out); |
|
68
|
|
|
} |
|
69
|
|
|
} catch (\Exception|\Error $e) { |
|
70
|
|
|
throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $docNum).':'.$e->getMessage(), 2, $e); |
|
71
|
|
|
} |
|
72
|
|
|
return $out; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Builds a node list. |
|
78
|
|
|
* |
|
79
|
|
|
* @param NodeList $list The node |
|
80
|
|
|
* @param mixed $parent The parent |
|
81
|
|
|
* |
|
82
|
|
|
* @return mixed The parent (object|array) or a string representing the NodeList. |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function buildNodeList(NodeList $list, &$parent = null) |
|
85
|
|
|
{ |
|
86
|
|
|
$out = ''; |
|
87
|
|
|
foreach ($list as $child) { |
|
88
|
|
|
if ($child instanceof NodeDocStart) { |
|
89
|
|
|
$child->build($parent); |
|
90
|
|
|
} else { |
|
91
|
|
|
$out .= ','.$child->build($parent); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
if (is_string($out)) { |
|
|
|
|
|
|
95
|
|
|
$result = implode(explode(',', $out)); |
|
96
|
|
|
$out = $result === '' ? null : Node::getScalar($result); |
|
97
|
|
|
} |
|
98
|
|
|
return is_null($out) ? $parent : $out; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.