CompactSequence::build()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 3
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 CompactSequence 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::SEQUENCE_VALUES, trim(substr(trim($nodeString), 1, -1)), $matches);
23 2
        foreach ($matches['item'] as $key => $item) {
24 2
            $i = new Item('', (int) $line);
25 2
            $i->indent = null;
26 2
            $itemValue = NodeFactory::get(trim($item));
27 2
            $itemValue->indent = null;
28 2
            $i->add($itemValue);
29 2
            $this->add($i);
30
        }
31
    }
32
33 1
    public function build(&$parent = null): ?Compact
34
    {
35 1
        if (is_null($this->value)) {
36 1
            return null;
37
        }
38 1
        if ($this->value instanceof NodeGeneric) {
39 1
            $this->value = new NodeList($this->value);
40 1
            $this->value->type = NodeList::SEQUENCE;
41
        }
42 1
        $arr = (array) $this->value->build();
43 1
        return new Compact($arr);
44
    }
45
}
46