|
1
|
|
|
<?php |
|
2
|
|
|
namespace ElevenLabs\Api\Service; |
|
3
|
|
|
|
|
4
|
|
|
use ElevenLabs\Api\Decoder\Adapter\SymfonyDecoderAdapter; |
|
5
|
|
|
use ElevenLabs\Api\Factory\CachedSchemaFactoryDecorator; |
|
6
|
|
|
use ElevenLabs\Api\Factory\SwaggerSchemaFactory; |
|
7
|
|
|
use ElevenLabs\Api\Service\Denormalizer\ResourceDenormalizer; |
|
8
|
|
|
use ElevenLabs\Api\Service\Pagination\PaginationProvider; |
|
9
|
|
|
use ElevenLabs\Api\Validator\MessageValidator; |
|
10
|
|
|
use Http\Client\HttpClient; |
|
11
|
|
|
use Http\Discovery\HttpClientDiscovery; |
|
12
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
13
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
|
14
|
|
|
use Http\Message\MessageFactory; |
|
15
|
|
|
use Http\Message\UriFactory; |
|
16
|
|
|
use JsonSchema\Validator; |
|
17
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
18
|
|
|
use Rize\UriTemplate; |
|
19
|
|
|
use Symfony\Component\Serializer\Encoder\ChainDecoder; |
|
20
|
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface; |
|
21
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
|
22
|
|
|
use Symfony\Component\Serializer\Encoder\XmlEncoder; |
|
23
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
24
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
25
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Builder for ApiService instances |
|
29
|
|
|
*/ |
|
30
|
|
|
class ApiServiceBuilder |
|
31
|
|
|
{ |
|
32
|
|
|
private $httpClient; |
|
33
|
|
|
private $messageFactory; |
|
34
|
|
|
private $uriFactory; |
|
35
|
|
|
private $serializer; |
|
36
|
|
|
private $denormalizers = []; |
|
37
|
|
|
private $encoders = []; |
|
38
|
|
|
private $schema; |
|
39
|
|
|
private $cache; |
|
40
|
|
|
private $config = []; |
|
41
|
|
|
private $paginationProvider = null; |
|
42
|
|
|
private $requestValidator; |
|
43
|
|
|
|
|
44
|
6 |
|
public static function create() |
|
45
|
|
|
{ |
|
46
|
6 |
|
return new static(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function withCacheProvider(CacheItemPoolInterface $cache) |
|
50
|
|
|
{ |
|
51
|
1 |
|
$this->cache = $cache; |
|
52
|
|
|
|
|
53
|
1 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
5 |
|
public function withHttpClient(HttpClient $httpClient) |
|
57
|
|
|
{ |
|
58
|
5 |
|
$this->httpClient = $httpClient; |
|
59
|
|
|
|
|
60
|
5 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
public function withMessageFactory(MessageFactory $messageFactory) |
|
64
|
|
|
{ |
|
65
|
3 |
|
$this->messageFactory = $messageFactory; |
|
66
|
|
|
|
|
67
|
3 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function withUriFactory(UriFactory $uriFactory) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->uriFactory = $uriFactory; |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function withSerializer(SerializerInterface $serializer) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->serializer = $serializer; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function withEncoder(EncoderInterface $encoder) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->encoders[] = $encoder; |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function withDenormalizer(NormalizerInterface $normalizer) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->denormalizers[] = $normalizer; |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function withPaginationProvider(PaginationProvider $paginationProvider) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->paginationProvider = $paginationProvider; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
public function withBaseUri($baseUri) |
|
104
|
|
|
{ |
|
105
|
2 |
|
$this->config['baseUri'] = $baseUri; |
|
106
|
|
|
|
|
107
|
2 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
public function disableRequestValidation() |
|
111
|
|
|
{ |
|
112
|
1 |
|
$this->config['validateRequest'] = false; |
|
113
|
|
|
|
|
114
|
1 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
public function enableResponseValidation() |
|
118
|
|
|
{ |
|
119
|
2 |
|
$this->config['validateResponse'] = true; |
|
120
|
|
|
|
|
121
|
2 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function returnResponse() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->config['returnResponse'] = true; |
|
127
|
|
|
|
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
8 |
|
public function build($schemaPath) |
|
132
|
|
|
{ |
|
133
|
|
|
// Build serializer |
|
134
|
8 |
|
if ($this->serializer === null) { |
|
135
|
8 |
|
if (empty($this->encoders)) { |
|
136
|
8 |
|
$this->encoders = [new JsonEncoder(), new XmlEncoder()]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
8 |
|
if (empty($this->denormalizers)) { |
|
140
|
8 |
|
$this->denormalizers[] = new ResourceDenormalizer($this->paginationProvider); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
8 |
|
$this->serializer = new Serializer( |
|
144
|
8 |
|
$this->denormalizers, |
|
145
|
8 |
|
$this->encoders |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
8 |
|
if ($this->uriFactory === null) { |
|
150
|
8 |
|
$this->uriFactory = UriFactoryDiscovery::find(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
8 |
|
if ($this->messageFactory === null) { |
|
154
|
5 |
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
8 |
|
if ($this->httpClient === null) { |
|
158
|
3 |
|
$this->httpClient = HttpClientDiscovery::find(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
8 |
|
$schemaFactory = new SwaggerSchemaFactory(); |
|
162
|
8 |
|
if ($this->cache !== null) { |
|
163
|
1 |
|
$schemaFactory = new CachedSchemaFactoryDecorator( |
|
164
|
1 |
|
$this->cache, |
|
165
|
1 |
|
$schemaFactory |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
8 |
|
$this->schema = $schemaFactory->createSchema($schemaPath); |
|
170
|
|
|
|
|
171
|
8 |
|
if (!isset($this->requestValidator)) { |
|
172
|
8 |
|
$this->requestValidator = new MessageValidator( |
|
173
|
8 |
|
new Validator(), |
|
174
|
8 |
|
new SymfonyDecoderAdapter(new ChainDecoder($this->encoders)) |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
8 |
|
return new ApiService( |
|
179
|
8 |
|
$this->uriFactory, |
|
180
|
8 |
|
new UriTemplate(), |
|
181
|
8 |
|
$this->httpClient, |
|
182
|
8 |
|
$this->messageFactory, |
|
183
|
8 |
|
$this->schema, |
|
184
|
8 |
|
$this->requestValidator, |
|
185
|
8 |
|
$this->serializer, |
|
186
|
8 |
|
$this->config |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|