1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_forms\Interfaces\IContainsControls; |
7
|
|
|
use kalanis\kw_rules\Validate; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AnyControl |
12
|
|
|
* @package kalanis\kw_forms\Controls |
13
|
|
|
* Form element for rendering other controls |
14
|
|
|
*/ |
15
|
|
|
class AnyControl extends AControl implements IContainsControls |
16
|
|
|
{ |
17
|
|
|
use TSubControls; |
18
|
|
|
use TSubErrors; |
19
|
|
|
|
20
|
|
|
protected bool $needAll = false; |
21
|
|
|
protected string $templateLabel = ''; |
22
|
|
|
|
23
|
1 |
|
public function removeControl(string $key): void |
24
|
|
|
{ |
25
|
1 |
|
if (isset($this->controls[$key])) { |
26
|
1 |
|
unset($this->controls[$key]); |
27
|
|
|
} |
28
|
1 |
|
} |
29
|
|
|
|
30
|
2 |
|
public function hasControl(string $key): bool |
31
|
|
|
{ |
32
|
2 |
|
return !empty($this->getControl($key)); |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
public function needAll(bool $yes = false): void |
36
|
|
|
{ |
37
|
3 |
|
$this->needAll = $yes; |
38
|
3 |
|
} |
39
|
|
|
|
40
|
4 |
|
public function validateControls(Validate $validate): bool |
41
|
|
|
{ |
42
|
4 |
|
$this->errors = []; |
43
|
4 |
|
$validation = true; |
44
|
4 |
|
foreach ($this->controls as &$child) { |
45
|
3 |
|
if ($child instanceof IContainsControls) { |
46
|
2 |
|
$result = $child->/** @scrutinizer ignore-call */validateControls($validate); |
47
|
2 |
|
if ($result && !$this->needAll) { |
48
|
1 |
|
$this->errors = []; |
49
|
1 |
|
return true; |
50
|
|
|
} |
51
|
2 |
|
$validation &= $result; |
52
|
2 |
|
$this->errors += $child->/** @scrutinizer ignore-call */getValidatedErrors(); |
53
|
3 |
|
} elseif ($child instanceof AControl) { |
54
|
3 |
|
$result = $validate->validate($child); |
55
|
3 |
|
if ($result && !$this->needAll) { |
56
|
2 |
|
$this->errors = []; |
57
|
2 |
|
return true; |
58
|
|
|
} |
59
|
3 |
|
$validation &= $result; |
60
|
3 |
|
$this->errors += $validate->getErrors(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
4 |
|
return boolval($validation); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function render(): string |
67
|
|
|
{ |
68
|
1 |
|
return ''; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function renderLabel($attributes = []): string |
72
|
|
|
{ |
73
|
1 |
|
return ''; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function renderInput($attributes = null): string |
77
|
|
|
{ |
78
|
1 |
|
return ''; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function renderErrors(array $errors): string |
82
|
|
|
{ |
83
|
1 |
|
return ''; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function setLabel(/** @scrutinizer ignore-unused */?string $value): void |
87
|
|
|
{ |
88
|
1 |
|
} |
89
|
|
|
|
90
|
1 |
|
public function getLabel(): ?string |
91
|
|
|
{ |
92
|
1 |
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function setValue(/** @scrutinizer ignore-unused */$value): void |
96
|
|
|
{ |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
public function getValue() |
100
|
|
|
{ |
101
|
1 |
|
return null; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|