Passed
Push — master ( 42571c...f8295f )
by stéphane
09:20
created

DocStart::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
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\Regex;
7
8
/**
9
 *
10
 * @author  Stéphane Rebai <[email protected]>
11
 * @license Apache 2.0
12
 * @link    https://github.com/dallgoot/yaml
13
 */
14
class DocStart extends NodeGeneric
15
{
16 5
    public function __construct(string $nodeString, int $line)
17
    {
18 5
        parent::__construct($nodeString, $line);
19 5
        $rest = substr(ltrim($nodeString), 3);
20 5
        if (!empty($rest)) {
21 1
            $n = NodeFactory::get($rest, $line);
22 1
            $n->indent = null;
23 1
            $this->add($n);
24
        }
25 5
    }
26
27 3
    public function add(NodeGeneric $child):NodeGeneric
28
    {
29 3
        if ($this->value instanceof NodeGeneric) {
30 1
            return $this->value->add($child);
31
        } else {
32 3
            return parent::add($child);
33
        }
34
    }
35
36 1
    public function build(&$parent = null)
37
    {
38 1
        if (is_null($parent)) {
39
            throw new \Exception(__METHOD__." expects a YamlObject as parent", 1);
40
        }
41 1
        if (!is_null($this->value)) {
42 1
            if ($this->value instanceof Tag){
43 1
                preg_match(Regex::TAG_PARTS, $this->value->raw, $tagparts);
44 1
                if (preg_match("/(?(DEFINE)".Regex::TAG_URI.')(?&url)/', $tagparts['tagname'], $matches)) {
45
                    // throw new \UnexpectedValueException("Tag '".$this->value->raw."' is invalid", 1);
46 1
                    $parent->addTag($tagparts['handle'], $tagparts['tagname']);
47
                    // var_dump('HERE');
48
                } else {
49
                    // var_dump('THERE');
50 1
                    $this->value->build($parent);
51
                }
52
            } else {
53 1
                $text = $this->value->build($parent);
54 1
                !is_null($text) && $parent->setText($text);
55
            }
56
        }
57 1
        return null;
58
    }
59
60 1
    public function isAwaitingChild(NodeGeneric $node):bool
61
    {
62 1
        return $this->value instanceof NodeGeneric && $this->value->isOneOf('Anchor', 'Literal', 'LiteralFolded');
63
    }
64
65 1
    public function getTargetOnEqualIndent(NodeGeneric &$node):NodeGeneric
66
    {
67 1
        if ($this->value instanceof NodeGeneric) {
68 1
            if ($this->value instanceof Tag) {
69
                if (!preg_match("/".Regex::TAG_URI."/", $this->value->raw)) {
70
                    return $this->value;
71
                }
72 1
            } elseif ($this->value->isAwaitingChild($node)) {
73 1
                return $this->value;
74
            }
75
        }
76 1
        return $this->getParent();
77
    }
78
}