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