|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2020 Enjoys. |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace Enjoys\Forms\Traits; |
|
28
|
|
|
|
|
29
|
|
|
use Enjoys\Forms\Element; |
|
30
|
|
|
use Enjoys\Forms\Elements\Button; |
|
31
|
|
|
use Enjoys\Forms\Elements\Captcha; |
|
32
|
|
|
use Enjoys\Forms\Elements\Checkbox; |
|
33
|
|
|
use Enjoys\Forms\Elements\Color; |
|
34
|
|
|
use Enjoys\Forms\Elements\Csrf; |
|
35
|
|
|
use Enjoys\Forms\Elements\Datalist; |
|
36
|
|
|
use Enjoys\Forms\Elements\Date; |
|
37
|
|
|
use Enjoys\Forms\Elements\Datetime; |
|
38
|
|
|
use Enjoys\Forms\Elements\Datetimelocal; |
|
39
|
|
|
use Enjoys\Forms\Elements\Email; |
|
40
|
|
|
use Enjoys\Forms\Elements\File; |
|
41
|
|
|
use Enjoys\Forms\Elements\Group; |
|
42
|
|
|
use Enjoys\Forms\Elements\Header; |
|
43
|
|
|
use Enjoys\Forms\Elements\Hidden; |
|
44
|
|
|
use Enjoys\Forms\Elements\Html; |
|
45
|
|
|
use Enjoys\Forms\Elements\Image; |
|
46
|
|
|
use Enjoys\Forms\Elements\Month; |
|
47
|
|
|
use Enjoys\Forms\Elements\Number; |
|
48
|
|
|
use Enjoys\Forms\Elements\Password; |
|
49
|
|
|
use Enjoys\Forms\Elements\Radio; |
|
50
|
|
|
use Enjoys\Forms\Elements\Range; |
|
51
|
|
|
use Enjoys\Forms\Elements\Reset; |
|
52
|
|
|
use Enjoys\Forms\Elements\Search; |
|
53
|
|
|
use Enjoys\Forms\Elements\Select; |
|
54
|
|
|
use Enjoys\Forms\Elements\Submit; |
|
55
|
|
|
use Enjoys\Forms\Elements\Tel; |
|
56
|
|
|
use Enjoys\Forms\Elements\Text; |
|
57
|
|
|
use Enjoys\Forms\Elements\Textarea; |
|
58
|
|
|
use Enjoys\Forms\Elements\Time; |
|
59
|
|
|
use Enjoys\Forms\Elements\Url; |
|
60
|
|
|
use Enjoys\Forms\Elements\Week; |
|
61
|
|
|
use Enjoys\Forms\Interfaces\CaptchaInterface; |
|
62
|
|
|
use Enjoys\Forms\Interfaces\ElementInterface; |
|
63
|
|
|
use Webmozart\Assert\Assert; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @method Text text(string $name, string $label = null) |
|
67
|
|
|
* @method Hidden hidden(string $name, string $value = null) |
|
68
|
|
|
* @method Password password(string $name, string $label = null) |
|
69
|
|
|
* @method Submit submit(string $name, string $title = null) |
|
70
|
|
|
* @method Header header(string $title = null) |
|
71
|
|
|
* @method Color color(string $name, string $label = null) |
|
72
|
|
|
* @method Date date(string $name, string $label = null) |
|
73
|
|
|
* @method Datetime datetime(string $name, string $label = null) |
|
74
|
|
|
* @method Datetimelocal datetimelocal(string $name, string $label = null) |
|
75
|
|
|
* @method Email email(string $name, string $label = null) |
|
76
|
|
|
* @method Number number(string $name, string $label = null) |
|
77
|
|
|
* @method Range range(string $name, string $label = null) |
|
78
|
|
|
* @method Search search(string $name, string $label = null) |
|
79
|
|
|
* @method Tel tel(string $name, string $label = null) |
|
80
|
|
|
* @method Time time(string $name, string $label = null) |
|
81
|
|
|
* @method Url url(string $name, string $label = null) |
|
82
|
|
|
* @method Month month(string $name, string $label = null) |
|
83
|
|
|
* @method Week week(string $name, string $label = null) |
|
84
|
|
|
* @method Textarea textarea(string $name, string $label = null) |
|
85
|
|
|
* @method Select select(string $name, string $label = null) |
|
86
|
|
|
* @method Button button(string $name, string $title = null) |
|
87
|
|
|
* @method Datalist datalist(string $name, string $label = null) |
|
88
|
|
|
* @method Checkbox checkbox(string $name, string $label = null) |
|
89
|
|
|
* @method Image image(string $name, string $src = null) |
|
90
|
|
|
* @method Radio radio(string $name, string $title = null) |
|
91
|
|
|
* @method Reset reset(string $name, string $title = null) |
|
92
|
|
|
* @method Captcha captcha(CaptchaInterface $captcha) |
|
93
|
|
|
* @method Group group(string $title = null) |
|
94
|
|
|
* @method File file(string $name, string $label = null) |
|
95
|
|
|
* @method Csrf csrf() |
|
96
|
|
|
* @method Html html(string $html) |
|
97
|
|
|
* |
|
98
|
|
|
* @author Enjoys |
|
99
|
|
|
*/ |
|
100
|
|
|
trait Container |
|
101
|
|
|
{ |
|
102
|
|
|
/** |
|
103
|
|
|
* @var Element[] |
|
104
|
|
|
*/ |
|
105
|
|
|
private array $elements = []; |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
57 |
|
public function __call(string $name, array $arguments): Element |
|
109
|
|
|
{ |
|
110
|
57 |
|
$className = '\Enjoys\\Forms\\Elements\\' . ucfirst($name); |
|
111
|
57 |
|
Assert::classExists($className); |
|
112
|
|
|
/** @var class-string<ElementInterface> $className */ |
|
113
|
55 |
|
$element = new $className(...$arguments); |
|
114
|
|
|
|
|
115
|
|
|
/** @var Element $element */ |
|
116
|
55 |
|
$this->addElement($element); |
|
117
|
55 |
|
return $element; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* |
|
122
|
|
|
* @param Element $element |
|
123
|
|
|
* @return $this |
|
124
|
|
|
* @noinspection PhpMissingReturnTypeInspection |
|
125
|
|
|
*/ |
|
126
|
89 |
|
public function addElement(Element $element) |
|
127
|
|
|
{ |
|
128
|
89 |
|
$element->setRequest($this->getRequest()); |
|
|
|
|
|
|
129
|
89 |
|
if ($element->prepare() === true) { |
|
|
|
|
|
|
130
|
14 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
89 |
|
if ($element->isAllowSameNameElements() === false |
|
135
|
89 |
|
&& false !== $keyElement = $this->getElementKeyByName($element->getName()) |
|
136
|
|
|
) { |
|
137
|
86 |
|
$this->elements[$keyElement] = $element; |
|
138
|
86 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
89 |
|
$this->elements[] = $element; |
|
143
|
89 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return Element[] |
|
149
|
|
|
*/ |
|
150
|
13 |
|
public function getElements(): array |
|
151
|
|
|
{ |
|
152
|
13 |
|
return $this->elements; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $name |
|
158
|
|
|
* @return Element|null |
|
159
|
|
|
*/ |
|
160
|
17 |
|
public function getElement(string $name): ?Element |
|
161
|
|
|
{ |
|
162
|
17 |
|
if (false !== $key = $this->getElementKeyByName($name)) { |
|
|
|
|
|
|
163
|
17 |
|
return $this->elements[$key]; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
3 |
|
return null; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* |
|
171
|
|
|
* @param Element|null $element |
|
172
|
|
|
* @return $this |
|
173
|
|
|
*/ |
|
174
|
19 |
|
public function removeElement(?Element $element = null): self |
|
175
|
|
|
{ |
|
176
|
19 |
|
if (null === $element) { |
|
177
|
1 |
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
19 |
|
$key = $this->getElementKeyByName($element->getName()); |
|
181
|
|
|
|
|
182
|
19 |
|
if ($key !== false) { |
|
|
|
|
|
|
183
|
10 |
|
unset($this->elements[$key]); |
|
184
|
|
|
} |
|
185
|
19 |
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
89 |
|
private function getElementKeyByName(string $name): int|false |
|
189
|
|
|
{ |
|
190
|
89 |
|
foreach ($this->elements as $key => $element) { |
|
191
|
89 |
|
if ($element->getName() === $name) { |
|
192
|
86 |
|
return $key; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
89 |
|
return false; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|