Conditions | 4 |
Paths | 4 |
Total Lines | 19 |
Code Lines | 11 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 20 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
29 | public function hydrate(ResponseInterface $response, string $class) |
||
30 | { |
||
31 | $body = $response->getBody()->__toString(); |
||
32 | if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) { |
||
33 | throw new HydrationException('The ModelHydrator cannot hydrate response with Content-Type:'.$response->getHeaderLine('Content-Type')); |
||
34 | } |
||
35 | |||
36 | $data = json_decode($body, true); |
||
37 | if (JSON_ERROR_NONE !== json_last_error()) { |
||
38 | throw new HydrationException(sprintf('Error (%d) when trying to json_decode response', json_last_error())); |
||
39 | } |
||
40 | |||
41 | if (is_subclass_of($class, CreatableFromArray::class)) { |
||
42 | $object = call_user_func($class.'::createFromArray', $data); |
||
43 | } else { |
||
44 | $object = new $class($data); |
||
45 | } |
||
46 | |||
47 | return $object; |
||
48 | } |
||
50 |