Test Failed
Push — master ( 529ac3...bfbdcf )
by stéphane
06:08
created

Builder   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 86
ccs 38
cts 38
cp 1
rs 10
c 1
b 0
f 0
wmc 17

5 Methods

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