DirSelect::getInput()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
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_forms\Exceptions\RenderException;
8
use kalanis\kw_templates\HtmlElement;
9
use kalanis\kw_tree\Essentials\FileNode;
10
use kalanis\kw_tree_controls\ControlNode;
11
12
13
/**
14
 * Class DirSelect
15
 * @package kalanis\kw_tree_controls\Controls
16
 */
17
class DirSelect extends ATreeControl
18
{
19
    use TSimpleValue;
20
21
    protected bool $wantEmptySub = false;
22
23
    /**
24
     * @param ControlNode|null $baseNode
25
     * @throws RenderException
26
     * @return string
27
     */
28 1
    protected function renderTree(?ControlNode $baseNode): string
29
    {
30 1
        if (empty($baseNode)) {
31 1
            return '';
32
        }
33 1
        $select = HtmlElement::init('select');
34 1
        $select->setAttribute('name', $this->key);
35 1
        $select->addChild($this->fillOptions([$baseNode]));
36 1
        return $select->render();
37
    }
38
39
    /**
40
     * @param ControlNode[] $nodes
41
     * @throws RenderException
42
     * @return string
43
     */
44 1
    protected function fillOptions(array $nodes): string
45
    {
46 1
        $result = [];
47 1
        foreach ($nodes as $subNode) {
48 1
            $result[] = $this->getOption($subNode)
49 1
                . $this->fillOptions($subNode->getSubNodes());
50
        }
51 1
        return implode('', $result);
52
    }
53
54
    /**
55
     * @param ControlNode $node
56
     * @throws RenderException
57
     * @return string
58
     */
59 1
    protected function getOption(ControlNode $node): string
60
    {
61 1
        return $node->getControl() ? $node->getControl()->render() : '';
62
    }
63
64 1
    protected function getInput(FileNode $node): Controls\AControl
65
    {
66 1
        $path = $this->stringPath($node);
67 1
        $input = new Controls\SelectOption();
68 1
        $input->setEntry($this->getKey(), $path, $path . DIRECTORY_SEPARATOR);
69 1
        $this->inputs[] = $input;
70 1
        return $input;
71
    }
72
}
73