1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace gossi\swagger; |
4
|
|
|
|
5
|
|
|
use gossi\swagger\parts\ConsumesPart; |
6
|
|
|
use gossi\swagger\parts\ExtensionPart; |
7
|
|
|
use gossi\swagger\parts\ExternalDocsPart; |
8
|
|
|
use gossi\swagger\parts\ParametersPart; |
9
|
|
|
use gossi\swagger\parts\ProducesPart; |
10
|
|
|
use gossi\swagger\parts\ResponsesPart; |
11
|
|
|
use gossi\swagger\parts\SchemesPart; |
12
|
|
|
use gossi\swagger\parts\TagsPart; |
13
|
|
|
use phootwork\collection\CollectionUtils; |
14
|
|
|
use phootwork\collection\Map; |
15
|
|
|
use phootwork\file\exception\FileNotFoundException; |
16
|
|
|
use phootwork\file\File; |
17
|
|
|
use phootwork\json\Json; |
18
|
|
|
use phootwork\lang\Arrayable; |
19
|
|
|
use gossi\swagger\collections\Paths; |
20
|
|
|
use gossi\swagger\collections\Definitions; |
21
|
|
|
|
22
|
|
|
class Swagger extends AbstractModel implements Arrayable |
23
|
|
|
{ |
24
|
|
|
use SchemesPart; |
25
|
|
|
use ConsumesPart; |
26
|
|
|
use ProducesPart; |
27
|
|
|
use TagsPart; |
28
|
|
|
use ParametersPart; |
29
|
|
|
use ResponsesPart; |
30
|
|
|
use ExternalDocsPart; |
31
|
|
|
use ExtensionPart; |
32
|
|
|
|
33
|
|
|
const T_INTEGER = 'integer'; |
34
|
|
|
const T_NUMBER = 'number'; |
35
|
|
|
const T_BOOLEAN = 'boolean'; |
36
|
|
|
const T_STRING = 'string'; |
37
|
|
|
const T_FILE = 'file'; |
38
|
|
|
|
39
|
|
|
const F_INT32 = 'int32'; |
40
|
|
|
const F_INT64 = 'int64'; |
41
|
|
|
const F_FLOAT = 'float'; |
42
|
|
|
const F_DOUBLE = 'double'; |
43
|
|
|
const F_STRING = 'string'; |
44
|
|
|
const F_BYTE = 'byte'; |
45
|
|
|
const F_BINARY = 'binary'; |
46
|
|
|
const F_DATE = 'date'; |
47
|
|
|
const F_DATETIME = 'date-time'; |
48
|
|
|
const F_PASSWORD = 'password'; |
49
|
|
|
|
50
|
|
|
public static $METHODS = ['get', 'post', 'put', 'patch', 'delete', 'options', 'head']; |
51
|
|
|
|
52
|
|
|
/** @var string */ |
53
|
|
|
private $swagger = '2.0'; |
54
|
|
|
|
55
|
|
|
/** @var Info */ |
56
|
|
|
private $info; |
57
|
|
|
|
58
|
|
|
/** @var string */ |
59
|
|
|
private $host; |
60
|
|
|
|
61
|
|
|
/** @var string */ |
62
|
|
|
private $basePath; |
63
|
|
|
|
64
|
|
|
/** @var Paths */ |
65
|
|
|
private $paths; |
66
|
|
|
|
67
|
|
|
/** @var Definitions */ |
68
|
|
|
private $definitions; |
69
|
|
|
|
70
|
|
|
/** @var Map */ |
71
|
|
|
private $securityDefinitions; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $filename |
75
|
|
|
* |
76
|
|
|
* @throws FileNotFoundException |
77
|
|
|
* @throws JsonException |
78
|
|
|
* |
79
|
|
|
* @return static |
80
|
|
|
*/ |
81
|
6 |
|
public static function fromFile($filename) |
82
|
|
|
{ |
83
|
6 |
|
$file = new File($filename); |
84
|
|
|
|
85
|
6 |
|
if (!$file->exists()) { |
86
|
|
|
throw new FileNotFoundException(sprintf('File not found at: %s', $filename)); |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
$json = Json::decode($file->read()); |
90
|
|
|
|
91
|
6 |
|
return new static($json); |
92
|
|
|
} |
93
|
|
|
|
94
|
10 |
|
public function __construct($contents = []) |
95
|
|
|
{ |
96
|
10 |
|
$this->parse($contents); |
97
|
10 |
|
} |
98
|
|
|
|
99
|
10 |
|
private function parse($contents) |
100
|
|
|
{ |
101
|
10 |
|
$data = CollectionUtils::toMap($contents); |
102
|
|
|
|
103
|
10 |
|
$this->swagger = $data->get('version', $this->swagger); |
104
|
10 |
|
$this->host = $data->get('host'); |
105
|
10 |
|
$this->basePath = $data->get('basePath'); |
106
|
10 |
|
$this->info = new Info($data->get('info', [])); |
107
|
10 |
|
$this->paths = new Paths($data->get('paths')); |
108
|
10 |
|
$this->definitions = new Definitions($data->get('definitions', new Map())); |
109
|
|
|
|
110
|
|
|
// security schemes |
111
|
10 |
|
$this->securityDefinitions = $data->get('securityDefinitions', new Map()); |
112
|
10 |
|
foreach ($this->securityDefinitions as $s => $def) { |
113
|
|
|
$this->securityDefinitions->set($s, new SecurityScheme($s, $def)); |
114
|
10 |
|
} |
115
|
|
|
|
116
|
|
|
// parts |
117
|
10 |
|
$this->parseSchemes($data); |
118
|
10 |
|
$this->parseConsumes($data); |
119
|
10 |
|
$this->parseProduces($data); |
120
|
10 |
|
$this->parseTags($data); |
121
|
10 |
|
$this->parseParameters($data); |
122
|
10 |
|
$this->parseResponses($data); |
123
|
10 |
|
$this->parseExternalDocs($data); |
124
|
10 |
|
$this->parseExtensions($data); |
125
|
10 |
|
} |
126
|
|
|
|
127
|
7 |
|
public function toArray() |
128
|
|
|
{ |
129
|
7 |
|
return $this->export('swagger', 'info', 'host', 'basePath', 'schemes', 'consumes', 'produces', |
130
|
7 |
|
'paths', 'definitions', 'parameters', 'responses', 'tags', 'externalDocs' |
131
|
7 |
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
2 |
|
public function getVersion() |
138
|
|
|
{ |
139
|
2 |
|
return $this->swagger; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $version |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
1 |
|
public function setVersion($version) |
148
|
|
|
{ |
149
|
1 |
|
$this->swagger = $version; |
150
|
|
|
|
151
|
1 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return Info |
156
|
|
|
*/ |
157
|
1 |
|
public function getInfo() |
158
|
|
|
{ |
159
|
1 |
|
return $this->info; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
1 |
|
public function getHost() |
166
|
|
|
{ |
167
|
1 |
|
return $this->host; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $host |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
1 |
|
public function setHost($host) |
176
|
|
|
{ |
177
|
1 |
|
$this->host = $host; |
178
|
|
|
|
179
|
1 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
1 |
|
public function getBasePath() |
186
|
|
|
{ |
187
|
1 |
|
return $this->basePath; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $basePath |
192
|
|
|
* |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
1 |
|
public function setBasePath($basePath) |
196
|
|
|
{ |
197
|
1 |
|
$this->basePath = $basePath; |
198
|
|
|
|
199
|
1 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return Paths |
204
|
|
|
*/ |
205
|
2 |
|
public function getPaths() |
206
|
|
|
{ |
207
|
2 |
|
return $this->paths; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return Map |
212
|
|
|
*/ |
213
|
1 |
|
public function getDefinitions() |
214
|
|
|
{ |
215
|
1 |
|
return $this->definitions; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return Map |
220
|
|
|
*/ |
221
|
|
|
public function getSecurityDefinitions() |
222
|
|
|
{ |
223
|
|
|
return $this->securityDefinitions; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// /** |
227
|
|
|
// * |
228
|
|
|
// * @param Map $securityDefinitions |
229
|
|
|
// */ |
230
|
|
|
// public function setSecurityDefinitions(Map $securityDefinitions) { |
231
|
|
|
// $this->securityDefinitions = $securityDefinitions; |
232
|
|
|
// return $this; |
233
|
|
|
// } |
234
|
|
|
} |
235
|
|
|
|