Conditions | 4 |
Paths | 4 |
Total Lines | 31 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
25 | public function transform(ResponseInterface $response, string $class) |
||
26 | { |
||
27 | if (0 !== \strpos($response->getHeaderLine('Content-Type'), 'application/json')) { |
||
28 | throw new TransformerException( |
||
29 | \sprintf( |
||
30 | 'The ModelTransformer can transform json response but %s given', |
||
31 | $response->getHeaderLine('Content-Type') |
||
32 | ) |
||
33 | ); |
||
34 | } |
||
35 | |||
36 | $body = $response->getBody()->__toString(); |
||
37 | $data = \json_decode($body, true); |
||
38 | |||
39 | if (JSON_ERROR_NONE !== \json_last_error()) { |
||
40 | throw new TransformerException( |
||
41 | \sprintf( |
||
42 | 'Invalid json response format : Error %d when trying to \json_decode response', |
||
43 | \json_last_error() |
||
44 | ) |
||
45 | ); |
||
46 | } |
||
47 | |||
48 | if (\is_subclass_of($class, FromArrayInterface::class)) { |
||
|
|||
49 | $object = \call_user_func($class.'::createFromArray', $data); |
||
50 | } else { |
||
51 | $object = new $class($data); |
||
52 | } |
||
53 | |||
54 | return $object; |
||
55 | } |
||
56 | } |
||
57 |