FileSelect   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 21
c 1
b 0
f 0
dl 0
loc 56
ccs 21
cts 21
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptionGroup() 0 10 4
A renderTree() 0 9 2
A getOption() 0 3 2
A getInput() 0 6 1
1
<?php
2
3
namespace kalanis\kw_tree_controls\Controls;
4
5
6
use kalanis\kw_forms\Controls\AControl;
7
use kalanis\kw_forms\Controls\SelectOption;
8
use kalanis\kw_forms\Exceptions\RenderException;
9
use kalanis\kw_templates\HtmlElement;
10
use kalanis\kw_tree\Essentials\FileNode;
11
use kalanis\kw_tree_controls\ControlNode;
12
13
14
/**
15
 * Class FileSelect
16
 * @package kalanis\kw_tree_controls\Controls
17
 */
18
class FileSelect extends ATreeControl
19
{
20
    use TSimpleValue;
21
22
    protected bool $wantEmptySub = false;
23
24
    /**
25
     * @param ControlNode|null $baseNode
26
     * @throws RenderException
27
     * @return string
28
     */
29 1
    protected function renderTree(?ControlNode $baseNode): string
30
    {
31 1
        if (empty($baseNode)) {
32 1
            return '';
33
        }
34 1
        $select = HtmlElement::init('select');
35 1
        $select->setAttribute('name', $this->key);
36 1
        $select->addChild($this->getOptionGroup($baseNode));
37 1
        return $select->render();
38
    }
39
40
    /**
41
     * @param ControlNode $node
42
     * @throws RenderException
43
     * @return string
44
     */
45 1
    protected function getOptionGroup(ControlNode $node): string
46
    {
47 1
        if ($node->getNode() && $node->getNode()->isDir()) {
48 1
            $group = HtmlElement::init('optgroup', ['label' => $this->stringPath($node->getNode())]);
49 1
            foreach ($node->getSubNodes() as $subNode) {
50 1
                $group->addChild($this->getOptionGroup($subNode));
51
            }
52 1
            return strval($group);
53
        } else {
54 1
            return $this->getOption($node);
55
        }
56
    }
57
58
    /**
59
     * @param ControlNode $node
60
     * @throws RenderException
61
     * @return string
62
     */
63 1
    protected function getOption(ControlNode $node): string
64
    {
65 1
        return $node->getControl() ? $node->getControl()->render() : '';
66
    }
67
68 1
    protected function getInput(FileNode $node): AControl
69
    {
70 1
        $input = new SelectOption();
71 1
        $input->setEntry($this->getKey(), $this->stringPath($node), $this->stringName($node));
72 1
        $this->inputs[] = $input;
73 1
        return $input;
74
    }
75
}
76