| Total Complexity | 12 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
| 1 | <?php |
||
| 13 | final class JsonBodyParser implements MiddlewareInterface |
||
| 14 | { |
||
| 15 | private bool $assoc = true; |
||
| 16 | private bool $throwException = true; |
||
| 17 | |||
| 18 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 19 | { |
||
| 20 | $contentType = $request->getHeaderLine(Header::CONTENT_TYPE); |
||
| 21 | |||
| 22 | if ($contentType && strpos(strtolower($contentType), 'application/json') !== false) { |
||
| 23 | $request = $request->withParsedBody( |
||
| 24 | $this->parse($request->getBody()->getContents()) |
||
| 25 | ); |
||
| 26 | } |
||
| 27 | |||
| 28 | return $handler->handle($request); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function withAssoc(bool $value): self |
||
| 32 | { |
||
| 33 | $new = clone $this; |
||
| 34 | $new->assoc = $value; |
||
| 35 | return $new; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function withThrowException(bool $value): self |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return mixed |
||
| 47 | */ |
||
| 48 | private function parse(string $body) |
||
| 61 | } |
||
| 62 | } |
||
| 63 |