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

NodeCompactMapping   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 22
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __constructor() 0 9 2
A build() 0 8 3
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
}