1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Controllers; |
4
|
|
|
|
5
|
|
|
use Respect\Validation\Validatable; |
6
|
|
|
use WebTheory\Saveyour\Contracts\DataFormatterInterface; |
7
|
|
|
use WebTheory\Saveyour\Contracts\FieldDataManagerInterface; |
8
|
|
|
use WebTheory\Saveyour\Contracts\FormFieldControllerInterface; |
9
|
|
|
use WebTheory\Saveyour\Contracts\FormFieldInterface; |
10
|
|
|
|
11
|
|
|
class FormFieldControllerBuilder |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $requestVar; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* formField |
20
|
|
|
* |
21
|
|
|
* @var FormFieldInterface |
22
|
|
|
*/ |
23
|
|
|
protected $formField; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* dataManager |
27
|
|
|
* |
28
|
|
|
* @var FieldDataManagerInterface |
29
|
|
|
*/ |
30
|
|
|
protected $dataManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var DataFormatterInterface |
34
|
|
|
*/ |
35
|
|
|
protected $formatter; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Callback to escape value on display |
39
|
|
|
* |
40
|
|
|
* @var callable |
41
|
|
|
*/ |
42
|
|
|
protected $escaper = 'htmlspecialchars'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected $processingDisabled = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Validation rules |
51
|
|
|
* |
52
|
|
|
* @var Validatable[] |
53
|
|
|
*/ |
54
|
|
|
protected $rules = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Callback function(s) to sanitize incoming data |
58
|
|
|
* |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected $filters = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Alerts to display upon validation failure |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $alerts = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string[] |
72
|
|
|
*/ |
73
|
|
|
protected $mustAwait = []; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* |
77
|
|
|
*/ |
78
|
3 |
|
public function __construct(?string $requestVar) |
79
|
|
|
{ |
80
|
3 |
|
$requestVar && $this->requestVar = $requestVar; |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* |
85
|
|
|
*/ |
86
|
3 |
|
public function create(): FormFieldControllerInterface |
87
|
|
|
{ |
88
|
3 |
|
return new FormFieldController( |
89
|
3 |
|
$this->requestVar, |
90
|
3 |
|
$this->formField, |
91
|
3 |
|
$this->dataManager, |
92
|
3 |
|
$this->formatter, |
93
|
3 |
|
$this->filters, |
94
|
3 |
|
$this->rules, |
95
|
3 |
|
$this->escaper, |
96
|
3 |
|
$this->processingDisabled, |
97
|
3 |
|
$this->mustAwait |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the value of requestVar |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getRequestVar(): string |
107
|
|
|
{ |
108
|
|
|
return $this->requestVar; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the value of requestVar |
113
|
|
|
* |
114
|
|
|
* @param string $requestVar |
115
|
|
|
* |
116
|
|
|
* @return self |
117
|
|
|
*/ |
118
|
|
|
public function setRequestVar(string $requestVar) |
119
|
|
|
{ |
120
|
|
|
$this->requestVar = $requestVar; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get formField |
127
|
|
|
* |
128
|
|
|
* @return FormFieldInterface |
129
|
|
|
*/ |
130
|
|
|
public function getFormField(): FormFieldInterface |
131
|
|
|
{ |
132
|
|
|
return $this->formField; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set formField |
137
|
|
|
* |
138
|
|
|
* @param FormFieldInterface $formField formField |
139
|
|
|
* |
140
|
|
|
* @return self |
141
|
|
|
*/ |
142
|
|
|
public function setFormField(FormFieldInterface $formField) |
143
|
|
|
{ |
144
|
|
|
$this->formField = $formField; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get dataManager |
151
|
|
|
* |
152
|
|
|
* @return FieldDataManagerInterface |
153
|
|
|
*/ |
154
|
|
|
public function getDataManager(): FieldDataManagerInterface |
155
|
|
|
{ |
156
|
|
|
return $this->dataManager; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set dataManager |
161
|
|
|
* |
162
|
|
|
* @param FieldDataManagerInterface $dataManager dataManager |
163
|
|
|
* |
164
|
|
|
* @return self |
165
|
|
|
*/ |
166
|
|
|
public function setDataManager(FieldDataManagerInterface $dataManager) |
167
|
|
|
{ |
168
|
|
|
$this->dataManager = $dataManager; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the value of formatter |
175
|
|
|
* |
176
|
|
|
* @return DataFormatterInterface |
177
|
|
|
*/ |
178
|
|
|
public function getFormatter(): DataFormatterInterface |
179
|
|
|
{ |
180
|
|
|
return $this->formatter; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set the value of formatter |
185
|
|
|
* |
186
|
|
|
* @param DataFormatterInterface $formatter |
187
|
|
|
* |
188
|
|
|
* @return self |
189
|
|
|
*/ |
190
|
|
|
public function setFormatter(DataFormatterInterface $formatter) |
191
|
|
|
{ |
192
|
|
|
$this->formatter = $formatter; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get callback to escape value on display |
199
|
|
|
* |
200
|
|
|
* @return callable|null |
201
|
|
|
*/ |
202
|
|
|
public function getEscaper(): callable |
203
|
|
|
{ |
204
|
|
|
return $this->escaper; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Set callback to escape value on display |
209
|
|
|
* |
210
|
|
|
* @param callable|null $escaper Callback to escape value on display |
211
|
|
|
* |
212
|
|
|
* @return self |
213
|
|
|
*/ |
214
|
|
|
public function setEscaper(?callable $escaper) |
215
|
|
|
{ |
216
|
|
|
$this->escaper = $escaper ?? FormFieldController::LAZY_ESCAPE; |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get the value of processingDisabled |
223
|
|
|
* |
224
|
|
|
* @return bool |
225
|
|
|
*/ |
226
|
|
|
public function getProcessingDisabled(): bool |
227
|
|
|
{ |
228
|
|
|
return $this->processingDisabled; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Set the value of processingDisabled |
233
|
|
|
* |
234
|
|
|
* @param bool $processingDisabled |
235
|
|
|
* |
236
|
|
|
* @return self |
237
|
|
|
*/ |
238
|
|
|
public function setProcessingDisabled(bool $processingDisabled) |
239
|
|
|
{ |
240
|
|
|
$this->processingDisabled = $processingDisabled; |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get callback function(s) to sanitize incoming data before saving to database |
247
|
|
|
* |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
public function getFilters(): array |
251
|
|
|
{ |
252
|
|
|
return $this->filters; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Set callback function(s) to sanitize incoming data before saving to database |
257
|
|
|
* |
258
|
|
|
* @param array $filters Callback function(s) to sanitize incoming data before saving to database |
259
|
|
|
* |
260
|
|
|
* @return self |
261
|
|
|
*/ |
262
|
|
|
public function setFilters(callable ...$filters) |
263
|
|
|
{ |
264
|
|
|
$this->filters = $filters; |
265
|
|
|
|
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Set callback function(s) to sanitize incoming data before saving to database |
271
|
|
|
* |
272
|
|
|
* @param callable $filters Callback function(s) to sanitize incoming data before saving to database |
273
|
|
|
* |
274
|
|
|
* @return self |
275
|
|
|
*/ |
276
|
|
|
public function addFilter(callable $filter) |
277
|
|
|
{ |
278
|
|
|
$this->filters[] = $filter; |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Get validation |
285
|
|
|
* |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
|
|
public function getRules(): array |
289
|
|
|
{ |
290
|
|
|
return $this->rules; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* |
295
|
|
|
*/ |
296
|
|
|
public function getRule(string $rule): Validatable |
297
|
|
|
{ |
298
|
|
|
return $this->rules[$rule]; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Add validation rules |
303
|
|
|
* |
304
|
|
|
* @param array $rules Array of Validatable instances |
305
|
|
|
* |
306
|
|
|
* @return self |
307
|
|
|
*/ |
308
|
|
|
public function setRules(array $rules) |
309
|
|
|
{ |
310
|
|
|
$this->rules = []; |
311
|
|
|
|
312
|
|
|
foreach ($rules as $rule => $validator) { |
313
|
|
|
|
314
|
|
|
if (is_array($validator)) { |
315
|
|
|
$alert = $validator['alert'] ?? null; |
316
|
|
|
$validator = $validator['validator']; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
$this->addRule($rule, $validator, $alert ?? null); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return $this; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Add validation rule |
327
|
|
|
* |
328
|
|
|
* @param string $rule Name of the the rule being checked |
329
|
|
|
* @param Validatable $validator Validatable instance |
330
|
|
|
* @param string $alert Message to be displayed if validation fails |
331
|
|
|
* |
332
|
|
|
* @return self |
333
|
|
|
*/ |
334
|
|
|
public function addRule(string $rule, Validatable $validator, ?string $alert = null) |
335
|
|
|
{ |
336
|
|
|
$this->rules[$rule] = $validator; |
337
|
|
|
|
338
|
|
|
if ($alert) { |
339
|
|
|
$this->addAlert($rule, $alert); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Get validation_messages |
347
|
|
|
* |
348
|
|
|
* @return string |
349
|
|
|
*/ |
350
|
|
|
public function getAlerts(): array |
351
|
|
|
{ |
352
|
|
|
return $this->alerts; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* |
357
|
|
|
*/ |
358
|
|
|
public function getAlert(string $alert) |
359
|
|
|
{ |
360
|
|
|
return $this->alerts[$alert]; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Set validation messages |
365
|
|
|
* |
366
|
|
|
* @param string $alerts validation_messages |
367
|
|
|
* |
368
|
|
|
* @return self |
369
|
|
|
*/ |
370
|
|
|
public function setAlerts(array $alerts) |
371
|
|
|
{ |
372
|
|
|
foreach ($alerts as $rule => $alert) { |
373
|
|
|
$this->addAlert($rule, $alert); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Set validation_messages |
381
|
|
|
* |
382
|
|
|
* @param string $alerts validation_messages |
383
|
|
|
* |
384
|
|
|
* @return self |
385
|
|
|
*/ |
386
|
|
|
public function addAlert(string $rule, string $alert) |
387
|
|
|
{ |
388
|
|
|
$this->alerts[$rule] = $alert; |
389
|
|
|
|
390
|
|
|
return $this; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Get the value of mustAwait |
395
|
|
|
* |
396
|
|
|
* @return string[] |
397
|
|
|
*/ |
398
|
|
|
public function getMustAwait(): array |
399
|
|
|
{ |
400
|
|
|
return $this->mustAwait; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Set the value of mustAwait |
405
|
|
|
* |
406
|
|
|
* @param string[] $mustAwait |
407
|
|
|
* |
408
|
|
|
* @return self |
409
|
|
|
*/ |
410
|
|
|
public function setMustAwait(string ...$fields) |
411
|
|
|
{ |
412
|
|
|
$this->mustAwait = $fields; |
413
|
|
|
|
414
|
|
|
return $this; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* |
419
|
|
|
*/ |
420
|
3 |
|
public function await(string $field) |
421
|
|
|
{ |
422
|
3 |
|
$this->mustAwait[] = $field; |
423
|
|
|
|
424
|
3 |
|
return $this; |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|