Completed
Push — master ( 10a146...62fa38 )
by Thomas
8s
created

Swagger::parse()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 2.0004

Importance

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