1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_forms\Exceptions\RenderException; |
7
|
|
|
use kalanis\kw_forms\Interfaces; |
8
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
9
|
|
|
use kalanis\kw_templates\Interfaces\IHtmlElement; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait TSubControls |
14
|
|
|
* @package kalanis\kw_forms\Controls |
15
|
|
|
* Trait for rendering other controls |
16
|
|
|
*/ |
17
|
|
|
trait TSubControls |
18
|
|
|
{ |
19
|
|
|
/** @var array<string, AControl> */ |
20
|
|
|
protected array $controls = []; |
21
|
|
|
|
22
|
10 |
|
public function addControl(string $key, AControl $control): void |
23
|
|
|
{ |
24
|
10 |
|
$this->controls[$key] = $control; |
25
|
10 |
|
} |
26
|
|
|
|
27
|
6 |
|
public function getControl(string $key): ?AControl |
28
|
|
|
{ |
29
|
6 |
|
foreach ($this->controls as &$control) { |
30
|
6 |
|
if ($control instanceof Interfaces\IContainsControls && $control->/** @scrutinizer ignore-call */hasControl($key)) { |
31
|
1 |
|
return $control->/** @scrutinizer ignore-call */getControl($key); |
32
|
6 |
|
} elseif ($control instanceof AControl) { |
33
|
6 |
|
if ($control->getKey() == $key) { |
34
|
5 |
|
return $control; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
5 |
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get values of all children |
43
|
|
|
* @return array<string, string|int|float|bool|null> |
44
|
|
|
*/ |
45
|
1 |
|
public function getValues(): array |
46
|
|
|
{ |
47
|
1 |
|
$array = []; |
48
|
1 |
|
foreach ($this->controls as &$control) { |
49
|
|
|
/** @var AControl $control */ |
50
|
1 |
|
if ($control instanceof Interfaces\IMultiValue) { |
51
|
1 |
|
$array += $control->/** @scrutinizer ignore-call */getValues(); |
52
|
|
|
} else { |
53
|
1 |
|
$array[$control->/** @scrutinizer ignore-call */getKey()] = $control->/** @scrutinizer ignore-call */getValue(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
1 |
|
return $array; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set values to all children, !!undefined values will NOT be set!! |
61
|
|
|
* <b>Usage</b> |
62
|
|
|
* <code> |
63
|
|
|
* $form->setValues($this->context->post) // set values from Post |
64
|
|
|
* $form->setValues($mapperObject) // set values from other source |
65
|
|
|
* </code> |
66
|
|
|
* @param array<string, string|int|float|bool|null> $data |
67
|
|
|
*/ |
68
|
4 |
|
public function setValues(array $data = []): void |
69
|
|
|
{ |
70
|
4 |
|
foreach ($this->controls as &$control) { |
71
|
|
|
/** @var AControl $control */ |
72
|
4 |
|
if ($control instanceof Interfaces\IMultiValue) { |
73
|
3 |
|
$control->/** @scrutinizer ignore-call */setValues($data); |
74
|
|
|
} else { |
75
|
4 |
|
if (isset($data[$control->getKey()])) { |
76
|
4 |
|
$control->setValue($data[$control->getKey()]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
4 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get labels of all children |
84
|
|
|
* @return array<string, string|null> |
85
|
|
|
*/ |
86
|
2 |
|
public function getLabels(): array |
87
|
|
|
{ |
88
|
2 |
|
$array = []; |
89
|
2 |
|
foreach ($this->controls as &$control) { |
90
|
|
|
/** @var AControl $control */ |
91
|
2 |
|
if ($control instanceof Interfaces\IContainsControls) { |
92
|
1 |
|
$array += $control->/** @scrutinizer ignore-call */getLabels(); |
93
|
|
|
} else { |
94
|
2 |
|
$array[$control->getKey()] = $control->getLabel(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
2 |
|
return $array; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set labels to all children |
102
|
|
|
* @param array<string, string|null> $array |
103
|
|
|
*/ |
104
|
2 |
|
public function setLabels(array $array = []): void |
105
|
|
|
{ |
106
|
2 |
|
foreach ($this->controls as &$control) { |
107
|
|
|
/** @var AControl $control */ |
108
|
2 |
|
if ($control instanceof Interfaces\IContainsControls) { |
109
|
1 |
|
$control->/** @scrutinizer ignore-call */setLabels($array); |
110
|
2 |
|
} elseif (isset($array[$control->getKey()])) { |
111
|
2 |
|
$control->setLabel($array[$control->getKey()]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array<string, array<int, RuleException>> $passedErrors |
118
|
|
|
* @param array<string|IHtmlElement> $wrappersError |
119
|
|
|
* @throws RenderException |
120
|
|
|
* @return array<string, string> |
121
|
|
|
*/ |
122
|
3 |
|
public function getErrors(array $passedErrors, array $wrappersError): array |
123
|
|
|
{ |
124
|
3 |
|
$returnErrors = []; |
125
|
3 |
|
foreach ($this->controls as &$child) { |
126
|
3 |
|
if ($child instanceof Interfaces\IContainsControls) { |
127
|
1 |
|
$returnErrors += $child->/** @scrutinizer ignore-call */getErrors($passedErrors, $wrappersError); |
128
|
3 |
|
} elseif ($child instanceof AControl) { |
129
|
3 |
|
if (isset($passedErrors[$child->getKey()])) { |
130
|
2 |
|
if (!$child->wrappersErrors()) { |
131
|
2 |
|
$child->addWrapperErrors($wrappersError); |
132
|
|
|
} |
133
|
2 |
|
$returnErrors[$child->getKey()] = $child->renderErrors($passedErrors[$child->getKey()]); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
3 |
|
return $returnErrors; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
public function count(): int |
142
|
|
|
{ |
143
|
1 |
|
return count($this->controls); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|