SetKey::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 8
ccs 7
cts 7
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
use Dallgoot\Yaml\Nodes\Generic\NodeGeneric;
8
9
/**
10
 *
11
 * @author  Stéphane Rebai <[email protected]>
12
 * @license Apache 2.0
13
 * @link    https://github.com/dallgoot/yaml
14
 */
15
class SetKey extends NodeGeneric
16
{
17 3
    public function __construct(string $nodeString, int $line)
18
    {
19 3
        parent::__construct($nodeString, $line);
20 3
        $v = substr(trim($nodeString), 1);
21 3
        if (!empty($v)) {
22 3
            $value = NodeFactory::get($v, $line);
23 3
            $value->indent = null;
24 3
            $this->add($value);
25
        }
26
    }
27
28
    /**
29
     * @param object $parent The parent
30
     *
31
     * @throws \Exception  if a problem occurs during serialisation (json format) of the key
32
     */
33 1
    public function build(&$parent = null)
34
    {
35 1
        $built = is_null($this->value) ? null : $this->value->build();
36 1
        $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built;
37 1
        $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES);
38 1
        if (empty($key)) throw new \Exception("Cant serialize complex key: " . var_export($this->value, true));
39 1
        $parent->{trim($key, '\'" ')} = null;
40 1
        return null;
41
    }
42
43 1
    public function isAwaitingChild(NodeGeneric $child): bool
44
    {
45 1
        return is_null($this->value);
46
    }
47
}
48