Completed
Push — master ( 3eacba...3b6a73 )
by stéphane
02:25
created

NodeItem::add()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 5
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 7
    public function __construct(string $nodeString, int $line)
14
    {
15 7
        parent::__construct($nodeString, $line);
16 7
        preg_match(Regex::ITEM, ltrim($nodeString), $matches);
17 7
        $value = isset($matches[1]) ? ltrim($matches[1]) : null;
18 7
        if (!empty($value)) {
19 4
            $n = NodeFactory::get($value, $line);
20 4
            $n->indent = $this->indent + 2;
21 4
            $this->add($n);
22
        }
23 7
    }
24
25 6
    public function add(Node $child):Node
26
    {
27 6
        $value = $this->value;
28 6
        if ($value instanceof NodeKey && $child instanceof NodeKey) {
29 2
            if ($value->indent === $child->indent) {
30 1
                return parent::add($child);
31 2
            } elseif ($value->isAwaitingChild($child)){
32 1
                return $value->add($child);
33
            } else {
34
                // throw new \ParseError('key ('.$value->identifier.')@'.$value->line.' has already a value', 1);
35 1
                throw new \ParseError('key @'.$value->line.' has already a value', 1);
36
            }
37
        }
38 6
        return parent::add($child);
39
    }
40
41 1
    public function getTargetOnEqualIndent(Node &$node):Node
42
    {
43 1
        $supposedParent = $this->getParent();
44 1
        if ($node->indent === $supposedParent->indent) {
45
            return $supposedParent->getParent();
46
        }
47 1
        return $supposedParent;
48
    }
49
50 1
    public function getTargetOnMoreIndent(Node &$node):Node
51
    {
52 1
        return $this->value instanceof Node && $this->value->isAwaitingChild($node) ? $this->value : $this;
53
    }
54
55
    /**
56
     * Builds an item. Adds the item value to the parent array|Iterator
57
     *
58
     * @param array|\Iterator|null $parent The parent
59
     *
60
     * @throws \Exception  if parent is another type than array or object Iterator
61
     * @return null
62
     */
63 1
    public function build(&$parent = null)
64
    {
65 1
        if (!is_null($parent) && !is_array($parent) && !($parent instanceof \ArrayIterator)) {
66
            throw new \Exception("parent must be an ArrayIterator not ".
67
                (is_object($parent) ? get_class($parent) : gettype($parent)));
68
        }
69 1
        $value = $this->value ? $this->value->build() : null;
70 1
        if (is_null($parent)) {
71 1
            return [$value];
72
        } else {
73 1
            $ref = is_array($parent) ? $parent : iterator_to_array($parent);
0 ignored issues
show
introduced by
The condition is_array($parent) is always true.
Loading history...
74 1
            $numKeys = array_keys($ref);
75 1
            $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0;
76 1
            $parent[$key] = $value;
77
        }
78 1
    }
79
80 1
    public function isAwaitingChild(Node $node):bool
81
    {
82 1
        if (is_null($this->value)) {
83 1
            return true;
84 1
        } elseif ($this->value instanceof NodeSetKey && $node instanceof NodeSetValue) {
85 1
            return true;
86
        } else {
87 1
            return $this->getDeepestNode()->isAwaitingChild($node);
88
        }
89
    }
90
}