| Total Complexity | 14 |
| Total Lines | 62 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | final class UrlEncodedTypeDecoder implements TypeDecoderInterface |
||
| 10 | { |
||
| 11 | public function getContentType(): string |
||
| 12 | { |
||
| 13 | return 'application/x-www-form-urlencoded'; |
||
| 14 | 2 | } |
|
| 15 | |||
| 16 | 2 | /** |
|
| 17 | * @throws DeserializerRuntimeException |
||
| 18 | */ |
||
| 19 | public function decode(string $data): array |
||
| 20 | { |
||
| 21 | $rawData = []; |
||
| 22 | parse_str($data, $rawData); |
||
| 23 | |||
| 24 | if ('' !== $data && [] === $rawData) { |
||
| 25 | throw DeserializerRuntimeException::createNotParsable($this->getContentType()); |
||
| 26 | 3 | } |
|
| 27 | |||
| 28 | 3 | return $this->fixValues($rawData); |
|
| 29 | 3 | } |
|
| 30 | |||
| 31 | 3 | private function fixValues(array $rawData): array |
|
| 32 | 1 | { |
|
| 33 | $data = []; |
||
| 34 | foreach ($rawData as $key => $value) { |
||
| 35 | 2 | if (is_array($value)) { |
|
| 36 | $data[$key] = $this->fixValues($value); |
||
| 37 | } else { |
||
| 38 | $data[$key] = $this->fixValue($value); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | return $data; |
||
| 43 | 2 | } |
|
| 44 | |||
| 45 | 2 | /** |
|
| 46 | 2 | * @return mixed |
|
| 47 | 2 | */ |
|
| 48 | 2 | private function fixValue(string $value) |
|
| 71 | } |
||
| 72 | } |
||
| 73 |