Conditions | 5 |
Paths | 5 |
Total Lines | 28 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
26 | public function hydrate(ResponseInterface $response, string $class = null) |
||
27 | { |
||
28 | if (null === $class) { |
||
29 | throw new \BadMethodCallException('The ModelHydrator requires a model class as the second parameter.'); |
||
30 | } |
||
31 | |||
32 | if (!$this->isJsonResponse($response)) { |
||
33 | $message = 'The ModelHydrator cannot hydrate a response with Content-Type: '; |
||
34 | throw new \BadMethodCallException($message . $response->getHeaderLine('Content-Type')); |
||
35 | } |
||
36 | |||
37 | $body = $this->getBodyContent($response); |
||
38 | if (JSON_ERROR_NONE !== \json_last_error()) { |
||
39 | throw new \BadMethodCallException(sprintf( |
||
40 | 'Error (%d) when trying to json_decode response: %s', |
||
41 | \json_last_error(), |
||
42 | \json_last_error_msg() |
||
43 | )); |
||
44 | } |
||
45 | |||
46 | $reflection = new ReflectionClass($class); |
||
47 | if ($reflection->implementsInterface(CreatableFromArray::class)) { |
||
48 | $model = \call_user_func($class.self::METHOD_CREATE, $body); |
||
49 | } else { |
||
50 | $model = new $class($body); |
||
51 | } |
||
52 | |||
53 | return $model; |
||
54 | } |
||
56 |