|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Enjoys\Forms\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Enjoys\Forms\Element; |
|
8
|
|
|
use Enjoys\Forms\Elements; |
|
9
|
|
|
use Enjoys\Forms\Interfaces\CaptchaInterface; |
|
10
|
|
|
use Enjoys\Forms\Interfaces\ElementInterface; |
|
11
|
|
|
use Webmozart\Assert\Assert; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @method Elements\Text text(string $name, string $label = null) |
|
15
|
|
|
* @method Elements\Hidden hidden(string $name, string $value = null) |
|
16
|
|
|
* @method Elements\Password password(string $name, string $label = null) |
|
17
|
|
|
* @method Elements\Submit submit(string $name = null, string $title = null) |
|
18
|
|
|
* @method Elements\Header header(string $title = null) |
|
19
|
|
|
* @method Elements\Color color(string $name, string $label = null) |
|
20
|
|
|
* @method Elements\Date date(string $name, string $label = null) |
|
21
|
|
|
* @method Elements\Datetime datetime(string $name, string $label = null) |
|
22
|
|
|
* @method Elements\Datetimelocal datetimelocal(string $name, string $label = null) |
|
23
|
|
|
* @method Elements\Email email(string $name, string $label = null) |
|
24
|
|
|
* @method Elements\Number number(string $name, string $label = null) |
|
25
|
|
|
* @method Elements\Range range(string $name, string $label = null) |
|
26
|
|
|
* @method Elements\Search search(string $name, string $label = null) |
|
27
|
|
|
* @method Elements\Tel tel(string $name, string $label = null) |
|
28
|
|
|
* @method Elements\Time time(string $name, string $label = null) |
|
29
|
|
|
* @method Elements\Url url(string $name, string $label = null) |
|
30
|
|
|
* @method Elements\Month month(string $name, string $label = null) |
|
31
|
|
|
* @method Elements\Week week(string $name, string $label = null) |
|
32
|
|
|
* @method Elements\Textarea textarea(string $name, string $label = null) |
|
33
|
|
|
* @method Elements\Select select(string $name, string $label = null) |
|
34
|
|
|
* @method Elements\Button button(string $name, string $title = null) |
|
35
|
|
|
* @method Elements\Datalist datalist(string $name, string $label = null) |
|
36
|
|
|
* @method Elements\Checkbox checkbox(string $name, string $label = null) |
|
37
|
|
|
* @method Elements\Image image(string $name, string $src = null) |
|
38
|
|
|
* @method Elements\Radio radio(string $name, string $title = null) |
|
39
|
|
|
* @method Elements\Reset reset(string $name, string $title = null) |
|
40
|
|
|
* @method Elements\Captcha captcha(CaptchaInterface $captcha) |
|
41
|
|
|
* @method Elements\Group group(string $title = null) |
|
42
|
|
|
* @method Elements\File file(string $name, string $label = null) |
|
43
|
|
|
* @method Elements\Csrf csrf() |
|
44
|
|
|
* @method Elements\Html html(string $html) |
|
45
|
|
|
* |
|
46
|
|
|
* @author Enjoys |
|
47
|
|
|
*/ |
|
48
|
|
|
trait Container |
|
49
|
|
|
{ |
|
50
|
|
|
/** |
|
51
|
|
|
* @var Element[] |
|
52
|
|
|
*/ |
|
53
|
|
|
private array $elements = []; |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
58 |
|
public function __call(string $name, array $arguments): Element |
|
57
|
|
|
{ |
|
58
|
58 |
|
$className = '\Enjoys\\Forms\\Elements\\' . ucfirst($name); |
|
59
|
58 |
|
Assert::classExists($className); |
|
60
|
|
|
/** @var class-string<ElementInterface> $className */ |
|
61
|
56 |
|
$element = new $className(...$arguments); |
|
62
|
|
|
|
|
63
|
|
|
/** @var Element $element */ |
|
64
|
56 |
|
$this->addElement($element); |
|
65
|
56 |
|
return $element; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param Element $element |
|
70
|
|
|
* @return $this |
|
71
|
|
|
* @noinspection PhpMissingReturnTypeInspection |
|
72
|
|
|
*/ |
|
73
|
90 |
|
public function addElement(Element $element) |
|
74
|
|
|
{ |
|
75
|
90 |
|
$element->setRequest($this->getRequest()); |
|
|
|
|
|
|
76
|
90 |
|
if ($element->prepare() === true) { |
|
|
|
|
|
|
77
|
14 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
90 |
|
if ($element->isAllowSameNames() === false |
|
82
|
90 |
|
&& false !== $key = $this->getElementKey($element) |
|
83
|
|
|
) { |
|
84
|
87 |
|
$this->elements[$key] = $element; |
|
85
|
87 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
90 |
|
$this->elements[] = $element; |
|
90
|
90 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return Element[] |
|
96
|
|
|
*/ |
|
97
|
15 |
|
public function getElements(): array |
|
98
|
|
|
{ |
|
99
|
15 |
|
return $this->elements; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
19 |
|
public function getElement(string $name): ?Element |
|
103
|
|
|
{ |
|
104
|
19 |
|
if (false !== $key = $this->getElementKey($name)) { |
|
105
|
19 |
|
return $this->elements[$key]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
3 |
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param Element|null $element |
|
113
|
|
|
* @return $this |
|
114
|
|
|
* @noinspection PhpMissingReturnTypeInspection |
|
115
|
|
|
*/ |
|
116
|
21 |
|
public function removeElement(?Element $element = null) |
|
117
|
|
|
{ |
|
118
|
21 |
|
if (null === $element) { |
|
119
|
1 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
21 |
|
if (false !== $key = $this->getElementKey($element)) { |
|
123
|
12 |
|
unset($this->elements[$key]); |
|
124
|
|
|
} |
|
125
|
21 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
90 |
|
private function getElementKey(string|Element $element): int|false |
|
129
|
|
|
{ |
|
130
|
90 |
|
$name = ($element instanceof Element) ? $element->getName() : $element; |
|
131
|
|
|
|
|
132
|
90 |
|
foreach ($this->elements as $key => $el) { |
|
133
|
90 |
|
if ($el->getName() === $name) { |
|
134
|
|
|
/** @var int $key */ |
|
135
|
87 |
|
return $key; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
90 |
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|