Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.12% |
39 / 41 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| FormSubmitDecoder | |
95.12% |
39 / 41 |
|
60.00% |
3 / 5 |
30 | |
0.00% |
0 / 1 |
| withOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| withParsedBody | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| decode | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
13 | |||
| requiresCsrf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fillIn | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
14.03 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\Encoders; |
| 3 | |
| 4 | use Apie\Serializer\Interfaces\DecoderInterface; |
| 5 | |
| 6 | class FormSubmitDecoder implements DecoderInterface |
| 7 | { |
| 8 | public function withOptions(string $options): DecoderInterface |
| 9 | { |
| 10 | return $this; |
| 11 | } |
| 12 | |
| 13 | public function withParsedBody(null|array|object $parsedBody): DecoderInterface |
| 14 | { |
| 15 | return $this; |
| 16 | } |
| 17 | |
| 18 | public function decode(string $input): string|int|float|bool|array|null |
| 19 | { |
| 20 | parse_str($input, $formContents); |
| 21 | $data = $formContents['form'] ?? []; |
| 22 | |
| 23 | // a form field can submit hidden fields with <input name="_apie[typehint][fieldName]"> to provide a null or empty array |
| 24 | $typehints = $formContents['_apie'] ?? []; |
| 25 | if (is_string($typehints)) { |
| 26 | return match ($typehints) { |
| 27 | 'string' => '', |
| 28 | 'int' => 0, |
| 29 | 'float' => 0.0, |
| 30 | 'bool' => false, |
| 31 | 'true' => true, |
| 32 | 'false' => false, |
| 33 | 'array' => ['_csrf' => $formContents['_csrf'] ?? 'no csrf'], |
| 34 | 'object' => ['_csrf' => $formContents['_csrf'] ?? 'no csrf'], |
| 35 | default => null, |
| 36 | }; |
| 37 | } |
| 38 | // @phpstan-ignore function.alreadyNarrowedType |
| 39 | if (is_array($typehints)) { |
| 40 | foreach ($typehints as $key => $typehintData) { |
| 41 | $this->fillIn($data, $key, $typehintData); |
| 42 | } |
| 43 | } |
| 44 | $data['_csrf'] = $formContents['_csrf'] ?? 'no csrf'; |
| 45 | return $data; |
| 46 | } |
| 47 | |
| 48 | public function requiresCsrf(): bool |
| 49 | { |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | private function fillIn(array& $data, string $key, string|int|float|bool|array|null $typehintData): void |
| 54 | { |
| 55 | if (!isset($data[$key])) { |
| 56 | if (is_array($typehintData)) { |
| 57 | $data[$key] = []; |
| 58 | } else { |
| 59 | $data[$key] = match ($typehintData) { |
| 60 | 'string' => '', |
| 61 | 'int' => 0, |
| 62 | 'float' => 0.0, |
| 63 | 'bool' => false, |
| 64 | 'true' => true, |
| 65 | 'false' => false, |
| 66 | 'array' => [], |
| 67 | 'object' => [], |
| 68 | default => null, |
| 69 | }; |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | if (is_array($typehintData)) { |
| 74 | foreach ($typehintData as $subKey => $typehintSubdata) { |
| 75 | $this->fillIn($data[$key], $subKey, $typehintSubdata); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |