Passed
Push — master ( e89a22...38e786 )
by Petr
08:13
created

Checkboxes::setValues()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
nc 3
nop 1
dl 0
loc 14
ccs 9
cts 10
cp 0.9
crap 5.025
rs 9.6111
c 2
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_forms\Controls;
4
5
6
use kalanis\kw_forms\Exceptions\RenderException;
7
use kalanis\kw_forms\Interfaces\IMultiValue;
8
9
10
/**
11
 * Definition of form controls Group of Checkboxes
12
 *
13
 * <b>Examples</b>
14
 * <code>
15
 * // render form for set 2 values, second will be empty
16
 * $form = new Form();
17
 * $checkboxes = $form->addCheckboxes('fotos', 'Choose files');
18
 * $checkboxes->addCheckbox('file1', 'File 1'));
19
 * $checkboxes->addCheckbox('file2', 'File 2', true));
20
 * echo $form;
21
 *
22
 * // render form for setting 5 values, first two will be set
23
 * $form = new Form();
24
 * for($i=1;$i<6;$i++) {
25
 *     $files[] = $form->getControlFactory()->getCheckbox('', 1, 'File '.$i);
26
 * }
27
 * $checkboxes = $form->addCheckboxes('fotos', 'Select files', ['0', 1], $files);
28
 * echo $form;
29
 * </code>
30
 */
31
class Checkboxes extends AControl implements IMultiValue
32
{
33
    use TShorterKey;
34
35
    public $templateLabel = '<label>%2$s</label>';
36
    public $templateInput = '%3$s';
37
38
    /**
39
     * Create group of form elements Checkbox
40
     * @param string $alias
41
     * @param array<string, string|int|float|bool> $value
42
     * @param string|null $label
43
     * @param iterable<string, string|Checkbox> $children
44
     * @return $this
45
     */
46 2
    public function set(string $alias, array $value = [], ?string $label = null, iterable $children = array())
47
    {
48 2
        $this->alias = $alias;
49 2
        $this->setLabel($label);
50
51 2
        foreach ($children as $childLabel => $childValue) {
52 2
            if ($childValue instanceof Checkbox) {
53 1
                $this->addChild($childValue, $childLabel);
54
            } else {
55 2
                $this->addCheckbox(strval($childLabel), strval($childValue), $childValue);
56
            }
57
        }
58
59 2
        if (!empty($value)) {
60 2
            $this->setValues($value);
61
        }
62 2
        return $this;
63
    }
64
65
    /**
66
     * Create single Checkbox element
67
     * @param string $alias
68
     * @param string $label
69
     * @param string $value
70
     * @param boolean $checked
71
     * @return Checkbox
72
     */
73 2
    public function addCheckbox(string $alias, string $label, $value, $checked = null)
74
    {
75 2
        $checkbox = new Checkbox();
76 2
        $checkbox->set($alias, $value, $label);
77 2
        $checkbox->setValue(strval($checked));
78 2
        $this->addChild($checkbox);
79 2
        return $checkbox;
80
    }
81
82
    /**
83
     * Get statuses of all children
84
     * @return array<string, string|int|float|bool|null>
85
     */
86 1
    public function getValues(): array
87
    {
88 1
        $array = array();
89 1
        foreach ($this->children as $child) {
90 1
            if ($child instanceof Checkbox) {
91 1
                $array[$child->getKey()] = $child->getValue();
92
            }
93
        }
94 1
        return $array;
95
    }
96
97
    /**
98
     * Set values to all children
99
     * !! UNDEFINED values will be SET too !!
100
     * @param array<string, string|int|float|bool|null> $array
101
     */
102 2
    public function setValues(array $array = []): void
103
    {
104 2
        foreach ($this->children as $child) {
105 2
            if ($child instanceof Checkbox) {
106 2
                $shortKey = $this->shorterKey($child->getKey());
107 2
                $child->setValue(
108 2
                isset($array[$shortKey])
109 1
                    ? $array[$shortKey]
110
                    : (
111 2
                    isset($array[$child->getKey()])
112
                        // @codeCoverageIgnoreStart
113
                        ? $array[$child->getKey()] // dunno how to test now, probably directly with "foo[bar]" variable
114
                        // @codeCoverageIgnoreEnd
115 2
                        : ''
116
                    )
117
                );
118
            }
119
        }
120 2
    }
121
122 1
    public function renderInput($attributes = null): string
123
    {
124 1
        return $this->wrapIt(sprintf($this->templateInput, '', '', $this->renderChildren()), $this->wrappersInput);
125
    }
126
127
    /**
128
     * Render all children, add missing prefixes
129
     * @throws RenderException
130
     * @return string
131
     */
132 1
    public function renderChildren(): string
133
    {
134 1
        $return = '';
135 1
        foreach ($this->children as $child) {
136 1
            if ($child instanceof AControl) {
137 1
                $child->setAttribute('id', $this->getAlias() . '_' . $child->getKey());
138
            }
139
140 1
            $return .= $this->wrapIt($child->render(), $this->wrappersChild) . PHP_EOL;
141
        }
142 1
        return $this->wrapIt($return, $this->wrappersChildren);
143
    }
144
}
145