SelectList::setValues()   B
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
nc 15
nop 1
dl 0
loc 13
ccs 10
cts 10
cp 1
crap 7
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_forms\Controls;
4
5
6
use kalanis\kw_forms\Interfaces\IMultiValue;
7
8
9
/**
10
 * Class SelectList
11
 * @package kalanis\kw_forms\Controls
12
 * Form element for selection - long list with multiple values to set
13
 */
14
class SelectList extends AControl implements IMultiValue
15
{
16
    use TMultiple;
17
18
    protected string $templateInput = '<select %2$s>%3$s</select>';
19
20
    /**
21
     * Create form element Select - variant for list
22
     * @param string $alias
23
     * @param string $label
24
     * @param iterable<string, string|SelectOption> $children
25
     * @param int|null $size
26
     * @return $this
27
     */
28 2
    public function set(string $alias, string $label = '', iterable $children = [], ?int $size = null): self
29
    {
30 2
        foreach ($children as $childAlias => $child) {
31 2
            if ($child instanceof SelectOption) {
32 1
                $this->addChild($child, $childAlias);
33
            } else {
34 2
                $this->addOption(strval($childAlias), $childAlias, strval($child));
35
            }
36
        }
37 2
        $this->setEntry($alias, '', $label);
38 2
        if (!is_null($size) && (0 != $size)) {
39 1
            $this->setSize($size);
40
        }
41 2
        $this->setAttribute('id', $this->getKey());
42 2
        return $this;
43
    }
44
45
    /**
46
     * Add simple option into select
47
     * @param string $alias
48
     * @param string|int|float|bool|null $value
49
     * @param string $label
50
     * @return SelectOption
51
     */
52 2
    public function addOption(string $alias, $value, string $label = ''): SelectOption
53
    {
54 2
        $option = new SelectOption();
55 2
        $option->setEntry($alias, $value, $label);
56 2
        $this->addChild($option, $alias);
57 2
        return $option;
58
    }
59
60 1
    public function getValue()
61
    {
62 1
        foreach ($this->children as $child) {
63 1
            if ($child instanceof SelectOption) {
64 1
                $value = $child->getValue();
65 1
                if (!empty($value)) {
66 1
                    return $value;
67
                }
68
            }
69
        }
70 1
        return '';
71
    }
72
73 1
    public function setValue($value): void
74
    {
75 1
        foreach ($this->children as $child) {
76 1
            if ($child instanceof SelectOption) {
77 1
                $child->setValue($value);
78
            }
79
        }
80 1
    }
81
82 1
    public function getValues(): array
83
    {
84 1
        $result = [];
85 1
        foreach ($this->children as $child) {
86 1
            if ($child instanceof SelectOption) {
87 1
                $result[$child->getKey()] = $child->getValue();
88
            }
89
        }
90 1
        return $result;
91
    }
92
93 1
    public function setValues(array $data): void
94
    {
95 1
        foreach ($this->children as $child) {
96 1
            if ($child instanceof SelectOption) {
97 1
                $child->setValue('');
98
            }
99
        }
100 1
        foreach ($data as $value) {
101 1
            foreach ($this->children as $child) {
102 1
                if ($child instanceof SelectOption) {
103 1
                    if ($child->getOriginalValue() == $value) {
104 1
                        $child->setValue($child->getOriginalValue());
105 1
                        break;
106
                    }
107
                }
108
            }
109
        }
110 1
    }
111
112 1
    public function renderInput($attributes = null): string
113
    {
114 1
        $this->addAttributes($attributes);
115 1
        $this->setAttribute('name', $this->getMultiple() ? $this->getKey() . '[]' : $this->getKey());
116 1
        return $this->wrapIt(sprintf($this->templateInput, '', $this->renderAttributes(), $this->renderChildren()), $this->wrappersInput);
117
    }
118
}
119