Builder::buildContent()   B
last analyzed

Complexity

Conditions 9
Paths 34

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.1582

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 23
ccs 14
cts 16
cp 0.875
rs 8.0555
c 0
b 0
f 0
cc 9
nc 34
nop 1
crap 9.1582
1
<?php
2
3
namespace Dallgoot\Yaml;
4
5
use Dallgoot\Yaml\Nodes\Root;
6
use Dallgoot\Yaml\Nodes\DocEnd;
7
use Dallgoot\Yaml\Nodes\DocStart;
8
use Dallgoot\Yaml\Types\YamlObject;
9
10
/**
11
 * Constructs the result (YamlObject or array) according to every Nodes and their values
12
 *
13
 * @author  Stéphane Rebai <[email protected]>
14
 * @license Apache 2.0
15
 * @link    https://github.com/dallgoot/yaml
16
 */
17
class Builder
18
{
19
    public bool $dateAsObject = false;
20
21
    private int $_options;
22
    private int $_debug = 0;
23
24
    const INVALID_DOCUMENT = "DOCUMENT %d is invalid,";
25
26 9
    public function __construct($options, $debug)
27
    {
28 9
        $this->_options = $options;
29 9
        $this->_debug = $debug;
30
    }
31
    /**
32
     * Builds a YAM content.  check multiple documents & split if more than one documents
33
     *
34
     * @param Root $root  The NodeRoot node
35
     *
36
     * @return array|YamlObject|null   list of documents or just one, null if appropriate debug lvl
37
     */
38 4
    public function buildContent(Root $root)
39
    {
40 4
        switch ($this->_debug) {
41 4
            case 2 : print_r($root);// fall-through
42 4
            case 1 : return null;
43
        }
44 3
        $documents = [];
45 3
        $buffer = new NodeList();
46
        try {
47 3
            foreach ($root->value as $child) {
48 3
                if ($child instanceof DocEnd && $child !== $root->value->top()) {
49 1
                    $this->pushAndSave($child, $buffer, $documents);
50 3
                } elseif ($child instanceof DocStart) {
51 1
                    $this->saveAndPush($child, $buffer, $documents);
52
                } else {
53 3
                    $buffer->push($child);
54
                }
55
            }
56 3
            $documents[] = $this->buildDocument($buffer, count($documents) + 1);
57
        } catch (\Throwable $e) {
58
            throw new \Exception($e->getMessage(), 1, $e);
59
        }
60 3
        return count($documents) === 1 ? $documents[0] : $documents;
61
    }
62
63
    /**
64
     *  Builds the tree of Node (NodeList) for this document
65
     *
66
     * @param NodeList $list   the list of nodes that constitutes the current document
67
     * @param int      $docNum the index (starts @ 0) of this document in the whole YAML content provided to $this->buildContent
68
     *
69
     * @return YamlObject the YAML document as an object
70
     */
71 7
    public function buildDocument(NodeList &$list, int $docNum): YamlObject
72
    {
73 7
        $yamlObject = new YamlObject($this->_options);
74 7
        $rootNode   = new Root();
75 7
        $list->setIteratorMode(NodeList::IT_MODE_DELETE);
76
        try {
77 7
            foreach ($list as $child) {
78 6
                $rootNode->add($child);
79
            }
80 7
            if ($this->_debug === 3) {
81 1
                echo "Document #$docNum\n";
82 1
                print_r($rootNode);
83
            }
84 7
            return $rootNode->build($yamlObject);
85
        } catch (\Throwable $e) {
86
            throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $docNum) . ':' . $e->getMessage(), 2, $e);
87
        }
88
    }
89
90 2
    public function pushAndSave(DocEnd $child, NodeList &$buffer, array &$documents)
91
    {
92 2
        $buffer->push($child);
93 2
        $documents[] = $this->buildDocument($buffer, count($documents) + 1);
94 2
        $buffer = new NodeList();
95
    }
96
97 2
    public function saveAndPush(DocStart $child, NodeList &$buffer, array &$documents)
98
    {
99 2
        if ($buffer->count() > 0 && $buffer->hasContent()) {
100 2
            $documents[] = $this->buildDocument($buffer, count($documents) + 1);
101 2
            $buffer = new NodeList($child);
102
        } else {
103 1
            $buffer->push($child);
104
        }
105
    }
106
}
107