| Total Complexity | 11 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 20 | class RequestBodyParser |
||
| 21 | { |
||
| 22 | public static function parseJson(ServerRequestInterface $request): array |
||
| 23 | { |
||
| 24 | if (!$request->hasHeader('Content-Type') || !\in_array('application/json', $request->getHeader('Content-Type'), true)) { |
||
| 25 | throw new InvalidArgumentException('Unsupported request body content type.'); |
||
| 26 | } |
||
| 27 | $parsedBody = $request->getParsedBody(); |
||
| 28 | if (\is_array($parsedBody) && 0 !== \count($parsedBody)) { |
||
| 29 | return $parsedBody; |
||
| 30 | } |
||
| 31 | |||
| 32 | $body = $request->getBody()->getContents(); |
||
| 33 | $json = \Safe\json_decode($body, true); |
||
| 34 | |||
| 35 | if (!\is_array($json)) { |
||
| 36 | throw new InvalidArgumentException('Invalid body'); |
||
| 37 | } |
||
| 38 | |||
| 39 | return $json; |
||
| 40 | } |
||
| 41 | |||
| 42 | public static function parseFormUrlEncoded(ServerRequestInterface $request): array |
||
| 55 | } |
||
| 56 | } |
||
| 57 |