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

NodeCompactMapping::__constructor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
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 NodeCompactMapping extends Node
12
{
13
    public function __constructor(string $nodeString, int $line)
14
    {
15
        parent::__construct($nodeString, $line);
16
        preg_match_all(Regex::MAPPING_VALUES, trim(substr(ltrim($nodeString), 1,-1)), $matches);
17
        foreach ($matches['k'] as $index => $property) {
18
            $pair = $property.': '.trim($matches['v'][$index]);
19
            // $fakeMatches = [0 => $property, 1 => trim($matches['v'][$index])];
20
            // $this->add(new NodeKey($pair, $line, $fakeMatches));
21
            $this->add(NodeFactory::get($pair, $line));
22
        }
23
    }
24
25
    public function build(&$parent = null)
26
    {
27
        $out = $parent ?? new \StdClass;
28
        $tmp = $this->value instanceof Node ? new NodeList($this->value) : $this->value;
29
        foreach ($tmp as $child) {
30
            $child->build($out);
31
        }
32
        return new Compact($out);
33
    }
34
35
36
}