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