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
|
|
View Code Duplication |
if (!isset($this->paths[$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
|
|
|
* @param string $method |
133
|
|
|
* |
134
|
|
|
* @return Schema |
135
|
|
|
*/ |
136
|
|
|
public function getRequestSchema(string $path, string $method): Schema |
137
|
|
|
{ |
138
|
|
|
return $this->getPath($path)->getOperation($method)->getRequestSchema(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $path |
143
|
|
|
* @param string $method |
144
|
|
|
* |
145
|
|
|
* @return Parameter|null |
146
|
|
|
*/ |
147
|
|
|
public function getRequestBodyParameter(string $path, string $method) |
148
|
|
|
{ |
149
|
|
|
foreach ($this->getPath($path)->getOperation($method)->getParameters() as $parameter) { |
150
|
|
|
if ($parameter->getIn() === Parameter::IN_BODY) { |
151
|
|
|
return $parameter; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $path |
158
|
|
|
* @param string $method |
159
|
|
|
* |
160
|
|
|
* @param int $code |
161
|
|
|
* |
162
|
|
|
* @return Schema |
163
|
|
|
*/ |
164
|
|
|
public function getResponseSchema(string $path, string $method, int $code): Schema |
165
|
|
|
{ |
166
|
|
|
return $this->getPath($path)->getOperation($method)->getResponse($code)->getSchema(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return Path[] |
171
|
|
|
*/ |
172
|
|
|
public function getPaths(): array |
173
|
|
|
{ |
174
|
|
|
return array_values($this->paths); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return Document |
179
|
|
|
*/ |
180
|
|
|
public function getDocument(): Document |
181
|
|
|
{ |
182
|
|
|
return $this->document; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.