1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Controllers; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
use WebTheory\Saveyour\Contracts\DataTransformerInterface; |
7
|
|
|
use WebTheory\Saveyour\Contracts\FieldDataManagerInterface; |
8
|
|
|
use WebTheory\Saveyour\Contracts\FieldOperationCacheInterface; |
9
|
|
|
use WebTheory\Saveyour\Contracts\FormFieldControllerInterface; |
10
|
|
|
use WebTheory\Saveyour\Contracts\FormFieldInterface; |
11
|
|
|
use WebTheory\Saveyour\InputPurifier; |
12
|
|
|
use WebTheory\Saveyour\Request; |
13
|
|
|
use WebTheory\Saveyour\Transformers\LazyTransformer; |
14
|
|
|
|
15
|
|
|
class BaseFormFieldController extends InputPurifier implements FormFieldControllerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $requestVar; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* formField |
24
|
|
|
* |
25
|
|
|
* @var FormFieldInterface |
26
|
|
|
*/ |
27
|
|
|
protected $formField; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* dataManager |
31
|
|
|
* |
32
|
|
|
* @var FieldDataManagerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $dataManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var DataTransformerInterface |
38
|
|
|
*/ |
39
|
|
|
protected $transformer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Callback to escape value on display |
43
|
|
|
* |
44
|
|
|
* @var callable|null |
45
|
|
|
*/ |
46
|
|
|
protected $escaper = 'htmlspecialchars'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $processingDisabled = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string[] |
55
|
|
|
*/ |
56
|
|
|
protected $mustAwait = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
*/ |
61
|
|
|
public const LAZY_ESCAPE = [self::class, 'lazyEscaper']; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
72 |
|
public function __construct( |
67
|
|
|
string $requestVar, |
68
|
|
|
?FormFieldInterface $formField = null, |
69
|
|
|
?FieldDataManagerInterface $dataManager = null, |
70
|
|
|
?DataTransformerInterface $transformer = null, |
71
|
|
|
?array $filters = null, |
72
|
|
|
?array $rules = null, |
73
|
|
|
?callable $escaper = null, |
74
|
|
|
?bool $processingDisabled = null, |
75
|
|
|
?array $await = null |
76
|
|
|
) { |
77
|
72 |
|
$this->requestVar = $requestVar; |
78
|
|
|
|
79
|
72 |
|
$formField && $this->formField = $formField; |
80
|
72 |
|
$dataManager && $this->dataManager = $dataManager; |
81
|
72 |
|
$processingDisabled && $this->processingDisabled = $processingDisabled; |
82
|
72 |
|
$escaper && $this->escaper = $escaper; |
83
|
|
|
|
84
|
72 |
|
$this->transformer = $transformer ?? new LazyTransformer(); |
85
|
|
|
|
86
|
72 |
|
$filters && $this->setFilters(...$filters); |
87
|
72 |
|
$rules && $this->setRules($rules); |
88
|
72 |
|
$await && $this->setMustAwait(...$await); |
89
|
72 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the value of requestVar |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
21 |
|
public function getRequestVar(): string |
97
|
|
|
{ |
98
|
21 |
|
return $this->requestVar; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get formField |
103
|
|
|
* |
104
|
|
|
* @return FormFieldInterface|null |
105
|
|
|
*/ |
106
|
12 |
|
public function getFormField(): ?FormFieldInterface |
107
|
|
|
{ |
108
|
12 |
|
return $this->formField; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get dataManager |
113
|
|
|
* |
114
|
|
|
* @return FieldDataManagerInterface|null |
115
|
|
|
*/ |
116
|
6 |
|
public function getDataManager(): ?FieldDataManagerInterface |
117
|
|
|
{ |
118
|
6 |
|
return $this->dataManager; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the value of transformer |
123
|
|
|
* |
124
|
|
|
* @return DataTransformerInterface|null |
125
|
|
|
*/ |
126
|
3 |
|
public function getTransformer(): ?DataTransformerInterface |
127
|
|
|
{ |
128
|
3 |
|
return $this->transformer; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get callback to escape value on display |
133
|
|
|
* |
134
|
|
|
* @return callable|null |
135
|
|
|
*/ |
136
|
6 |
|
public function getEscaper(): ?callable |
137
|
|
|
{ |
138
|
6 |
|
return $this->escaper; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get hasDataManager |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
24 |
|
public function hasDataManager(): bool |
147
|
|
|
{ |
148
|
24 |
|
return isset($this->dataManager); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the value of mustAwait |
153
|
|
|
* |
154
|
|
|
* @return string[] |
155
|
|
|
*/ |
156
|
6 |
|
public function mustAwait(): array |
157
|
|
|
{ |
158
|
6 |
|
return $this->mustAwait; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set the value of mustAwait |
163
|
|
|
* |
164
|
|
|
* @param string[] $mustAwait |
165
|
|
|
* |
166
|
|
|
* @return self |
167
|
|
|
*/ |
168
|
3 |
|
protected function setMustAwait(string ...$fields) |
169
|
|
|
{ |
170
|
3 |
|
$this->mustAwait = $fields; |
171
|
|
|
|
172
|
3 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the value of savingDisabled |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
9 |
|
public function isProcessingDisabled(): bool |
181
|
|
|
{ |
182
|
9 |
|
return $this->processingDisabled; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritDoc} |
187
|
|
|
*/ |
188
|
15 |
|
public function process(ServerRequestInterface $request): FieldOperationCacheInterface |
189
|
|
|
{ |
190
|
15 |
|
$results = new FieldOperationCacheBuilder(); |
191
|
15 |
|
$this->processData($request, $results); |
192
|
15 |
|
$results->withRuleViolations($this->getViolations()); |
193
|
15 |
|
$this->clearViolations(); |
194
|
|
|
|
195
|
15 |
|
return $results; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* |
200
|
|
|
*/ |
201
|
15 |
|
protected function processData(ServerRequestInterface $request, FieldOperationCacheBuilder $results) |
202
|
|
|
{ |
203
|
15 |
|
$filteredInput = $this->getSanitizedInput($request); |
204
|
|
|
|
205
|
15 |
|
$results->withSanitizedInputValue($filteredInput); |
206
|
|
|
|
207
|
15 |
|
if (false !== $filteredInput && $this->canProcessInput()) { |
208
|
|
|
|
209
|
6 |
|
$updated = $this->processInput($request, $filteredInput); |
210
|
|
|
|
211
|
6 |
|
$results->withUpdateAttempted(true)->withUpdateSuccessful($updated); |
212
|
|
|
} |
213
|
|
|
|
214
|
15 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* |
219
|
|
|
*/ |
220
|
6 |
|
protected function processInput(ServerRequestInterface $request, $input): bool |
221
|
|
|
{ |
222
|
6 |
|
return $this->dataManager->handleSubmittedData($request, $this->transformInput($input)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* |
227
|
|
|
*/ |
228
|
6 |
|
public function getPresetValue(ServerRequestInterface $request) |
229
|
|
|
{ |
230
|
6 |
|
$data = $this->hasDataManager() ? $this->dataManager->getCurrentData($request) : ''; |
231
|
|
|
|
232
|
6 |
|
return $this->escapeValue($this->transformData($data)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* |
237
|
|
|
*/ |
238
|
|
|
public function requestVarExists(ServerRequestInterface $request): bool |
239
|
|
|
{ |
240
|
|
|
return Request::has($request, $this->requestVar); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* |
245
|
|
|
*/ |
246
|
15 |
|
protected function getRawInput(ServerRequestInterface $request) |
247
|
|
|
{ |
248
|
15 |
|
return Request::var($request, $this->requestVar); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* |
253
|
|
|
*/ |
254
|
15 |
|
public function getSanitizedInput(ServerRequestInterface $request) |
255
|
|
|
{ |
256
|
15 |
|
return $this->filterInput($this->getRawInput($request)); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* |
261
|
|
|
*/ |
262
|
12 |
|
public function canProcessInput(): bool |
263
|
|
|
{ |
264
|
12 |
|
return $this->hasDataManager() && !$this->isProcessingDisabled(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* |
269
|
|
|
*/ |
270
|
6 |
|
protected function transformData($data) |
271
|
|
|
{ |
272
|
6 |
|
return $this->transformer->transform($data); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* |
277
|
|
|
*/ |
278
|
6 |
|
protected function transformInput($input) |
279
|
|
|
{ |
280
|
6 |
|
return $this->transformer->reverseTransform($input); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* |
285
|
|
|
*/ |
286
|
6 |
|
protected function escapeValue($value) |
287
|
|
|
{ |
288
|
6 |
|
if (!isset($this->escaper)) { |
289
|
|
|
return $value; |
290
|
|
|
} |
291
|
|
|
|
292
|
6 |
|
return !is_array($value) |
293
|
6 |
|
? call_user_func($this->escaper, $value) |
294
|
6 |
|
: array_filter($value, $this->escaper); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* |
299
|
|
|
*/ |
300
|
6 |
|
protected function setFormFieldName() |
301
|
|
|
{ |
302
|
6 |
|
$this->formField->setName($this->getRequestVar()); |
303
|
|
|
|
304
|
6 |
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* |
309
|
|
|
*/ |
310
|
6 |
|
public function setFormFieldValue(ServerRequestInterface $request) |
311
|
|
|
{ |
312
|
6 |
|
$this->formField->setValue($this->getPresetValue($request)); |
313
|
|
|
|
314
|
6 |
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* |
319
|
|
|
*/ |
320
|
6 |
|
public function render(ServerRequestInterface $request): ?FormFieldInterface |
321
|
|
|
{ |
322
|
6 |
|
return $this->prepareFormFieldForRendering($request)->getFormField(); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* |
327
|
|
|
*/ |
328
|
6 |
|
protected function prepareFormFieldForRendering(ServerRequestInterface $request) |
329
|
|
|
{ |
330
|
6 |
|
return $this->setFormFieldName()->setFormFieldValue($request); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* |
335
|
|
|
*/ |
336
|
3 |
|
protected static function lazyEscaper($value) |
337
|
|
|
{ |
338
|
3 |
|
return $value; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|