|
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 NodeItem extends Node |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct(string $nodeString, int $line) |
|
14
|
|
|
{ |
|
15
|
|
|
parent::__construct($nodeString, $line); |
|
16
|
|
|
preg_match(Regex::ITEM, $nodeString, $matches); |
|
17
|
|
|
if (isset($matches[1]) && !empty(trim($matches[1]))) { |
|
18
|
|
|
// $n->indent = $indent + 2; |
|
19
|
|
|
$n = NodeFactory::get($matches[1], $line); |
|
20
|
|
|
$this->add($n); |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
/** |
|
24
|
|
|
* Modify parent target when current Node indentation is equal to previous node indentation |
|
25
|
|
|
* |
|
26
|
|
|
* @param Node $previous The previous |
|
27
|
|
|
* |
|
28
|
|
|
* @return Node |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getTargetonEqualIndent(Node &$previous):Node |
|
31
|
|
|
{ |
|
32
|
|
|
$deepest = $previous->getDeepestNode(); |
|
33
|
|
|
if ($deepest->isAwaitingChildren()) { |
|
34
|
|
|
return $deepest; |
|
35
|
|
|
} else { |
|
36
|
|
|
return parent::getTargetonEqualIndent($previous); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Builds an item. Adds the item value to the parent array|Iterator |
|
43
|
|
|
* |
|
44
|
|
|
* @param array|\Iterator|null $parent The parent |
|
45
|
|
|
* |
|
46
|
|
|
* @throws \Exception if parent is another type than array or object Iterator |
|
47
|
|
|
* @return null |
|
48
|
|
|
*/ |
|
49
|
|
|
public function build(&$parent = null):void |
|
50
|
|
|
{ |
|
51
|
|
|
// extract((array) $node, EXTR_REFS); |
|
52
|
|
|
if (!is_array($parent) && !($parent instanceof \ArrayIterator)) { |
|
53
|
|
|
throw new \Exception("parent must be an ArrayIterator not ".(is_object($parent) ? get_class($parent) : gettype($parent))); |
|
54
|
|
|
} |
|
55
|
|
|
$ref = $parent instanceof \ArrayIterator ? $parent->getArrayCopy() : $parent; |
|
|
|
|
|
|
56
|
|
|
$numKeys = array_filter(array_keys($ref), 'is_int'); |
|
57
|
|
|
$key = count($numKeys) > 0 ? max($numKeys) + 1 : 0; |
|
58
|
|
|
if ($this->value instanceof NodeKey) { |
|
59
|
|
|
$this->value->build($parent); |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
$parent[$key] = $this->value->build(); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
} |