| Conditions | 4 |
| Paths | 4 |
| Total Lines | 22 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 29 | public function hydrate(ResponseInterface $response, string $class = null) |
||
| 30 | { |
||
| 31 | if (null === $class) { |
||
| 32 | throw new HydrationException('The ModelHydrator requires a model class as the second parameter.'); |
||
| 33 | } |
||
| 34 | |||
| 35 | if (!$this->isJsonResponse($response)) { |
||
| 36 | $message = 'The ModelHydrator cannot hydrate a response with Content-Type: '; |
||
| 37 | |||
| 38 | throw new HydrationException($message.$response->getHeaderLine('Content-Type')); |
||
| 39 | } |
||
| 40 | |||
| 41 | $body = $this->getBodyContent($response); |
||
| 42 | |||
| 43 | $reflection = new ReflectionClass($class); |
||
| 44 | if ($reflection->implementsInterface(CreatableFromArray::class)) { |
||
| 45 | $model = \call_user_func($class.self::METHOD_CREATE, $body); |
||
| 46 | } else { |
||
| 47 | $model = new $class($body); |
||
| 48 | } |
||
| 49 | |||
| 50 | return $model; |
||
| 51 | } |
||
| 53 |