1
|
|
|
<?php |
2
|
|
|
namespace ElevenLabs\Api\Service\Denormalizer; |
3
|
|
|
|
4
|
|
|
use ElevenLabs\Api\Definition\ResponseDefinition; |
5
|
|
|
use ElevenLabs\Api\Service\Pagination\PaginationProvider; |
6
|
|
|
use ElevenLabs\Api\Service\Resource\Collection; |
7
|
|
|
use ElevenLabs\Api\Service\Resource\Item; |
8
|
|
|
use ElevenLabs\Api\Service\Resource\Resource; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
12
|
|
|
|
13
|
|
|
class ResourceDenormalizer implements DenormalizerInterface |
14
|
|
|
{ |
15
|
|
|
private $paginationProvider; |
16
|
|
|
|
17
|
15 |
|
public function __construct(PaginationProvider $paginationProvider = null) |
18
|
|
|
{ |
19
|
15 |
|
$this->paginationProvider = $paginationProvider; |
20
|
15 |
|
} |
21
|
|
|
|
22
|
|
|
/** {@inheritdoc} */ |
23
|
9 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
24
|
|
|
{ |
25
|
|
|
/** @var ResponseInterface $response */ |
26
|
9 |
|
$response = $context['response']; |
27
|
|
|
|
28
|
|
|
/** @var RequestInterface $request */ |
29
|
9 |
|
$request = $context['request']; |
30
|
|
|
|
31
|
|
|
/** @var ResponseDefinition $definition */ |
32
|
9 |
|
$definition = $context['responseDefinition']; |
33
|
|
|
|
34
|
9 |
|
if (! $definition->hasBodySchema()) { |
35
|
1 |
|
throw new \LogicException( |
36
|
1 |
|
sprintf( |
37
|
1 |
|
'Cannot transform the response into a resource. You need to provide a schema for response %d in %s %s', |
38
|
1 |
|
$response->getStatusCode(), |
39
|
1 |
|
$request->getMethod(), |
40
|
1 |
|
$request->getUri()->getPath() |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
8 |
|
$schema = $definition->getBodySchema(); |
46
|
8 |
|
$meta = ['headers' => $response->getHeaders()]; |
47
|
|
|
|
48
|
8 |
|
if ($this->getSchemaType($schema) === 'array') { |
49
|
2 |
|
$pagination = null; |
50
|
2 |
|
if ($this->paginationProvider !== null && |
51
|
2 |
|
$this->paginationProvider->supportPagination($data, $response, $definition) |
52
|
|
|
) { |
53
|
1 |
|
$pagination = $this->paginationProvider->getPagination($data, $response, $definition); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
return new Collection($data, $meta, $pagination); |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
return new Item($data, $meta); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** {@inheritdoc} */ |
63
|
4 |
|
public function supportsDenormalization($data, $type, $format = null) |
64
|
|
|
{ |
65
|
4 |
|
return ($type === Resource::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Extract the type for a given JSON Schema |
70
|
|
|
* |
71
|
|
|
* @param \stdClass $schema |
72
|
|
|
* @throws \RuntimeException |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
8 |
|
private function getSchemaType(\stdClass $schema) |
77
|
|
|
{ |
78
|
8 |
|
if (isset($schema->type) === true) { |
79
|
6 |
|
return $schema->type; |
80
|
|
|
} |
81
|
2 |
|
if (isset($schema->allOf[0]->type) === true) { |
82
|
1 |
|
return $schema->allOf[0]->type; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
throw new \RuntimeException('Cannot extract type from schema'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|