1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml\Nodes; |
4
|
|
|
|
5
|
|
|
use Dallgoot\Yaml\Nodes\Generic\NodeGeneric; |
6
|
|
|
use Dallgoot\Yaml\NodeFactory; |
7
|
|
|
use Dallgoot\Yaml\Regex; |
8
|
|
|
use Dallgoot\Yaml\Types\YamlObject; |
9
|
|
|
use Dallgoot\Yaml\Types\Compact; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* @author Stéphane Rebai <[email protected]> |
14
|
|
|
* @license Apache 2.0 |
15
|
|
|
* @link https://github.com/dallgoot/yaml |
16
|
|
|
*/ |
17
|
|
|
class Item extends NodeGeneric |
18
|
|
|
{ |
19
|
9 |
|
public function __construct(string $nodeString, int $line) |
20
|
|
|
{ |
21
|
9 |
|
parent::__construct($nodeString, $line); |
22
|
9 |
|
preg_match(Regex::ITEM, ltrim($nodeString), $matches); |
23
|
9 |
|
$value = isset($matches[1]) ? ltrim($matches[1]) : null; |
24
|
9 |
|
if (!empty($value)) { |
25
|
4 |
|
$n = NodeFactory::get($value, $line); |
26
|
4 |
|
$n->indent = $this->indent + 2; |
27
|
4 |
|
$this->add($n); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
public function add(NodeGeneric $child): NodeGeneric |
32
|
|
|
{ |
33
|
6 |
|
$value = $this->value; |
34
|
6 |
|
if ($value instanceof Key && $child instanceof Key) { |
35
|
2 |
|
if ($value->indent === $child->indent) { |
36
|
1 |
|
return parent::add($child); |
37
|
2 |
|
} elseif ($value->isAwaitingChild($child)) { |
38
|
1 |
|
return $value->add($child); |
39
|
|
|
} else { |
40
|
|
|
// throw new \ParseError('key ('.$value->identifier.')@'.$value->line.' has already a value', 1); |
41
|
1 |
|
throw new \ParseError('key @' . $value->line . ' has already a value', 1); |
42
|
|
|
} |
43
|
|
|
} |
44
|
6 |
|
return parent::add($child); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function getTargetOnEqualIndent(NodeGeneric &$node): NodeGeneric |
48
|
|
|
{ |
49
|
1 |
|
if($node instanceof Key ) { |
50
|
|
|
return $this->getParent()->getParent(); |
51
|
|
|
} |
52
|
1 |
|
$supposedParent = $this->getParent(); |
53
|
|
|
// Note: the check for indent = 0 is to support Items WITH indent that are NOT at the root of the document (indent > 0) |
54
|
1 |
|
if ($node->indent === 0 && $node->indent === $supposedParent->indent) { |
55
|
1 |
|
return $supposedParent->getParent(); |
56
|
|
|
} |
57
|
1 |
|
return $supposedParent; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function getTargetOnMoreIndent(NodeGeneric &$node): NodeGeneric |
61
|
|
|
{ |
62
|
1 |
|
return $this->value instanceof NodeGeneric && $this->value->isAwaitingChild($node) ? $this->value : $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Builds an item. Adds the item value to the parent array|Iterator |
67
|
|
|
* |
68
|
|
|
* @param array|YamlObject|Compact|null $parent The parent |
69
|
|
|
* |
70
|
|
|
* @throws \Exception if parent is another type than array or object Iterator |
71
|
|
|
*/ |
72
|
3 |
|
public function build(&$parent = null): ?array |
73
|
|
|
{ |
74
|
3 |
|
if (!is_null($parent) |
75
|
3 |
|
&& !is_array($parent) |
76
|
3 |
|
&& !($parent instanceof YamlObject) |
77
|
3 |
|
&& !($parent instanceof Compact) |
|
|
|
|
78
|
|
|
) { |
79
|
2 |
|
throw new \Exception("parent must be an array or YamlObject not " . |
80
|
2 |
|
(is_object($parent) ? get_class($parent) : gettype($parent))); |
81
|
|
|
} |
82
|
1 |
|
$value = $this->value ? $this->value->build() : null; |
83
|
1 |
|
if (is_null($parent)) { |
84
|
1 |
|
return [$value]; |
85
|
|
|
} else { |
86
|
|
|
// $ref = is_array($parent) ? $parent : iterator_to_array($parent); |
87
|
|
|
// $numKeys = array_keys($ref); |
88
|
|
|
// $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0; |
89
|
|
|
// $parent[$key] = $value; |
90
|
1 |
|
$parent[] = $value; |
91
|
1 |
|
return null; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function isAwaitingChild(NodeGeneric $node): bool |
96
|
|
|
{ |
97
|
1 |
|
if (is_null($this->value)) { |
98
|
1 |
|
return true; |
99
|
1 |
|
} elseif ($this->value instanceof SetKey && $node instanceof SetValue) { |
100
|
1 |
|
return true; |
101
|
|
|
} else { |
102
|
1 |
|
return $this->getDeepestNode()->isAwaitingChild($node); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|