Completed
Push — master ( c45521...844759 )
by stéphane
08:22
created

NodeKey::isAwaitingChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 NodeKey extends Node
12
{
13
    const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d";
14
15
    public function __construct(string $nodeString, int $line, array $matches)
16
    {
17
        parent::__construct($nodeString, $line);
18
        $this->identifier = trim($matches[1], '"\' ');
19
        $value = isset($matches[2]) ? trim($matches[2]) : null;
20
        if (!is_null($value)) {
21
            $hasComment = strpos($value, ' #');
22
            if (is_bool($hasComment) || Regex::isProperlyQuoted($value)) {
23
                $child = NodeFactory::get(trim($value), $line);
24
            } else {
25
                $child = new NodeComment(trim(substr($value, 0, $hasComment)), $line);
26
            }
27
            $this->add($child);
28
        }
29
    }
30
31
    public function getTargetOnLessIndent(Node $previous):Node
32
    {
33
        if ($this->indent === 0) {
34
            return $previous->getParent(-1);//get root
35
        } else {
36
            return parent::getTargetOnLessIndent($previous);
37
        }
38
    }
39
40
    /**
41
     * Modify parent target when current Node indentation is equal to previous node indentation
42
     *
43
     * @param Node $previous The previous Node
44
     *
45
     * @return Node
46
     */
47
    public function getTargetonEqualIndent(Node &$previous):Node
48
    {
49
        if ($this->indent === 0) {
50
            return $previous->getParent(-1);//get root
51
        } else {
52
            return parent::getTargetonEqualIndent($previous);
53
        }
54
    }
55
56
    public function isAwaitingChildren()
57
    {
58
        return is_null($this->value);
59
    }
60
61
62
63
    /**
64
     * Builds a key and set the property + value to the given parent
65
     *
66
     * @param object|array $parent The parent
67
     *
68
     * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed
69
     * @return null
70
     */
71
    public function build(&$parent = null)
72
    {
73
        if (is_null($this->identifier)) {
74
            throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $this->line));
75
        } else {
76
            if (is_null($this->value)) {
77
                $result = null;
78
            } elseif ($this->value instanceof Node) {
79
                $value = $this->value;
80
                switch (get_class($this->value)) {
81
                    case 'NodeItem':$mother = new NodeSequence();
82
                                    $mother->add($this->value);
83
                                    $value = $mother;
84
                        break;
85
                    case 'NodeKey': $mother = new NodeMapping();
86
                                    $mother->add($this->value);
87
                                    $value = $mother;
88
                        break;
89
                    case 'NodeSetKey':$mother = new NodeSet();
90
                                    $mother->add($this->value);
91
                                    $value = $mother;
92
                        break;
93
                }
94
                $result = $value->build($parent);
95
            } elseif ($this->value instanceof NodeList) {
96
                $result = Builder::buildNodeList($this->value);
97
            }
98
            if (is_null($parent)) {
99
                return $result;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.
Loading history...
100
            } else {
101
                if (is_array($parent)) {
102
                    $parent[$this->identifier] = $result;
103
                } else {
104
                    $parent->{$this->identifier} = $result;
105
                }
106
            }
107
        }
108
    }
109
110
}