Passed
Push — master ( 3b6a73...d590ba )
by stéphane
02:55
created

NodeKey::add()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Dallgoot\Yaml;
4
use Dallgoot\Yaml;
5
6
/**
7
 *
8
 * @author  Stéphane Rebai <[email protected]>
9
 * @license Apache 2.0
10
 * @link    TODO : url to specific online doc
11
 */
12
class NodeKey extends Node
13
{
14
    const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d";
15
16 9
    public function __construct(string $nodeString, int $line, array $matches = null)
17
    {
18 9
        parent::__construct($nodeString, $line);
19 9
        if (is_null($matches)) {
20 9
            if (!((bool) preg_match(Regex::KEY, ltrim($nodeString), $matches))) {
21 1
                throw new \ParseError("Not a KEY:VALUE syntax ($nodeString)", 1);
22
            }
23
        }
24 9
        $this->setIdentifier($matches[1]);
25 9
        $value = isset($matches[2]) ? trim($matches[2]) : null;
26 9
        if (!empty($value)) {
27 9
            $child = NodeFactory::get($value, $line);
28 9
            $child->indent = null;
29 9
            $this->add($child);
30
        }
31 9
    }
32
33 9
    public function setIdentifier(string $keyString)
34
    {
35 9
        if ($keyString === '') {
36 1
           throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $this->line));
37
        } else {
38 9
            $keyNode = NodeFactory::get($keyString);
39 9
            if ($keyNode instanceof NodeTag || $keyNode instanceof NodeQuoted) {
0 ignored issues
show
introduced by
$keyNode is never a sub-type of Dallgoot\Yaml\NodeQuoted.
Loading history...
40 2
                $this->identifier = $keyNode->build();
41 9
            } elseif ($keyNode instanceof NodeScalar) {
42 9
                $this->identifier = trim($keyNode->raw);
43
            }
44
            // if (!is_null($keyNode->anchor)) {
45
            //     $this->anchor = $keyNode->anchor;
46
            //     $anchor = $keyNode->anchor;
47
            //     $pos = strlen($keyNode->anchor);
48
            //     $this->identifier = $keyNode->value->raw;
49
            // } elseif (!is_null($keyNode->tag)) {
50
            //     $this->tag = $keyNode->tag;
51
            //     $raw = $keyNode->raw;
52
            //     $pos = strlen($keyNode->tag);
53
            //     $this->identifier = trim(substr($raw, $pos));
54
            // } elseif ($keyNode instanceof NodeScalar) {
55
            //     $this->identifier = ltrim($keyNode->raw);
56
            // }
57
        }
58 9
    }
59
60 9
    public function add(Node $child):Node
61
    {
62 9
        if ($this->value instanceof Node && Yaml::isOneOf($this->value, ['NodeLit','NodeLitFolded', 'NodeAnchor'])) {
63 1
            return $this->value->add($child);
64
        } else {
65 9
            return parent::add($child);
66
        }
67
    }
68
69 1
    public function getTargetOnEqualIndent(Node &$node):Node
70
    {
71 1
        if ($node instanceof NodeItem) {
72 1
            return $this;
73
        }
74 1
        return $this->getParent();
75
    }
76
77 1
    public function getTargetOnMoreIndent(Node &$node):Node
78
    {
79 1
        if (!is_null($this->value)) {
80 1
            if ($this->getDeepestNode()->isAwaitingChild($node)) {
81 1
                return $this->getDeepestNode();
82
            }
83
        }
84 1
        return $this;
85
    }
86
87
88 1
    public function isAwaitingChild(Node $node):bool
89
    {
90 1
        if (is_null($this->value) || $node instanceof NodeComment) {
91
            return true;
92
        }
93 1
        $current = $this->value instanceof Node ? $this->value : $this->value->current();
94 1
        if ($current instanceof NodeComment) {
95 1
            return true;
96
        }
97 1
        if($current instanceof NodeScalar) {
98 1
            return Yaml::isOneOf($node, ['NodeScalar', 'NodeBlank']);
99
        }
100 1
        if ($current instanceof NodeItem) {
101 1
            return $node instanceof NodeItem;
102
        }
103 1
        if ($current instanceof NodeKey) {
104 1
            return $node instanceof NodeKey;
105
        }
106 1
        if ($current instanceof NodeLiterals) {
107 1
            return $node->indent > $this->indent;
108
        }
109 1
        if ($current instanceof NodeAnchor) {
110 1
            return $current->isAwaitingChild($node);
111
        }
112
        return false;
113
    }
114
115
    /**
116
     * Builds a key and set the property + value to the given parent
117
     *
118
     * @param object|array $parent The parent
119
     *
120
     * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed
121
     * @return null|\StdClass
122
     */
123 1
    public function build(&$parent = null)
124
    {
125
        // if (!is_null($this->tag)) {
126
        //     return TagFactory::transform($this->tag, $this)->build($parent);
127
        // }
128 1
        $result = is_null($this->value) ? null : $this->value->build();
129 1
        if (is_null($parent)) {
130 1
            $parent = new \StdClass;
131 1
            $parent->{$this->identifier} = $result;
132 1
            return $parent;
133
        } else {
134 1
            $parent->{$this->identifier} = $result;
135
        }
136
    }
137
}