Completed
Push — master ( 8c011c...0282a0 )
by Pol
13:26
created

HtmlBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 17 3
A __toString() 0 9 1
1
<?php
2
3
namespace drupol\htmltag;
4
5
use drupol\htmltag\Tag\TagFactory;
6
7
/**
8
 * Class HtmlBuilder
9
 */
10
final class HtmlBuilder
11
{
12
    /**
13
     * @var \drupol\htmltag\Tag\TagInterface
14
     */
15
    private $currenttag;
16
17
    /**
18
     * @var \drupol\htmltag\Tag\TagInterface[]
19
     */
20
    private $storage;
21
22
    /**
23
     * @param $name
24
     * @param $arguments
25
     *
26
     * @return $this
27
     */
28
    public function __call($name, $arguments) {
29
        if ('end' !== $name) {
30
            $tag = TagFactory::build($name, ...$arguments);
31
32
            if (null !== $this->currenttag) {
33
                $this->currenttag->content($this->currenttag->content(), $tag);
34
            } else {
35
                $this->currenttag = $tag;
36
            }
37
38
            $this->storage[spl_object_hash($this->currenttag)] = $this->currenttag;
39
        } else {
40
            $this->currenttag = null;
41
        }
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function __toString() {
50
        return array_reduce(
51
            $this->storage,
52
            function ($carry, $item) {
53
                return $carry . $item->render();
54
            },
55
            ''
56
        );
57
    }
58
}
59