1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
* @author Stéphane Rebai <[email protected]> |
8
|
|
|
* @license Apache 2.0 |
9
|
|
|
* @link TODO : url to specific online doc |
10
|
|
|
*/ |
11
|
|
|
class NodeKey extends Node |
12
|
|
|
{ |
13
|
|
|
const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d"; |
14
|
|
|
|
15
|
|
|
public function __construct(string $nodeString, int $line, array $matches) |
16
|
|
|
{ |
17
|
|
|
parent::__construct($nodeString, $line); |
18
|
|
|
$this->setIdentifier(trim($matches[1], '"\' ')); |
19
|
|
|
$value = isset($matches[2]) ? trim($matches[2]) : null; |
20
|
|
|
if (!empty($value)) { |
21
|
|
|
$child = NodeFactory::get($value, $line); |
22
|
|
|
$child->indent = null; |
23
|
|
|
$this->add($child); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function setIdentifier(string $keyString) |
28
|
|
|
{ |
29
|
|
|
if ($keyString === '') { |
30
|
|
|
throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $this->line)); |
31
|
|
|
} else { |
32
|
|
|
$keyNode = NodeFactory::get($keyString); |
33
|
|
|
if (!is_null($keyNode->_anchor)) { |
34
|
|
|
$this->_anchor = $keyNode->_anchor; |
35
|
|
|
$this->identifier = trim($keyNode->value->raw); |
|
|
|
|
36
|
|
|
} elseif (!is_null($keyNode->_tag)) { |
37
|
|
|
$this->_tag = $keyNode->_tag; |
38
|
|
|
$this->identifier = trim($keyNode->value->raw); |
39
|
|
|
} elseif ($keyNode instanceof NodeScalar) { |
40
|
|
|
$this->identifier = trim($keyNode->raw); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function add(Node $child):Node |
46
|
|
|
{ |
47
|
|
|
if ($this->value instanceof Node && isOneOf($this->value, ['NodeLit','NodeLitFolded', 'NodeAnchor'])) { |
48
|
|
|
return $this->value->add($child); |
49
|
|
|
} else { |
50
|
|
|
return parent::add($child); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getTargetOnEqualIndent(Node &$node):Node |
55
|
|
|
{ |
56
|
|
|
if ($node instanceof NodeItem) { |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
return $this->getParent(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getTargetOnMoreIndent(Node &$node):Node |
63
|
|
|
{ |
64
|
|
|
if (!is_null($this->value)) { |
65
|
|
|
if ($this->getDeepestNode()->isAwaitingChild($node)) { |
66
|
|
|
return $this->getDeepestNode(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
public function isAwaitingChild(Node $node):bool |
74
|
|
|
{ |
75
|
|
|
if (is_null($this->value) || $node instanceof NodeComment) { |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
$current = $this->value instanceof Node ? $this->value : $this->value->current(); |
79
|
|
|
if ($current instanceof NodeComment) { |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
if($current instanceof NodeScalar) { |
83
|
|
|
return isOneOf($node, ['NodeScalar', 'NodeBlank']); |
84
|
|
|
} |
85
|
|
|
if ($current instanceof NodeItem) { |
86
|
|
|
return $node instanceof NodeItem; |
87
|
|
|
} |
88
|
|
|
if ($current instanceof NodeKey) { |
89
|
|
|
return $node instanceof NodeKey; |
90
|
|
|
} |
91
|
|
|
if ($current instanceof NodeLiterals) { |
92
|
|
|
return $node->indent > $this->indent; |
93
|
|
|
} |
94
|
|
|
if ($current instanceof NodeAnchor) { |
95
|
|
|
return $current->isAwaitingChild($node); |
96
|
|
|
} |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Builds a key and set the property + value to the given parent |
102
|
|
|
* |
103
|
|
|
* @param object|array $parent The parent |
104
|
|
|
* |
105
|
|
|
* @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
106
|
|
|
* @return null |
107
|
|
|
*/ |
108
|
|
|
public function build(&$parent = null) |
109
|
|
|
{ |
110
|
|
|
if (!is_null($this->_tag)) { |
111
|
|
|
return TagFactory::transform($this->_tag, $this)->build($parent); |
112
|
|
|
} |
113
|
|
|
$result = is_null($this->value) ? null : $this->value->build(); |
114
|
|
|
if (is_null($parent)) { |
115
|
|
|
$parent = new \StdClass; |
116
|
|
|
$parent->{$this->identifier} = $result; |
117
|
|
|
return $parent; |
|
|
|
|
118
|
|
|
} else { |
119
|
|
|
$parent->{$this->identifier} = $result; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |