Passed
Push — master ( 87aa04...fc7ae1 )
by Petr
08:05
created

ATreeControl::stringName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace kalanis\kw_tree_controls\Controls;
4
5
6
use kalanis\kw_forms\Controls;
7
use kalanis\kw_paths\Interfaces\IPaths;
8
use kalanis\kw_tree\Essentials\FileNode;
9
use kalanis\kw_tree_controls\ControlNode;
10
11
12
/**
13
 * Class ATreeControl
14
 * @package kalanis\kw_tree_controls\Controls
15
 */
16
abstract class ATreeControl extends Controls\AControl
17
{
18
    protected $templateInput = '%1$s'; // by our own!
19
    /** @var ControlNode|null */
20
    protected $tree = null;
21
    /** @var Controls\AControl[]|Controls\Checkbox[]|Controls\Radio[]|Controls\SelectOption[] */
22
    protected $inputs = [];
23
    /** @var bool */
24
    protected $wantEmptySub = true;
25
26 7
    public function set(string $key, string $value = '', string $label = '', ?FileNode $tree = null, bool $wantRootControl = true): self
27
    {
28 7
        $this->setEntry($key, $value, $label);
29 7
        $this->tree = $this->fillTreeControl($tree, $wantRootControl);
30 7
        $this->setValue($value);
31 7
        return $this;
32
    }
33
34 6
    public function renderInput($attributes = null): string
35
    {
36 6
        $this->addAttributes($attributes);
37 6
        return $this->wrapIt(sprintf($this->templateInput, $this->renderTree($this->tree)), $this->wrappersInput);
38
    }
39
40 7
    protected function fillTreeControl(?FileNode $baseNode, bool $wantRootControl): ?ControlNode
41
    {
42 7
        if (!$baseNode) {
43 7
            return null;
44
        }
45 6
        $node = $this->getControlNode();
46 6
        if ($wantRootControl) {
47 6
            $node->setControl($this->getInput($baseNode));
48 6
        } elseif ($this->wantEmptySub) {
49 4
            $node->setControl($this->getEmpty($baseNode));
50
        } // no else -> no control
51 6
        $node->setNode($baseNode);
52 6
        foreach ($baseNode->getSubNodes() as $subNode) {
53 6
            if ($subControl = $this->fillTreeControl($subNode, true)) {
54 6
                $node->addSubNode($subControl);
55
            }
56
        }
57 6
        return $node;
58
    }
59
60 6
    protected function getControlNode(): ControlNode
61
    {
62 6
        return new ControlNode();
63
    }
64
65
    abstract protected function getInput(FileNode $node): Controls\AControl;
66
67 4
    protected function getEmpty(FileNode $node): Controls\AControl
68
    {
69 4
        $input = new EmptyControl();
70 4
        $input->set($this->getKey(), null, $this->stringName($node));
71 4
        return $input;
72
    }
73
74
    abstract protected function renderTree(?ControlNode $baseNode): string;
75
76 6
    protected function stringName(?FileNode $node): string
77
    {
78 6
        if (is_null($node)) {
79 1
            return '';
80
        }
81 6
        $path = $node->getPath();
82 6
        $last = end($path);
83 6
        return (false !== $last) ? strval($last) : IPaths::SPLITTER_SLASH;
84
    }
85
86 7
    protected function stringPath(?FileNode $node): string
87
    {
88 7
        return is_null($node)
89 1
            ? IPaths::SPLITTER_SLASH
90 7
            : implode(IPaths::SPLITTER_SLASH, $node->getPath());
91
    }
92
}
93