Completed
Push — master ( 0750a6...119392 )
by Pol
02:19
created

HtmlBuilder::update()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\htmltag;
6
7
use drupol\htmltag\Tag\TagFactory;
8
use drupol\htmltag\Tag\TagInterface;
9
10
/**
11
 * Class HtmlBuilder.
12
 */
13
final class HtmlBuilder implements StringableInterface
14
{
15
    /**
16
     * The tag scope.
17
     *
18
     * @var null|\drupol\htmltag\Tag\TagInterface
19
     */
20
    private $scope;
21
22
    /**
23
     * The storage.
24
     *
25
     * @var \drupol\htmltag\Tag\TagInterface[]|string[]
26
     */
27
    private $storage;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function __call($name, array $arguments = [])
33
    {
34 1
        if ('c' === $name) {
35 1
            if (!isset($arguments[0])) {
36 1
                return $this;
37
            }
38
39 1
            return $this->update(
40 1
                HtmlTag::tag('!--', [], $arguments[0])
41
            );
42
        }
43
44 1
        if ('_' === $name) {
45 1
            $this->scope = null;
46
47 1
            return $this;
48
        }
49
50 1
        $tag = TagFactory::build($name, ...$arguments);
51
52 1
        return $this->update($tag, true);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function __toString()
59
    {
60 1
        $output = '';
61
62 1
        foreach ($this->storage as $item) {
63 1
            $output .= $item;
64
        }
65
66 1
        return $output;
67
    }
68
69
    /**
70
     * Add the current tag to the stack.
71
     *
72
     * @param \drupol\htmltag\Tag\TagInterface $tag
73
     *   The tag
74
     * @param bool $updateScope
75
     *   True if the current scope needs to be updated
76
     *
77
     * @return \drupol\htmltag\HtmlBuilder
78
     *   The HTML Builder object
79
     */
80 1
    private function update(TagInterface $tag, $updateScope = false)
81
    {
82 1
        if (null !== $this->scope) {
83 1
            $this->scope->content($this->scope->getContentAsArray(), $tag);
84
        } else {
85 1
            $this->storage[] = $tag;
86
        }
87
88 1
        if (true === $updateScope) {
89 1
            $this->scope = $tag;
90
        }
91
92 1
        return $this;
93
    }
94
}
95