1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\PhpApi\Descriptions package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\PhpApi\Descriptions\Description; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\VisiteeMixin; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author John Kleijn <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Operation implements Element |
18
|
|
|
{ |
19
|
|
|
use VisiteeMixin; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $id; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $path; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $method; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Parameter[] |
38
|
|
|
*/ |
39
|
|
|
protected $parameters; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Schema |
43
|
|
|
*/ |
44
|
|
|
protected $requestSchema; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Response[] |
48
|
|
|
*/ |
49
|
|
|
protected $responses; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $secured; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
private $extensions; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Operation constructor. |
63
|
|
|
* |
64
|
|
|
* @param string $id |
65
|
|
|
* @param string $path |
66
|
|
|
* @param string $method |
67
|
|
|
* @param Parameter[] $parameters |
68
|
|
|
* @param Schema $requestSchema |
69
|
|
|
* @param Response[] $responses |
70
|
|
|
* @param array $extensions |
71
|
|
|
* @param bool $isSecured |
72
|
|
|
*/ |
73
|
|
|
public function __construct( |
74
|
|
|
string $id, |
75
|
|
|
string $path, |
76
|
|
|
string $method, |
77
|
|
|
array $parameters = [], |
78
|
|
|
Schema $requestSchema = null, |
79
|
|
|
array $responses = [], |
80
|
|
|
array $extensions = [], |
81
|
|
|
$isSecured = false |
82
|
|
|
) |
83
|
|
|
{ |
84
|
|
|
$this->id = $id; |
85
|
|
|
$this->path = $path; |
86
|
|
|
$this->method = $method; |
87
|
|
|
$this->parameters = $parameters; |
88
|
|
|
$this->requestSchema = $requestSchema; |
89
|
|
|
$this->responses = $responses; |
90
|
|
|
$this->extensions = $extensions; |
91
|
|
|
$this->secured = $isSecured; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $name |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public function getExtension(string $name) |
100
|
|
|
{ |
101
|
|
|
return isset($this->extensions[$name]) ? $this->extensions[$name] : null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getPath(): string |
108
|
|
|
{ |
109
|
|
|
return $this->path; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getId(): string |
116
|
|
|
{ |
117
|
|
|
return $this->id; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return int[] |
122
|
|
|
*/ |
123
|
|
|
public function getStatusCodes(): array |
124
|
|
|
{ |
125
|
|
|
return array_keys($this->responses); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param int $code |
130
|
|
|
* |
131
|
|
|
* @return Response |
132
|
|
|
*/ |
133
|
|
|
public function getResponse(int $code): Response |
134
|
|
|
{ |
135
|
|
|
if (!isset($this->responses[$code])) { |
136
|
|
|
if (isset($this->responses[0])) { |
137
|
|
|
// Return default response |
138
|
|
|
return $this->responses[0]; |
139
|
|
|
} |
140
|
|
|
throw new \InvalidArgumentException( |
141
|
|
|
"Operation '{$this->getId()}' does not have a definition for status '$code'" . |
142
|
|
|
" (has " . implode(', ', array_keys($this->responses)) . ')' |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this->responses[$code]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return Response[] |
151
|
|
|
*/ |
152
|
|
|
public function getResponses() |
153
|
|
|
{ |
154
|
|
|
return array_values($this->responses); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return Schema |
159
|
|
|
*/ |
160
|
|
|
public function getRequestSchema(): Schema |
161
|
|
|
{ |
162
|
|
|
return $this->requestSchema; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function hasParameters(): bool |
169
|
|
|
{ |
170
|
|
|
return (bool)count($this->parameters); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return Parameter[] |
175
|
|
|
*/ |
176
|
|
|
public function getParameters(): array |
177
|
|
|
{ |
178
|
|
|
return $this->hasParameters() ? $this->parameters : []; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $path |
|
|
|
|
183
|
|
|
* @param string $method |
|
|
|
|
184
|
|
|
* |
185
|
|
|
* @return Parameter|null |
186
|
|
|
*/ |
187
|
|
|
public function getRequestBodyParameter() |
188
|
|
|
{ |
189
|
|
|
foreach ($this->getParameters() as $parameter) { |
190
|
|
|
if ($parameter->getIn() === Parameter::IN_BODY) { |
191
|
|
|
return $parameter; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $name |
198
|
|
|
* |
199
|
|
|
* @return Parameter |
200
|
|
|
*/ |
201
|
|
|
public function getParameter(string $name): Parameter |
202
|
|
|
{ |
203
|
|
|
foreach ($this->getParameters() as $parameter) { |
204
|
|
|
if ($parameter->getName() === $name) { |
205
|
|
|
return $parameter; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
throw new \OutOfBoundsException("Parameter '$name' does not exist"); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getMethod(): string |
215
|
|
|
{ |
216
|
|
|
return $this->method; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return boolean |
221
|
|
|
*/ |
222
|
|
|
public function isSecured(): bool |
223
|
|
|
{ |
224
|
|
|
return $this->secured; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.