Children::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace BestServedCold\HTMLBuilder\Output;
4
5
use BestServedCold\HTMLBuilder\Html\Node,
6
    BestServedCold\HTMLBuilder\Output;
7
8
/**
9
 * Class Children
10
 * 
11
 * @package BestServedCold\HTMLBuilder\Output
12
 */
13
class Children
14
{
15
    private $node;
16
    private $depth;
17
    private $content = [];
18
    private $string  = '';
19
20
    /**
21
     * Children constructor.
22
     * 
23
     * @param Node $node
24
     * @param int  $depth
25
     */
26 3
    public function __construct(Node $node, $depth)
27
    {
28 3
        $this->node    = $node;
29 3
        $this->depth   = $depth;
30 3
        $this->content = $node->getContent();
31 3
    }
32
33
    /**
34
     * @param  Node  $node
35
     * @param  int   $depth
36
     * @return string
37
     */
38 3
    public static function run(Node $node, $depth)
39
    {
40 3
        return (new static($node, $depth))->children();
41
    }
42
43
    /**
44
     * @return string
45
     */
46 3
    public function children()
47
    {
48 3
        $this->string .= $this->getContent();
49
50 3
        foreach ($this->node->getChildren() as $child) {
51 1
            $this->string .= $this->child($child) . $this->getContent();
52 3
        }
53
54 3
        return $this->string . $this->getContent();
55
    }
56
57
    /**
58
     * @return null|string
59
     */
60 3
    private function getContent()
61
    {
62 3
        $content = array_shift($this->content);
63 3
        return $content ? Format::string($content, $this->depth + 1) : null;
64
    }
65
66
    /**
67
     * @param  Node  $child
68
     * @return mixed
69
     */
70 1
    private function child(Node $child)
71
    {
72 1
        return (new Output($child, $this->depth + 1))->get();
73
    }
74
}
75