Completed
Push — master ( d240eb...82cfa8 )
by Pavel
02:18
created

ApiRouteSpec::getDisable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2016 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\ApiRouter;
10
11
use Nette;
12
use Ublaboo\ApiRouter\Exception\ApiRouteWrongPropertyException;
13
14
abstract class ApiRouteSpec extends Nette\Object
15
{
16
17
	/**
18
	 * @var string
19
	 */
20
	protected $description;
21
22
	/**
23
	 * @var string
24
	 */
25
	protected $path = '/';
26
27
	/**
28
	 * @Enum({"CREATE", "READ", "UPDATE", "DELETE", "OPTIONS"})
29
	 * @var string
30
	 */
31
	protected $method;
32
33
	/**
34
	 * @var array
35
	 */
36
	protected $parameters = [];
37
38
	/**
39
	 * @var array
40
	 */
41
	protected $parameters_infos = ['requirement', 'type', 'description', 'default'];
42
43
	/**
44
	 * @var int
45
	 */
46
	protected $priority = 0;
47
48
	/**
49
	 * @Enum({"json", "xml"})
50
	 * @var string
51
	 */
52
	protected $format;
53
54
	/**
55
	 * @var mixed
56
	 */
57
	protected $example;
58
59
	/**
60
	 * @var string
61
	 */
62
	protected $section;
63
64
	/**
65
	 * @var array
66
	 */
67
	protected $tags = [];
68
69
	/**
70
	 * @var array
71
	 */
72
	protected $response_codes = [];
73
74
	/**
75
	 * @Enum({TRUE, FALSE})
76
	 * @var bool
77
	 */
78
	protected $disable = FALSE;
79
80
81
	/**
82
	 * @param array $data
83
	 */
84
	public function __construct(array $data)
85
	{
86
		foreach ($data as $key => $value) {
87
			$method = 'set' . str_replace('_', '', ucwords($key, '_'));
88
89
			if (!method_exists($this, $method)) {
90
				throw new ApiRouteWrongPropertyException(
91
					sprintf('Unknown property "%s" on annotation "%s"', $key, get_class($this))
92
				);
93
			}
94
95
			$this->$method($value);
96
		}
97
	}
98
99
100
	/**
101
	 * @param string $description
102
	 * @return void
103
	 */
104
	public function setDescription($description)
105
	{
106
		$this->description = $description;
107
	}
108
109
110
	/**
111
	 * @return string
112
	 */
113
	public function getDescription()
114
	{
115
		return $this->description;
116
	}
117
118
119
	/**
120
	 * @param string $path
121
	 * @return void
122
	 */
123
	protected function setPath($path)
124
	{
125
		if (!$path) {
126
			throw new ApiRouteWrongPropertyException('ApiRoute path can not be empty');
127
		}
128
129
		$this->path = (string) $path;
130
	}
131
132
133
	/**
134
	 * @return string
135
	 */
136
	public function getPath()
137
	{
138
		return $this->path;
139
	}
140
141
142
	/**
143
	 * @param string $method
144
	 * @return void
145
	 */
146
	protected function setMethod($method)
147
	{
148
		$this->method = strtoupper($method);
149
	}
150
151
152
	/**
153
	 * @return string
154
	 */
155
	public function getMethod()
156
	{
157
		return $this->method;
158
	}
159
160
161
	/**
162
	 * @param array $parameters
163
	 */
164
	protected function setParameters(array $parameters)
165
	{
166
		foreach ($parameters as $key => $info) {
167
			if (FALSE === strpos($this->getPath(), "<{$key}>")) {
168
				throw new ApiRouteWrongPropertyException("Parameter <$key> is not present in the url mask");
169
			}
170
171
			foreach ($info as $info_key => $value) {
172
				if (!in_array($info_key, $this->parameters_infos)) {
173
					throw new ApiRouteWrongPropertyException(sprintf(
174
						"You cat set only these description informations: [%s] - \"%s\" given",
175
						implode(', ', $this->parameters_infos),
176
						$info_key
177
					));
178
				}
179
180
				if (!is_scalar($value) && !is_null($value)) {
181
					throw new ApiRouteWrongPropertyException(
182
						"You cat set only scalar parameters informations (key [{$info_key}])"
183
					);
184
				}
185
			}
186
		}
187
188
		$this->parameters = $parameters;
189
	}
190
191
192
	/**
193
	 * @return array
194
	 */
195
	public function getParameters()
196
	{
197
		return $this->parameters;
198
	}
199
200
201
	/**
202
	 * @param int $priority
203
	 * @return void
204
	 */
205
	public function setPriority($priority)
206
	{
207
		$this->priority = $priority;
208
	}
209
210
211
	/**
212
	 * @return int
213
	 */
214
	public function getPriority()
215
	{
216
		return $this->priority;
217
	}
218
219
220
	/**
221
	 * @param string $format
222
	 * @return void
223
	 */
224
	public function setFormat($format)
225
	{
226
		$this->format = $format;
227
	}
228
229
230
	/**
231
	 * @return string
232
	 */
233
	public function getFormat()
234
	{
235
		return $this->format;
236
	}
237
238
239
	/**
240
	 * @param mixed $example
241
	 * @return void
242
	 */
243
	public function setExample($example)
244
	{
245
		$this->example = $example;
246
	}
247
248
249
	/**
250
	 * @return mixed
251
	 */
252
	public function getExample()
253
	{
254
		return $this->example;
255
	}
256
257
258
	/**
259
	 * @param string $section
260
	 * @return void
261
	 */
262
	public function setSection($section)
263
	{
264
		$this->section = $section;
265
	}
266
267
268
	/**
269
	 * @return string
270
	 */
271
	public function getSection()
272
	{
273
		return $this->section;
274
	}
275
276
277
	/**
278
	 * @param array $tags
279
	 * @return void
280
	 */
281
	public function setTags(array $tags)
282
	{
283
		$this->tags = $tags;
284
	}
285
286
287
	/**
288
	 * @return array
289
	 */
290
	public function getTags()
291
	{
292
		$return = [];
293
294
		/**
295
		 * Tag may be saves aither with color: [tagName => color] or without: [tagName]
296
		 */
297
		foreach ($this->tags as $tag => $color) {
298
			if (is_numeric($tag)) {
299
				$return[$color] = '#9b59b6';
300
			} else {
301
				$return[$tag] = $color;
302
			}
303
		}
304
305
		return $return;
306
	}
307
308
309
	/**
310
	 * @param array $response_codes
311
	 * @return void
312
	 */
313
	public function setResponseCodes(array $response_codes)
314
	{
315
		$this->response_codes = $response_codes;
316
	}
317
318
319
	/**
320
	 * @return array
321
	 */
322
	public function getResponseCodes()
323
	{
324
		return $this->response_codes;
325
	}
326
327
328
	/**
329
	 * @param bool $disable
330
	 */
331
	public function setDisable($disable)
332
	{
333
		$this->disable = (bool) $disable;
334
	}
335
336
337
	/**
338
	 * @return bool
339
	 */
340
	public function getDisable()
341
	{
342
		return $this->disable;
343
	}
344
345
}
346
347