|
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 FormFieldController 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
|
|
|
private $processingDisabled = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
*/ |
|
56
|
57 |
|
public function __construct( |
|
57
|
|
|
string $requestVar, |
|
58
|
|
|
?FormFieldInterface $formField = null, |
|
59
|
|
|
?FieldDataManagerInterface $dataManager = null, |
|
60
|
|
|
?DataTransformerInterface $transformer = null |
|
61
|
|
|
) { |
|
62
|
57 |
|
$this->requestVar = $requestVar; |
|
63
|
57 |
|
$this->transformer = $transformer ?? new LazyTransformer(); |
|
64
|
|
|
|
|
65
|
57 |
|
if (isset($formField)) { |
|
66
|
3 |
|
$this->setFormField($formField); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
57 |
|
if (isset($dataManager)) { |
|
70
|
3 |
|
$this->setDataManager($dataManager); |
|
71
|
|
|
} |
|
72
|
57 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the value of postVar |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
15 |
|
public function getRequestVar(): string |
|
80
|
|
|
{ |
|
81
|
15 |
|
return $this->requestVar; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the value of formField |
|
86
|
|
|
* |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
12 |
|
public function getFormField(): ?FormFieldInterface |
|
90
|
|
|
{ |
|
91
|
12 |
|
return $this->formField; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set the value of formField |
|
96
|
|
|
* |
|
97
|
|
|
* @param mixed $formField |
|
98
|
|
|
* |
|
99
|
|
|
* @return self |
|
100
|
|
|
*/ |
|
101
|
12 |
|
public function setFormField(FormFieldInterface $formField) |
|
102
|
|
|
{ |
|
103
|
12 |
|
$this->formField = $formField; |
|
104
|
|
|
|
|
105
|
12 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the value of dataManager |
|
110
|
|
|
* |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
6 |
|
public function getDataManager(): ?FieldDataManagerInterface |
|
114
|
|
|
{ |
|
115
|
6 |
|
return $this->dataManager; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Set the value of dataManager |
|
120
|
|
|
* |
|
121
|
|
|
* @param FieldDataManagerInterface $dataManager |
|
122
|
|
|
* |
|
123
|
|
|
* @return self |
|
124
|
|
|
*/ |
|
125
|
24 |
|
public function setDataManager(FieldDataManagerInterface $dataManager) |
|
126
|
|
|
{ |
|
127
|
24 |
|
$this->dataManager = $dataManager; |
|
128
|
|
|
|
|
129
|
24 |
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get hasDataManager |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
24 |
|
public function hasDataManager(): bool |
|
138
|
|
|
{ |
|
139
|
24 |
|
return isset($this->dataManager); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get the value of transformer |
|
144
|
|
|
* |
|
145
|
|
|
* @return DataTransformerInterface |
|
146
|
|
|
*/ |
|
147
|
3 |
|
public function getTransformer(): DataTransformerInterface |
|
148
|
|
|
{ |
|
149
|
3 |
|
return $this->transformer; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get callback to escape value on display |
|
154
|
|
|
* |
|
155
|
|
|
* @return callable|null |
|
156
|
|
|
*/ |
|
157
|
6 |
|
public function getEscaper(): ?callable |
|
158
|
|
|
{ |
|
159
|
6 |
|
return $this->escaper; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Set callback to escape value on display |
|
164
|
|
|
* |
|
165
|
|
|
* @param callable|null $escape Callback to escape value on display |
|
166
|
|
|
* |
|
167
|
|
|
* @return self |
|
168
|
|
|
*/ |
|
169
|
6 |
|
public function setEscaper(?callable $escaper) |
|
170
|
|
|
{ |
|
171
|
6 |
|
$this->escaper = $escaper; |
|
172
|
|
|
|
|
173
|
6 |
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get the value of savingDisabled |
|
178
|
|
|
* |
|
179
|
|
|
* @return bool |
|
180
|
|
|
*/ |
|
181
|
12 |
|
public function isProcessingDisabled(): bool |
|
182
|
|
|
{ |
|
183
|
12 |
|
return $this->processingDisabled; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Set the value of savingDisabled |
|
188
|
|
|
* |
|
189
|
|
|
* @param bool $savingDisabled |
|
190
|
|
|
* |
|
191
|
|
|
* @return self |
|
192
|
|
|
*/ |
|
193
|
3 |
|
public function setProcessingDisabled(bool $savingDisabled) |
|
194
|
|
|
{ |
|
195
|
3 |
|
$this->processingDisabled = $savingDisabled; |
|
196
|
|
|
|
|
197
|
3 |
|
return $this; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* {@inheritDoc} |
|
202
|
|
|
*/ |
|
203
|
15 |
|
public function process(ServerRequestInterface $request): FieldOperationCacheInterface |
|
204
|
|
|
{ |
|
205
|
15 |
|
$results = new FieldOperationCacheBuilder(); |
|
206
|
15 |
|
$requestVarExists = $this->requestVarExists($request); |
|
207
|
15 |
|
$results->withRequestVarPresent($requestVarExists); |
|
208
|
|
|
|
|
209
|
15 |
|
if (!$requestVarExists) { |
|
210
|
3 |
|
return $results; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
12 |
|
$this->processData($request, $results); |
|
214
|
|
|
|
|
215
|
12 |
|
$results->withRuleViolations($this->getViolations()); |
|
216
|
|
|
|
|
217
|
12 |
|
$this->clearViolations(); |
|
218
|
|
|
|
|
219
|
12 |
|
return $results; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* |
|
224
|
|
|
*/ |
|
225
|
12 |
|
protected function processData(ServerRequestInterface $request, FieldOperationCacheBuilder $results) |
|
226
|
|
|
{ |
|
227
|
12 |
|
$filteredInput = $this->getSanitizedInput($request); |
|
228
|
|
|
|
|
229
|
12 |
|
$results->withSanitizedInputValue($filteredInput); |
|
230
|
|
|
|
|
231
|
12 |
|
if ($this->canProcessInput() && false !== $filteredInput) { |
|
232
|
|
|
|
|
233
|
6 |
|
$updated = $this->dataManager->handleSubmittedData($request, $filteredInput); |
|
234
|
|
|
|
|
235
|
6 |
|
$results->withUpdateAttempted(true)->withUpdateSuccessful($updated); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
12 |
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* |
|
243
|
|
|
*/ |
|
244
|
6 |
|
public function getPresetValue(ServerRequestInterface $request) |
|
245
|
|
|
{ |
|
246
|
6 |
|
$data = $this->dataManager->getCurrentData($request); |
|
247
|
|
|
|
|
248
|
6 |
|
return $this->escapeValue($this->transformData($data)); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* |
|
253
|
|
|
*/ |
|
254
|
15 |
|
public function requestVarExists(ServerRequestInterface $request): bool |
|
255
|
|
|
{ |
|
256
|
15 |
|
return Request::has($request, $this->requestVar); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* |
|
261
|
|
|
*/ |
|
262
|
12 |
|
private function getRawInput(ServerRequestInterface $request) |
|
263
|
|
|
{ |
|
264
|
12 |
|
return Request::var($request, $this->requestVar); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* |
|
269
|
|
|
*/ |
|
270
|
12 |
|
public function getSanitizedInput(ServerRequestInterface $request) |
|
271
|
|
|
{ |
|
272
|
12 |
|
return $this->filterInput($this->transformInput($this->getRawInput($request))); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* |
|
277
|
|
|
*/ |
|
278
|
12 |
|
public function canProcessInput(): bool |
|
279
|
|
|
{ |
|
280
|
12 |
|
return $this->hasDataManager() && !$this->isProcessingDisabled(); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* |
|
285
|
|
|
*/ |
|
286
|
6 |
|
protected function transformData($data) |
|
287
|
|
|
{ |
|
288
|
6 |
|
return $this->transformer->transform($data); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* |
|
293
|
|
|
*/ |
|
294
|
12 |
|
protected function transformInput($input) |
|
295
|
|
|
{ |
|
296
|
12 |
|
return $this->transformer->reverseTransform($input); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* |
|
301
|
|
|
*/ |
|
302
|
6 |
|
protected function escapeValue($value) |
|
303
|
|
|
{ |
|
304
|
6 |
|
if (!isset($this->escaper)) { |
|
305
|
3 |
|
return $value; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
6 |
|
return !is_array($value) |
|
309
|
6 |
|
? call_user_func($this->escaper, $value) |
|
310
|
6 |
|
: array_filter($value, $this->escaper); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* |
|
315
|
|
|
*/ |
|
316
|
6 |
|
protected function setFormFieldName() |
|
317
|
|
|
{ |
|
318
|
6 |
|
$this->formField->setName($this->getRequestVar()); |
|
319
|
|
|
|
|
320
|
6 |
|
return $this; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* |
|
325
|
|
|
*/ |
|
326
|
6 |
|
public function setFormFieldValue(ServerRequestInterface $request) |
|
327
|
|
|
{ |
|
328
|
6 |
|
if ($this->hasDataManager()) { |
|
329
|
6 |
|
$this->formField->setValue($this->getPresetValue($request)); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
6 |
|
return $this; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* |
|
337
|
|
|
*/ |
|
338
|
6 |
|
public function render(ServerRequestInterface $request): ?FormFieldInterface |
|
339
|
|
|
{ |
|
340
|
|
|
return $this |
|
341
|
6 |
|
->prepareFormFieldForRendering($request) |
|
342
|
6 |
|
->getFormField(); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* |
|
347
|
|
|
*/ |
|
348
|
6 |
|
protected function prepareFormFieldForRendering(ServerRequestInterface $request) |
|
349
|
|
|
{ |
|
350
|
|
|
return $this |
|
351
|
6 |
|
->setFormFieldValue($request) |
|
352
|
6 |
|
->setFormFieldName(); |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
|