ATreeControl::getEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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