Select   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 80
ccs 34
cts 34
cp 1
rs 10
c 2
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 18 5
A setValue() 0 5 3
A getValue() 0 11 4
A addOption() 0 6 1
A addGroup() 0 6 1
1
<?php
2
3
namespace kalanis\kw_forms\Controls;
4
5
6
/**
7
 * Class Select
8
 * @package kalanis\kw_forms\Controls
9
 * Form element for selection - dropdown menu
10
 */
11
class Select extends AControl
12
{
13
    protected string $templateInput = '<select %2$s>%3$s</select>';
14
15
    /**
16
     * Create element of form entry Select
17
     * @param string $alias
18
     * @param string|int|float|null $value
19
     * @param string $label
20
     * @param iterable<string, string|int|float|SelectOptgroup|SelectOption|iterable<string, string|SelectOption>> $children
21
     * @return $this
22
     */
23 2
    public function set(string $alias, $value = null, string $label = '', iterable $children = []): self
24
    {
25
        // transfer child values arrays to option group or single entries into options
26 2
        foreach ($children as $childAlias => $child) {
27 1
            if (is_iterable($child)) {
28 1
                $this->addGroup(strval($childAlias), $child, strval($childAlias));
29 1
            } elseif ($child instanceof SelectOptgroup) {
30 1
                $this->addChild($child, $childAlias);
31 1
            } elseif ($child instanceof SelectOption) {
32 1
                $this->addChild($child, $childAlias);
33
            } else {
34 1
                $this->addOption(strval($childAlias), $childAlias, strval($child));
35
            }
36
        }
37 2
        $this->setEntry($alias, '', $label);
38 2
        $this->setValue($value);
39 2
        $this->setAttribute('id', $this->getKey());
40 2
        return $this;
41
    }
42
43
    /**
44
     * Add group into select
45
     * @param string $alias
46
     * @param iterable<string, string|SelectOption> $values
47
     * @param string $label
48
     * @return SelectOptgroup
49
     */
50 1
    public function addGroup(string $alias, iterable $values, string $label = '')
51
    {
52 1
        $options = new SelectOptgroup();
53 1
        $options->set($alias, $label, $values);
54 1
        $this->addChild($options, $alias);
55 1
        return $options;
56
    }
57
58
    /**
59
     * Add simple option into select
60
     * @param string $alias
61
     * @param string $value
62
     * @param string $label
63
     * @return SelectOption
64
     */
65 1
    public function addOption(string $alias, $value, string $label = '')
66
    {
67 1
        $option = new SelectOption();
68 1
        $option->setEntry($alias, $value, $label);
69 1
        $this->addChild($option, $alias);
70 1
        return $option;
71
    }
72
73 1
    public function getValue()
74
    {
75 1
        foreach ($this->children as $child) {
76 1
            if ($child instanceof AControl) {
77 1
                $returnValues = $child->getValue();
78 1
                if (!empty($returnValues)) {
79 1
                    return $returnValues;
80
                }
81
            }
82
        }
83 1
        return '';
84
    }
85
86 2
    public function setValue($value): void
87
    {
88 2
        foreach ($this->children as $child) {
89 1
            if ($child instanceof AControl) {
90 1
                $child->setValue($value);
91
            }
92
        }
93 2
    }
94
}
95