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\Document\Document; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema; |
13
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\ClosureVisitorScope; |
14
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\Visitee; |
15
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\VisiteeMixin; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author John Kleijn <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Description implements Visitee, ClosureVisitorScope |
21
|
|
|
{ |
22
|
|
|
use VisiteeMixin; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Path[] |
26
|
|
|
*/ |
27
|
|
|
protected $paths; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ComplexType[] |
31
|
|
|
*/ |
32
|
|
|
protected $complexTypes; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $host; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $schemes = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $extensions = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Document |
51
|
|
|
*/ |
52
|
|
|
protected $document; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Description constructor. |
56
|
|
|
* |
57
|
|
|
* @param Path[] $paths |
58
|
|
|
* @param ComplexType[] $complexTypes |
59
|
|
|
* @param string $host |
60
|
|
|
* @param array $schemes |
61
|
|
|
* @param array $extensions |
62
|
|
|
* @param Document $document |
63
|
|
|
*/ |
64
|
|
|
public function __construct( |
65
|
|
|
array $paths, |
66
|
|
|
array $complexTypes, |
67
|
|
|
$host, |
68
|
|
|
array $schemes, |
69
|
|
|
array $extensions, |
70
|
|
|
Document $document |
71
|
|
|
) { |
72
|
|
|
$this->paths = $paths; |
73
|
|
|
$this->complexTypes = $complexTypes; |
74
|
|
|
$this->host = $host; |
75
|
|
|
$this->schemes = $schemes; |
76
|
|
|
$this->document = $document; |
77
|
|
|
$this->extensions = $extensions; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $name |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function getExtension(string $name) |
86
|
|
|
{ |
87
|
|
|
return isset($this->extensions[$name]) ? $this->extensions[$name] : null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return ComplexType[] |
92
|
|
|
*/ |
93
|
|
|
public function getComplexTypes(): array |
94
|
|
|
{ |
95
|
|
|
return $this->complexTypes; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function getSchemes(): array |
102
|
|
|
{ |
103
|
|
|
return $this->schemes; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string|null |
108
|
|
|
*/ |
109
|
|
|
public function getHost() |
110
|
|
|
{ |
111
|
|
|
return $this->host; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $path |
116
|
|
|
* |
117
|
|
|
* @return Path |
118
|
|
|
*/ |
119
|
|
|
public function getPath(string $path): Path |
120
|
|
|
{ |
121
|
|
|
if (!$this->hasPath($path)) { |
122
|
|
|
throw new \InvalidArgumentException( |
123
|
|
|
"Path '$path' does not exist (have ".implode(', ', array_keys($this->paths)).')' |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->paths[$path]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $path |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function hasPath(string $path): bool |
135
|
|
|
{ |
136
|
|
|
return isset($this->paths[$path]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $path |
141
|
|
|
* @param string $method |
142
|
|
|
* |
143
|
|
|
* @return Schema |
144
|
|
|
*/ |
145
|
|
|
public function getRequestSchema(string $path, string $method): Schema |
146
|
|
|
{ |
147
|
|
|
return $this->getPath($path)->getOperation($method)->getRequestSchema(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $path |
152
|
|
|
* @param string $method |
153
|
|
|
* |
154
|
|
|
* @return Parameter|null |
155
|
|
|
*/ |
156
|
|
|
public function getRequestBodyParameter(string $path, string $method) |
157
|
|
|
{ |
158
|
|
|
foreach ($this->getPath($path)->getOperation($method)->getParameters() as $parameter) { |
159
|
|
|
if ($parameter->getIn() === Parameter::IN_BODY) { |
160
|
|
|
return $parameter; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $path |
167
|
|
|
* @param string $method |
168
|
|
|
* |
169
|
|
|
* @param int $code |
170
|
|
|
* |
171
|
|
|
* @return Schema |
172
|
|
|
*/ |
173
|
|
|
public function getResponseSchema(string $path, string $method, int $code): Schema |
174
|
|
|
{ |
175
|
|
|
return $this->getPath($path)->getOperation($method)->getResponse($code)->getSchema(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return Path[] |
180
|
|
|
*/ |
181
|
|
|
public function getPaths(): array |
182
|
|
|
{ |
183
|
|
|
return array_values($this->paths); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return Document |
188
|
|
|
*/ |
189
|
|
|
public function getDocument(): Document |
190
|
|
|
{ |
191
|
|
|
return $this->document; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|