Completed
Push — master ( d5e18b...36bd6b )
by stéphane
04:01
created

Key   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 95.08%

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 115
ccs 58
cts 61
cp 0.9508
rs 9.92
c 0
b 0
f 0
wmc 31

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 5
B isAwaitingChild() 0 25 10
A add() 0 6 3
A getTargetOnEqualIndent() 0 6 2
A build() 0 12 3
A setIdentifier() 0 15 5
A getTargetOnMoreIndent() 0 8 3
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 Key extends NodeGeneric
15
{
16
    const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d";
17
18 9
    public function __construct(string $nodeString, int $line, array $matches = null)
19
    {
20 9
        parent::__construct($nodeString, $line);
21 9
        if (is_null($matches)) {
22 9
            if (!((bool) preg_match(Regex::KEY, ltrim($nodeString), $matches))) {
23 1
                throw new \ParseError("Not a KEY:VALUE syntax ($nodeString)", 1);
24
            }
25
        }
26 9
        $this->setIdentifier($matches[1]);
27 9
        $value = isset($matches[2]) ? trim($matches[2]) : null;
28 9
        if (!empty($value)) {
29 9
            $child = NodeFactory::get($value, $line);
30 9
            $child->indent = null;
31 9
            $this->add($child);
32
        }
33 9
    }
34
35 9
    public function setIdentifier(string $keyString)
36
    {
37 9
        if ($keyString === '') {
38 1
           throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $this->line));
39
        } else {
40 9
            $node = NodeFactory::get($keyString);
41 9
            if ($node->isOneOf('Tag', 'Quoted')) {
42 2
                $built = $node->build();
43 2
                if (is_object($built)) {
44
                    $this->identifier = $built->value;
45
                } else {
46 2
                    $this->identifier = (string) $node->build();
47
                }
48 9
            } elseif ($node instanceof Scalar) {
49 9
                $this->identifier = trim($node->raw);
50
            }
51
        }
52 9
    }
53
54 9
    public function add(NodeGeneric $child):NodeGeneric
55
    {
56 9
        if ($this->value instanceof NodeGeneric && $this->value->isOneOf('Literal','LiteralFolded', 'Anchor')) {
57 1
            return $this->value->add($child);
58
        } else {
59 9
            return parent::add($child);
60
        }
61
    }
62
63 1
    public function getTargetOnEqualIndent(NodeGeneric &$node):NodeGeneric
64
    {
65 1
        if ($node instanceof Item) {
66 1
            return $this;
67
        }
68 1
        return $this->getParent();
69
    }
70
71 1
    public function getTargetOnMoreIndent(NodeGeneric &$node):NodeGeneric
72
    {
73 1
        if (!is_null($this->value)) {
74 1
            if ($this->getDeepestNode()->isAwaitingChild($node)) {
75 1
                return $this->getDeepestNode();
76
            }
77
        }
78 1
        return $this;
79
    }
80
81
82 1
    public function isAwaitingChild(NodeGeneric $node):bool
83
    {
84 1
        if (is_null($this->value) || $node instanceof Comment) {
85
            return true;
86
        }
87 1
        $current = $this->value instanceof NodeGeneric ? $this->value : $this->value->current();
88 1
        if ($current instanceof Comment) {
89 1
            return true;
90
        }
91 1
        if($current instanceof Scalar) {
92 1
            return $node->isOneOf('Scalar', 'Blank');
93
        }
94 1
        if ($current instanceof Item) {
95 1
            return $node instanceof Item;
96
        }
97 1
        if ($current instanceof Key) {
98 1
            return $node instanceof Key;
99
        }
100 1
        if ($current instanceof Literals) {
101 1
            return $node->indent > $this->indent;
102
        }
103 1
        if ($current instanceof Anchor) {
104 1
            return $current->isAwaitingChild($node);
105
        }
106
        return false;
107
    }
108
109
    /**
110
     * Builds a key and set the property + value to the given parent
111
     *
112
     * @param object|array $parent The parent
113
     *
114
     * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed
115
     * @return null|\StdClass
116
     */
117 1
    public function build(&$parent = null)
118
    {
119
        // if (!is_null($this->tag)) {
120
        //     return TagFactory::transform($this->tag, $this)->build($parent);
121
        // }
122 1
        $result = is_null($this->value) ? null : $this->value->build();
123 1
        if (is_null($parent)) {
124 1
            $parent = new \StdClass;
125 1
            $parent->{$this->identifier} = $result;
126 1
            return $parent;
127
        } else {
128 1
            $parent->{$this->identifier} = $result;
129
        }
130
    }
131
}