Passed
Push — master ( f81cc4...5281ab )
by stéphane
04:50
created

Item::getTargetOnEqualIndent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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