1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArgentCrusade\Forms; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
class FormField |
8
|
|
|
{ |
9
|
|
|
protected $label = ''; |
10
|
|
|
protected $type = ''; |
11
|
|
|
protected $value; |
12
|
|
|
protected $attributes = []; |
13
|
|
|
protected $classes = []; |
14
|
|
|
protected $parameters = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* FormField constructor. |
18
|
|
|
* |
19
|
|
|
* @param string $label |
20
|
|
|
*/ |
21
|
|
|
public function __construct(string $label) |
22
|
|
|
{ |
23
|
|
|
$this->withLabel($label) |
24
|
|
|
->withClasses($this->classes); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Determines whether the field is using its own markup. |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function usingOwnMarkup() |
33
|
|
|
{ |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the field's view name. |
39
|
|
|
* |
40
|
|
|
* @param string $prefix |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function viewName(string $prefix = '') |
45
|
|
|
{ |
46
|
|
|
return ($prefix ? $prefix.'.' : '').$this->type; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set field label. |
51
|
|
|
* |
52
|
|
|
* @param string $label |
53
|
|
|
* |
54
|
|
|
* @return FormField |
55
|
|
|
*/ |
56
|
|
|
public function withLabel(string $label) |
57
|
|
|
{ |
58
|
|
|
$this->label = $label; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set field type. |
65
|
|
|
* |
66
|
|
|
* @param string $type |
67
|
|
|
* |
68
|
|
|
* @return FormField |
69
|
|
|
*/ |
70
|
|
|
public function withType(string $type) |
71
|
|
|
{ |
72
|
|
|
$this->type = $type; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set field value |
79
|
|
|
* |
80
|
|
|
* @param mixed $value |
81
|
|
|
* |
82
|
|
|
* @return FormField |
83
|
|
|
*/ |
84
|
|
|
public function withValue($value) |
85
|
|
|
{ |
86
|
|
|
$this->value = $value; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set field hint. |
93
|
|
|
* |
94
|
|
|
* @param string $hint |
95
|
|
|
* |
96
|
|
|
* @return FormField |
97
|
|
|
*/ |
98
|
|
|
public function withHint(string $hint) |
99
|
|
|
{ |
100
|
|
|
return $this->withParameter('hint', $hint); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Add attributes to the field. |
105
|
|
|
* |
106
|
|
|
* @param array $attributes |
107
|
|
|
* @param bool $replace |
108
|
|
|
* |
109
|
|
|
* @return FormField |
110
|
|
|
*/ |
111
|
|
|
public function withAttributes(array $attributes, bool $replace = false) |
112
|
|
|
{ |
113
|
|
|
return $this->mergeOrReplace('attributes', $attributes, $replace); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set field attribute value. |
118
|
|
|
* |
119
|
|
|
* @param string $attribute |
120
|
|
|
* @param $value |
121
|
|
|
* |
122
|
|
|
* @return FormField |
123
|
|
|
*/ |
124
|
|
|
public function withAttribute(string $attribute, $value) |
125
|
|
|
{ |
126
|
|
|
$this->attributes[$attribute] = $value; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Add parameters to the field. |
133
|
|
|
* |
134
|
|
|
* @param array $parameters |
135
|
|
|
* @param bool $replace |
136
|
|
|
* |
137
|
|
|
* @return FormField |
138
|
|
|
*/ |
139
|
|
|
public function withParameters(array $parameters, bool $replace = false) |
140
|
|
|
{ |
141
|
|
|
return $this->mergeOrReplace('parameters', $parameters, $replace); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set field parameter. |
146
|
|
|
* |
147
|
|
|
* @param string $parameter |
148
|
|
|
* @param mixed $value |
149
|
|
|
* |
150
|
|
|
* @return FormField |
151
|
|
|
*/ |
152
|
|
|
public function withParameter(string $parameter, $value) |
153
|
|
|
{ |
154
|
|
|
$this->parameters[$parameter] = $value; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Add CSS classes to the field. |
161
|
|
|
* |
162
|
|
|
* @param array|string $classes |
163
|
|
|
* @param bool $replace |
164
|
|
|
* |
165
|
|
|
* @return FormField|FormField |
166
|
|
|
*/ |
167
|
|
|
public function withClasses($classes, bool $replace = false) |
168
|
|
|
{ |
169
|
|
|
if (!$classes) { |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->classes = $this->normalizeClasses($this->classes); |
174
|
|
|
$classes = $this->normalizeClasses($classes); |
175
|
|
|
|
176
|
|
|
return $this->mergeOrReplace('classes', $classes, $replace); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Determines whether the field has given attribute. |
181
|
|
|
* |
182
|
|
|
* @param string $path |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function hasAttribute(string $path) |
187
|
|
|
{ |
188
|
|
|
return Arr::has($this->attributes, $path); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get field attribute value. |
193
|
|
|
* |
194
|
|
|
* @param string $path |
195
|
|
|
* @param mixed $default = null |
196
|
|
|
* |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
|
|
public function getAttribute(string $path, $default = null) |
200
|
|
|
{ |
201
|
|
|
return Arr::get($this->attributes, $path, $default); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get all field attributes. |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function attributes() |
210
|
|
|
{ |
211
|
|
|
return $this->attributes; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get string representation of attribute-value pairs. |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function joinAttributes() |
220
|
|
|
{ |
221
|
|
|
return collect($this->attributes()) |
222
|
|
|
->map(function (string $value, string $attribute) { |
223
|
|
|
return $attribute.'="'.$value.'"'; |
224
|
|
|
}) |
225
|
|
|
->implode(' '); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Determines whether the field has given parameter. |
230
|
|
|
* |
231
|
|
|
* @param string $path |
232
|
|
|
* |
233
|
|
|
* @return bool |
234
|
|
|
*/ |
235
|
|
|
public function hasParameter(string $path) |
236
|
|
|
{ |
237
|
|
|
return Arr::has($this->parameters, $path); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get parameter value. |
242
|
|
|
* |
243
|
|
|
* @param string $path |
244
|
|
|
* @param mixed $default = null |
245
|
|
|
* |
246
|
|
|
* @return mixed |
247
|
|
|
*/ |
248
|
|
|
public function getParameter(string $path, $default = null) |
249
|
|
|
{ |
250
|
|
|
return Arr::get($this->parameters, $path, $default); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Get all field parameters. |
255
|
|
|
* |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
public function parameters() |
259
|
|
|
{ |
260
|
|
|
return $this->parameters; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Get field's CSS classes list. |
265
|
|
|
* |
266
|
|
|
* @return array |
267
|
|
|
*/ |
268
|
|
|
public function classList() |
269
|
|
|
{ |
270
|
|
|
return $this->classes; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Get string representation of the field's CSS classes list. |
275
|
|
|
* |
276
|
|
|
* @param array|string $classes = null |
277
|
|
|
* |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
public function classes($classes = null) |
281
|
|
|
{ |
282
|
|
|
$mergeWith = []; |
283
|
|
|
|
284
|
|
|
if (!is_null($classes)) { |
285
|
|
|
$mergeWith = $this->normalizeClasses($classes); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return implode(' ', array_merge($this->classList(), $mergeWith)); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get field label. |
293
|
|
|
* |
294
|
|
|
* @return string |
295
|
|
|
*/ |
296
|
|
|
public function label() |
297
|
|
|
{ |
298
|
|
|
return $this->label; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Get field type. |
303
|
|
|
* |
304
|
|
|
* @return string |
305
|
|
|
*/ |
306
|
|
|
public function type() |
307
|
|
|
{ |
308
|
|
|
return $this->type; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Get field value. |
313
|
|
|
* |
314
|
|
|
* @return mixed |
315
|
|
|
*/ |
316
|
|
|
public function value() |
317
|
|
|
{ |
318
|
|
|
return $this->value; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Determines whether the field has hint. |
323
|
|
|
* |
324
|
|
|
* @return bool |
325
|
|
|
*/ |
326
|
|
|
public function hasHint() |
327
|
|
|
{ |
328
|
|
|
return $this->hasParameter('hint'); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Get field hint. |
333
|
|
|
* |
334
|
|
|
* @return mixed |
335
|
|
|
*/ |
336
|
|
|
public function hint() |
337
|
|
|
{ |
338
|
|
|
return $this->getParameter('hint'); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Normalize given CSS classes & transform them to array. |
343
|
|
|
* |
344
|
|
|
* @param string|array $classes |
345
|
|
|
* |
346
|
|
|
* @return array |
347
|
|
|
*/ |
348
|
|
|
protected function normalizeClasses($classes) |
349
|
|
|
{ |
350
|
|
|
if (!is_array($classes)) { |
351
|
|
|
$classes = explode(' ', $classes); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$classes = array_filter($classes, function ($value) { |
355
|
|
|
return is_string($value) && trim($value); |
356
|
|
|
}); |
357
|
|
|
|
358
|
|
|
if (!count($classes)) { |
359
|
|
|
return []; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return array_map(function (string $class) { |
363
|
|
|
return trim($class); |
364
|
|
|
}, $classes); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Set field class member's value. |
369
|
|
|
* |
370
|
|
|
* @param string $field |
371
|
|
|
* @param array $values |
372
|
|
|
* @param bool $replace |
373
|
|
|
* |
374
|
|
|
* @return FormField |
375
|
|
|
*/ |
376
|
|
|
protected function mergeOrReplace(string $field, array $values, bool $replace) |
377
|
|
|
{ |
378
|
|
|
if ($replace) { |
379
|
|
|
$this->{$field} = $values; |
380
|
|
|
} else { |
381
|
|
|
$this->{$field} = array_merge($this->{$field}, $values); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return $this; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|