BasePresenter::renderRecursiveToString()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Vine\Presenters;
4
5
use Vine\Node;
6
use Vine\NodeCollection;
7
use Vine\Tree;
8
9
abstract class BasePresenter
10
{
11
    protected $collection;
12
13 9
    public function __construct()
14
    {
15 9
        $this->collection = new NodeCollection();
16 9
    }
17
18 9
    public function collection(NodeCollection $collection)
19
    {
20 9
        $this->collection = $collection;
21
22 9
        return $this;
23
    }
24
25
    /**
26
     * Render collection
27
     *
28
     * @return string
29
     */
30
    abstract public function render();
31
32
    /**
33
     * Render each branch and its children recursively
34
     *
35
     * @param NodeCollection $nodeCollection
36
     * @param int $level
37
     * @return string
38
     */
39
    protected function renderRecursiveToString(NodeCollection $nodeCollection, $level = 0): string
40
    {
41
        $output = '';
42
43
        foreach($nodeCollection as $node)
44
        {
45
            $output .= $this->template($node,$level);
46
47
            if(!$node->isLeaf()) $output .= $this->renderRecursiveToString($node->children(),$level+1);
48
        }
49
50
        return $output;
51
    }
52
53
    /**
54
     * Template for a single node entry.
55
     *
56
     * @param Node $node
57
     * @param int $level
58
     * @return string
59
     */
60
    abstract protected function template(Node $node,$level = 0);
61
}
62