CompactMapping::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 9
ccs 8
cts 8
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\NodeList;
7
use Dallgoot\Yaml\Types\Compact;
8
use Dallgoot\Yaml\Regex;
9
use Dallgoot\Yaml\Nodes\Generic\NodeGeneric;
10
11
/**
12
 *
13
 * @author  Stéphane Rebai <[email protected]>
14
 * @license Apache 2.0
15
 * @link    https://github.com/dallgoot/yaml
16
 */
17
class CompactMapping extends NodeGeneric
18
{
19 2
    public function __construct(string $nodeString, ?int $line)
20
    {
21 2
        parent::__construct($nodeString, $line);
22 2
        preg_match_all(Regex::MAPPING_VALUES, trim(substr(trim($nodeString), 1, -1)), $matches);
23 2
        foreach ($matches['k'] as $index => $property) {
24 2
            $pair = $property . ': ' . trim($matches['v'][$index]);
25 2
            $child = NodeFactory::get($pair, (int) $line);
26 2
            $child->indent = null;
27 2
            $this->add($child);
28
        }
29
    }
30
31 1
    public function build(&$parent = null): ?Compact
32
    {
33 1
        if (is_null($this->value)) {
34 1
            return null;
35
        }
36 1
        if ($this->value instanceof NodeGeneric) {
37 1
            $this->value = new NodeList($this->value);
38 1
            $this->value->type = NodeList::MAPPING;
39
        }
40 1
        $obj = (object) $this->value->build();
41 1
        return new Compact($obj);
42
    }
43
}
44