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