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
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $path; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $method; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Parameter[] |
39
|
|
|
*/ |
40
|
|
|
protected $parameters; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Schema |
44
|
|
|
*/ |
45
|
|
|
protected $requestSchema; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Response[] |
49
|
|
|
*/ |
50
|
|
|
protected $responses; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $extensions; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Operation constructor. |
59
|
|
|
* |
60
|
|
|
* @param string $id |
61
|
|
|
* @param string $path |
62
|
|
|
* @param string $method |
63
|
|
|
* @param Parameter[] $parameters |
64
|
|
|
* @param Schema $requestSchema |
65
|
|
|
* @param Response[] $responses |
66
|
|
|
* @param array $extensions |
67
|
|
|
*/ |
68
|
|
|
public function __construct( |
69
|
|
|
string $id, |
70
|
|
|
string $path, |
71
|
|
|
string $method, |
72
|
|
|
array $parameters = [], |
73
|
|
|
Schema $requestSchema = null, |
74
|
|
|
array $responses = [], |
75
|
|
|
array $extensions = [] |
76
|
|
|
) { |
77
|
|
|
$this->id = $id; |
78
|
|
|
$this->path = $path; |
79
|
|
|
$this->method = $method; |
80
|
|
|
$this->parameters = $parameters; |
81
|
|
|
$this->requestSchema = $requestSchema; |
82
|
|
|
$this->responses = $responses; |
83
|
|
|
$this->extensions = $extensions; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $name |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
public function getExtension(string $name) |
92
|
|
|
{ |
93
|
|
|
return isset($this->extensions[$name]) ? $this->extensions[$name] : null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getPath(): string |
100
|
|
|
{ |
101
|
|
|
return $this->path; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getId(): string |
108
|
|
|
{ |
109
|
|
|
return $this->id; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return int[] |
114
|
|
|
*/ |
115
|
|
|
public function getStatusCodes(): array |
116
|
|
|
{ |
117
|
|
|
return array_keys($this->responses); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param int $code |
122
|
|
|
* |
123
|
|
|
* @return Response |
124
|
|
|
*/ |
125
|
|
|
public function getResponse(int $code): Response |
126
|
|
|
{ |
127
|
|
|
if (!isset($this->responses[$code])) { |
128
|
|
|
if (isset($this->responses[0])) { |
129
|
|
|
// Return default response |
130
|
|
|
return $this->responses[0]; |
131
|
|
|
} |
132
|
|
|
throw new \InvalidArgumentException( |
133
|
|
|
"Operation '{$this->getId()}' does not have a definition for status '$code'" . |
134
|
|
|
" (has " . implode(', ', array_keys($this->responses)) . ')' |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->responses[$code]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return Response[] |
143
|
|
|
*/ |
144
|
|
|
public function getResponses() |
145
|
|
|
{ |
146
|
|
|
return array_values($this->responses); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return Schema |
151
|
|
|
*/ |
152
|
|
|
public function getRequestSchema(): Schema |
153
|
|
|
{ |
154
|
|
|
return $this->requestSchema; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
public function hasParameters(): bool |
161
|
|
|
{ |
162
|
|
|
return (bool)count($this->parameters); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return Parameter[] |
167
|
|
|
*/ |
168
|
|
|
public function getParameters(): array |
169
|
|
|
{ |
170
|
|
|
return $this->hasParameters() ? $this->parameters : []; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $name |
175
|
|
|
* |
176
|
|
|
* @return Parameter |
177
|
|
|
*/ |
178
|
|
|
public function getParameter(string $name): Parameter |
179
|
|
|
{ |
180
|
|
|
foreach ($this->getParameters() as $parameter) { |
181
|
|
|
if ($parameter->getName() === $name) { |
182
|
|
|
return $parameter; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
throw new \OutOfBoundsException("Parameter '$name' does not exist"); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function getMethod(): string |
192
|
|
|
{ |
193
|
|
|
return $this->method; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|