Test Failed
Push — master ( 41f581...fc4f20 )
by stéphane
13:19
created

Builder::getScalar()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 36
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 7
nop 2
dl 0
loc 36
ccs 5
cts 5
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
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 static $dateAsObject = false;
22
23
    private static $_debug;
24
25
    const INVALID_DOCUMENT = "DOCUMENT %d is invalid,";
26
27
    /**
28
     * Builds a file.  check multiple documents & split if more than one documents
29
     *
30
     * @param Root $root  The NodeRoot node
31
     * @param int  $_debug    the level of debugging requested
32
     *
33
     * @return array|YamlObject|null   list of documents or just one.
34
     */
35 2
    public static function buildContent(Root $root, int $_debug = 0)
36
    {
37 2
        if ($_debug === 2) {
38 1
            print_r($root);
39 1
            return null;
40
        }
41 2
        self::$_debug = $_debug;
42 2
        $documents = [];
43 2
        $buffer = new NodeList();
44
        try {
45 2
            foreach ($root->value as $child) {
46 1
                if ($child instanceof DocEnd && $child !== $root->value->top()) {
47 1
                    self::pushAndSave($child, $buffer, $documents);
48 1
                } elseif ($child instanceof DocStart && $buffer->count() > 0 && $buffer->hasContent()) {
49 1
                    self::saveAndPush($child, $buffer, $documents);
50
                } else {
51 1
                    $buffer->push($child);
52
                }
53
            }
54 1
            $documents[] = self::buildDocument($buffer, count($documents) +1);
55 1
        } catch (\Exception|\Error|\ParseError $e) {
56 1
            throw new \Exception($e->getMessage(), 1, $e);
57
        }
58 1
        return count($documents) === 1 ? $documents[0] : $documents;
59
    }
60
61
    /**
62
     *  Builds the tree of Node (NodeList) for this document
63
     *
64
     * @param NodeList $list   the list of nodes that constitutes the current document
65
     * @param int      $docNum the index (starts @ 0) of this document in the whole YAML content provided to self::buildContent
66
     *
67
     * @return YamlObject the YAML document as an object
68
     */
69 6
    public static function buildDocument(NodeList &$list, int $docNum):YamlObject
70
    {
71 6
        $yamlObject = new YamlObject;
72 6
        $rootNode   = new Root();
73 6
        $list->setIteratorMode(NodeList::IT_MODE_DELETE);
74
        try {
75 6
            foreach ($list as $child) {
76 5
                $rootNode->add($child);
77
            }
78 5
            if (self::$_debug === 3) {
79 1
                echo "Document #$docNum\n";
80 1
                print_r($rootNode);
81
            }
82 5
            return $rootNode->build($yamlObject);
83 1
        } catch (\Exception|\Error|\ParseError $e) {
84 1
            throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $docNum).':'.$e->getMessage(), 2, $e);
85
        }
86
    }
87
88
    public static function pushAndSave(NodeGeneric $child, NodeList &$buffer, array &$documents)
89
    {
90
        $buffer->push($child);
91
        $documents[] = self::buildDocument($buffer, count($documents) + 1);
92
        $buffer = new NodeList();
93
    }
94
95
    public static function saveAndPush(NodeGeneric $child, NodeList &$buffer, array &$documents)
96
    {
97 4
        $documents[] = self::buildDocument($buffer, count($documents) + 1);
98
        $buffer = new NodeList($child);
99
    }
100
101
102
}
103