1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Enjoys\Forms; |
6
|
|
|
|
7
|
|
|
use Enjoys\Forms\Traits\Attributes; |
8
|
|
|
use Enjoys\Forms\Traits\Request; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Element |
12
|
|
|
* @package Enjoys\Forms |
13
|
|
|
*/ |
14
|
|
|
abstract class Element implements ElementInterface |
15
|
|
|
{ |
16
|
|
|
use Attributes; |
17
|
|
|
use Request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected string $name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected string $type = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected ?string $label = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Флаг для обозначения обязательности заполнения этого элемента или нет |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
protected bool $required = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* |
45
|
|
|
* @var Form|null |
46
|
|
|
*/ |
47
|
|
|
protected ?Form $form = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $name |
51
|
|
|
* @param string|null $label |
52
|
|
|
*/ |
53
|
223 |
|
public function __construct(string $name, string $label = null) |
54
|
|
|
{ |
55
|
223 |
|
$this->setName($name); |
56
|
|
|
|
57
|
223 |
|
if (!is_null($label)) { |
58
|
100 |
|
$this->setLabel($label); |
59
|
|
|
} |
60
|
223 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Form $form |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
80 |
|
public function setForm(Form $form): void |
68
|
|
|
{ |
69
|
80 |
|
$this->form = $form; |
70
|
80 |
|
$this->setDefault(); |
71
|
80 |
|
if($this instanceof FillableInterface){ |
72
|
14 |
|
foreach ($this->getElements() as $element) { |
73
|
1 |
|
$element->setDefault($this->getDefaultValue()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
80 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* @return Form |
81
|
|
|
*/ |
82
|
80 |
|
public function getForm(): Form |
83
|
|
|
{ |
84
|
80 |
|
return $this->form ?? new Form(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
80 |
|
public function unsetForm(): void |
92
|
|
|
{ |
93
|
80 |
|
$this->form = null; |
94
|
80 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
79 |
|
public function prepare() |
100
|
|
|
{ |
101
|
79 |
|
$this->unsetForm(); |
102
|
79 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
57 |
|
public function getType(): string |
109
|
|
|
{ |
110
|
57 |
|
return $this->type; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* |
115
|
|
|
* @param string $name |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
223 |
|
protected function setName(string $name): self |
119
|
|
|
{ |
120
|
223 |
|
$this->name = $name; |
121
|
223 |
|
$this->setAttributes( |
122
|
|
|
[ |
123
|
223 |
|
'id' => $this->name, |
124
|
223 |
|
'name' => $this->name |
125
|
|
|
] |
126
|
|
|
); |
127
|
|
|
|
128
|
223 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
167 |
|
public function getName(): string |
136
|
|
|
{ |
137
|
167 |
|
return $this->name; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* |
142
|
|
|
* @param string|null $title |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
100 |
|
public function setLabel(?string $title = null): self |
146
|
|
|
{ |
147
|
100 |
|
$this->label = $title; |
148
|
100 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* |
153
|
|
|
* @return string|null |
154
|
|
|
*/ |
155
|
28 |
|
public function getLabel(): ?string |
156
|
|
|
{ |
157
|
28 |
|
return $this->label; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
80 |
|
protected function setDefault(): self |
164
|
|
|
{ |
165
|
80 |
|
$value = $this->getForm() |
166
|
80 |
|
->getDefaultsHandler() |
167
|
80 |
|
->getValue($this->getName()); |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
|
171
|
80 |
|
if (is_array($value)) { |
172
|
1 |
|
$this->setAttributes( |
173
|
|
|
[ |
174
|
1 |
|
'value' => $value[0] |
175
|
|
|
] |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
80 |
|
if (is_string($value) || is_numeric($value)) { |
180
|
|
|
// $this->setValue($value); |
181
|
7 |
|
$this->setAttributes( |
182
|
|
|
[ |
183
|
7 |
|
'value' => $value |
184
|
|
|
] |
185
|
|
|
); |
186
|
|
|
} |
187
|
80 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
2 |
|
public function baseHtml(): string |
191
|
|
|
{ |
192
|
2 |
|
return "<input type=\"{$this->getType()}\"{$this->getAttributesString()}>"; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|