| Conditions | 4 |
| Paths | 4 |
| Total Lines | 20 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 22 | public function hydrate(ResponseInterface $response, string $class) |
||
| 23 | { |
||
| 24 | $body = $response->getBody()->__toString(); |
||
| 25 | if (\mb_strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) { |
||
| 26 | throw new HydrationException(sprintf('The ModelHydrator cannot hydrate response with Content-Type: %s', $response->getHeaderLine('Content-Type'))); |
||
| 27 | } |
||
| 28 | |||
| 29 | try { |
||
| 30 | $data = JSON::decode($body); |
||
| 31 | } catch (\JsonException $e) { |
||
| 32 | throw new HydrationException(\sprintf('Error (%d) when trying to json_decode response', $e->getCode())); |
||
| 33 | } |
||
| 34 | |||
| 35 | if (\is_subclass_of($class, CreatableFromArray::class)) { |
||
| 36 | $object = \call_user_func($class.'::createFromArray', $data); |
||
| 37 | } else { |
||
| 38 | $object = new $class($data); |
||
| 39 | } |
||
| 40 | |||
| 41 | return $object; |
||
| 42 | } |
||
| 44 |