|
1
|
|
|
<?php |
|
2
|
|
|
namespace ElevenLabs\Api\Definition; |
|
3
|
|
|
|
|
4
|
|
|
class Parameters implements \Serializable, \IteratorAggregate |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* @var Parameter[] |
|
8
|
|
|
*/ |
|
9
|
|
|
private $parameters; |
|
10
|
|
|
|
|
11
|
22 |
|
public function __construct(array $parameters) |
|
12
|
|
|
{ |
|
13
|
22 |
|
$this->parameters = []; |
|
14
|
22 |
|
foreach ($parameters as $parameter) { |
|
15
|
14 |
|
$this->addParameter($parameter); |
|
16
|
|
|
} |
|
17
|
21 |
|
} |
|
18
|
|
|
|
|
19
|
1 |
|
public function getIterator() |
|
20
|
|
|
{ |
|
21
|
1 |
|
return new \ArrayIterator($this->parameters); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
2 |
|
public function hasBodySchema() |
|
25
|
|
|
{ |
|
26
|
2 |
|
$body = $this->getBody(); |
|
27
|
|
|
|
|
28
|
2 |
|
return ($body !== null && $body->hasSchema()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* JSON Schema for the body. |
|
33
|
|
|
* |
|
34
|
|
|
* @return \stdClass|null |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function getBodySchema() |
|
37
|
|
|
{ |
|
38
|
2 |
|
return $this->getBody()->getSchema(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
public function hasPathSchema() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->getPathSchema() !== null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* JSON Schema for the path parameters. |
|
51
|
|
|
* |
|
52
|
|
|
* @return \stdClass|null |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getPathSchema() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->getSchema($this->getPath()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function hasQueryParametersSchema() |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->getQueryParametersSchema() !== null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* JSON Schema for the query parameters. |
|
69
|
|
|
* |
|
70
|
|
|
* @return \stdClass|null |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function getQueryParametersSchema() |
|
73
|
|
|
{ |
|
74
|
2 |
|
return $this->getSchema($this->getQuery()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function hasHeadersSchema() |
|
81
|
|
|
{ |
|
82
|
2 |
|
return $this->getHeadersSchema() !== null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* JSON Schema for the headers. |
|
87
|
|
|
* |
|
88
|
|
|
* @return \stdClass|null |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function getHeadersSchema() |
|
91
|
|
|
{ |
|
92
|
2 |
|
return $this->getSchema($this->getHeaders()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return Parameter[] |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function getPath() |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->findByLocation('path'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return Parameter[] |
|
105
|
|
|
*/ |
|
106
|
2 |
|
public function getQuery() |
|
107
|
|
|
{ |
|
108
|
2 |
|
return $this->findByLocation('query'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Parameter[] |
|
113
|
|
|
*/ |
|
114
|
2 |
|
public function getHeaders() |
|
115
|
|
|
{ |
|
116
|
2 |
|
return $this->findByLocation('header'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return Parameter|null |
|
121
|
|
|
*/ |
|
122
|
3 |
|
public function getBody() |
|
123
|
|
|
{ |
|
124
|
3 |
|
$match = $this->findByLocation('body'); |
|
125
|
3 |
|
if (empty($match)) { |
|
126
|
1 |
|
return null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
return current($match); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get one request parameter by name |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $name |
|
136
|
|
|
* @return Parameter|null |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function getByName($name) |
|
139
|
|
|
{ |
|
140
|
1 |
|
if (! isset($this->parameters[$name])) { |
|
141
|
|
|
return null; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
return $this->parameters[$name]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param Parameter[] $parameters |
|
149
|
|
|
* |
|
150
|
|
|
* @return \stdClass|null |
|
151
|
|
|
*/ |
|
152
|
3 |
|
private function getSchema(array $parameters) |
|
153
|
|
|
{ |
|
154
|
3 |
|
if (empty($parameters)) { |
|
155
|
1 |
|
return null; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
2 |
|
$schema = new \stdClass(); |
|
159
|
2 |
|
$schema->type = 'object'; |
|
160
|
2 |
|
$schema->required = []; |
|
161
|
2 |
|
$schema->properties = new \stdClass(); |
|
162
|
2 |
|
foreach ($parameters as $name => $parameter) { |
|
163
|
2 |
|
if ($parameter->isRequired()) { |
|
164
|
1 |
|
$schema->required[] = $parameter->getName(); |
|
165
|
|
|
} |
|
166
|
2 |
|
$schema->properties->{$name} = $parameter->getSchema(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
2 |
|
return $schema; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
3 |
|
public function serialize() |
|
173
|
|
|
{ |
|
174
|
3 |
|
return serialize(['parameters' => $this->parameters]); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
3 |
|
public function unserialize($serialized) |
|
178
|
|
|
{ |
|
179
|
3 |
|
$data = unserialize($serialized); |
|
180
|
3 |
|
$this->parameters = $data['parameters']; |
|
181
|
3 |
|
} |
|
182
|
|
|
|
|
183
|
6 |
|
private function findByLocation($location) |
|
184
|
|
|
{ |
|
185
|
6 |
|
return array_filter( |
|
186
|
6 |
|
$this->parameters, |
|
187
|
|
|
function (Parameter $parameter) use ($location) { |
|
188
|
5 |
|
return $parameter->getLocation() === $location; |
|
189
|
6 |
|
} |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
14 |
|
private function addParameter(Parameter $parameter) |
|
194
|
|
|
{ |
|
195
|
14 |
|
if (!in_array($parameter->getLocation(), Parameter::LOCATIONS, true)) { |
|
196
|
1 |
|
throw new \InvalidArgumentException( |
|
197
|
1 |
|
sprintf( |
|
198
|
1 |
|
'%s is not a supported parameter location, supported: %s', |
|
199
|
1 |
|
$parameter->getLocation(), |
|
200
|
1 |
|
implode(', ', Parameter::LOCATIONS) |
|
201
|
|
|
) |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
13 |
|
$this->parameters[$parameter->getName()] = $parameter; |
|
206
|
13 |
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|