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"}) |
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
|
|
|
/** |
76
|
|
|
* @param array $data |
77
|
|
|
*/ |
78
|
|
|
public function __construct(array $data) |
79
|
|
|
{ |
80
|
|
|
foreach ($data as $key => $value) { |
81
|
|
|
$method = 'set' . str_replace('_', '', ucwords($key, '_')); |
82
|
|
|
|
83
|
|
|
if (!method_exists($this, $method)) { |
84
|
|
|
throw new ApiRouteWrongPropertyException( |
85
|
|
|
sprintf('Unknown property "%s" on annotation "%s"', $key, get_class($this)) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->$method($value); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $description |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function setDescription($description) |
99
|
|
|
{ |
100
|
|
|
$this->description = $description; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getDescription() |
108
|
|
|
{ |
109
|
|
|
return $this->description; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $path |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function setPath($path) |
118
|
|
|
{ |
119
|
|
|
if (!$path) { |
120
|
|
|
throw new ApiRouteWrongPropertyException('ApiRoute path can not be empty'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->path = (string) $path; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getPath() |
131
|
|
|
{ |
132
|
|
|
return $this->path; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $method |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
protected function setMethod($method) |
141
|
|
|
{ |
142
|
|
|
$this->method = strtoupper($method); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getMethod() |
150
|
|
|
{ |
151
|
|
|
return $this->method; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $parameters |
157
|
|
|
*/ |
158
|
|
|
protected function setParameters(array $parameters) |
159
|
|
|
{ |
160
|
|
|
foreach ($parameters as $key => $info) { |
161
|
|
|
if (FALSE === strpos($this->getPath(), "<{$key}>")) { |
162
|
|
|
throw new ApiRouteWrongPropertyException("Parameter <$key> is not present in the url mask"); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
foreach ($info as $info_key => $value) { |
166
|
|
|
if (!in_array($info_key, $this->parameters_infos)) { |
167
|
|
|
throw new ApiRouteWrongPropertyException(sprintf( |
168
|
|
|
"You cat set only these description informations: [%s] - \"%s\" given", |
169
|
|
|
implode(', ', $this->parameters_infos), |
170
|
|
|
$info_key |
171
|
|
|
)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (!is_scalar($value) && !is_null($value)) { |
175
|
|
|
throw new ApiRouteWrongPropertyException( |
176
|
|
|
"You cat set only scalar parameters informations (key [{$info_key}])" |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->parameters = $parameters; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
public function getParameters() |
190
|
|
|
{ |
191
|
|
|
return $this->parameters; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param int $priority |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
public function setPriority($priority) |
200
|
|
|
{ |
201
|
|
|
$this->priority = $priority; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return int |
207
|
|
|
*/ |
208
|
|
|
public function getPriority() |
209
|
|
|
{ |
210
|
|
|
return $this->priority; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $format |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
|
|
public function setFormat($format) |
219
|
|
|
{ |
220
|
|
|
$this->format = $format; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function getFormat() |
228
|
|
|
{ |
229
|
|
|
return $this->format; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param mixed $example |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
public function setExample($example) |
238
|
|
|
{ |
239
|
|
|
$this->example = $example; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return mixed |
245
|
|
|
*/ |
246
|
|
|
public function getExample() |
247
|
|
|
{ |
248
|
|
|
return $this->example; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $section |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
|
|
public function setSection($section) |
257
|
|
|
{ |
258
|
|
|
$this->section = $section; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return string |
264
|
|
|
*/ |
265
|
|
|
public function getSection() |
266
|
|
|
{ |
267
|
|
|
return $this->section; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param array $tags |
273
|
|
|
* @return void |
274
|
|
|
*/ |
275
|
|
|
public function setTags(array $tags) |
276
|
|
|
{ |
277
|
|
|
$this->tags = $tags; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return array |
283
|
|
|
*/ |
284
|
|
|
public function getTags() |
285
|
|
|
{ |
286
|
|
|
$return = []; |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Tag may be saves aither with color: [tagName => color] or without: [tagName] |
290
|
|
|
*/ |
291
|
|
|
foreach ($this->tags as $tag => $color) { |
292
|
|
|
if (is_numeric($tag)) { |
293
|
|
|
$return[$color] = '#9b59b6'; |
294
|
|
|
} else { |
295
|
|
|
$return[$tag] = $color; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $return; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param array $response_codes |
305
|
|
|
* @return void |
306
|
|
|
*/ |
307
|
|
|
public function setResponseCodes(array $response_codes) |
308
|
|
|
{ |
309
|
|
|
$this->response_codes = $response_codes; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return array |
315
|
|
|
*/ |
316
|
|
|
public function getResponseCodes() |
317
|
|
|
{ |
318
|
|
|
return $this->response_codes; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
|