Swagger::setBasePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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