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