Conditions | 6 |
Paths | 4 |
Total Lines | 18 |
Code Lines | 10 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
21 | public static function parseJson(ServerRequestInterface $request): array |
||
22 | { |
||
23 | if (!$request->hasHeader('Content-Type') || !in_array('application/json', $request->getHeader('Content-Type'))) { |
||
24 | throw new \InvalidArgumentException('Unsupported request body content type.'); |
||
25 | } |
||
26 | if (is_array($request->getParsedBody()) && !empty($request->getParsedBody())) { |
||
27 | return $request->getParsedBody(); |
||
28 | } |
||
29 | |||
30 | $body = $request->getBody()->getContents(); |
||
31 | $json = json_decode($body, true); |
||
32 | |||
33 | if (!is_array($json)) { |
||
34 | throw new \InvalidArgumentException('Invalid body'); |
||
35 | } |
||
36 | |||
37 | return $json; |
||
38 | } |
||
39 | |||
54 |