These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Copyright (c) 2015 Petr Olišar (http://olisar.eu) |
||
4 | * |
||
5 | * For the full copyright and license information, please view |
||
6 | * the file LICENSE.md that was distributed with this source code. |
||
7 | */ |
||
8 | |||
9 | namespace Oli\GoogleAPI; |
||
10 | |||
11 | use Nette\Application\UI\Control; |
||
12 | use Nette\Application\Responses\JsonResponse; |
||
13 | use Nette\Application\UI\ITemplate; |
||
14 | use Nette\Utils\Html; |
||
15 | use Nette\Utils\Json; |
||
16 | |||
17 | |||
18 | /** |
||
19 | * Description of GoogleAPI |
||
20 | * |
||
21 | * @author Petr Olišar <[email protected]> |
||
22 | * @property-read ITemplate $template |
||
23 | */ |
||
24 | class MapAPI extends Control |
||
25 | 1 | { |
|
26 | |||
27 | const ROADMAP = 'ROADMAP', SATELLITE = 'SATELLITE', HYBRID = 'HYBRID', TERRAIN = 'TERRAIN', |
||
28 | BICYCLING = 'BICYCLING', DRIVING = 'DRIVING', TRANSIT = 'TRANSIT', WALKING = 'WALKING'; |
||
29 | |||
30 | /** |
||
31 | * @var double|string |
||
32 | */ |
||
33 | private $width; |
||
34 | |||
35 | /** |
||
36 | * @var double|string |
||
37 | */ |
||
38 | private $height; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $coordinates; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $zoom; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $type; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $staticMap = FALSE; |
||
59 | |||
60 | /** |
||
61 | * @var bool|string|callable |
||
62 | */ |
||
63 | private $clickable = FALSE; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | private $key; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | private $markers = array(); |
||
74 | |||
75 | /** |
||
76 | * @var bool |
||
77 | */ |
||
78 | private $bound; |
||
79 | |||
80 | /** |
||
81 | * @var bool |
||
82 | */ |
||
83 | private $markerClusterer; |
||
84 | |||
85 | /** |
||
86 | * @var array |
||
87 | */ |
||
88 | private $clusterOptions; |
||
89 | |||
90 | /** |
||
91 | * @var bool |
||
92 | */ |
||
93 | private $scrollable = FALSE; |
||
94 | |||
95 | /** |
||
96 | * @var array |
||
97 | */ |
||
98 | private $waypoints; |
||
99 | |||
100 | /** |
||
101 | * @var array |
||
102 | */ |
||
103 | private $direction = ['travelmode' => 'DRIVING']; |
||
104 | |||
105 | |||
106 | public function __construct() |
||
107 | { |
||
108 | |||
109 | 1 | } |
|
110 | |||
111 | |||
112 | /** |
||
113 | * @param array $config |
||
114 | * @internal |
||
115 | */ |
||
116 | public function setup(array $config) |
||
117 | { |
||
118 | 1 | $this->width = $config['width']; |
|
119 | 1 | $this->height = $config['height']; |
|
120 | 1 | } |
|
121 | |||
122 | |||
123 | /** |
||
124 | * @param array $coordinates (latitude, longitude) - center of the map |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function setCoordinates(array $coordinates) |
||
128 | { |
||
129 | 1 | if(!count($coordinates)) |
|
130 | 1 | { |
|
131 | 1 | $this->coordinates = array(NULL, NULL); |
|
132 | 1 | } else |
|
133 | { |
||
134 | 1 | $this->coordinates = array_values($coordinates); |
|
135 | } |
||
136 | |||
137 | 1 | return $this; |
|
138 | } |
||
139 | |||
140 | |||
141 | /** |
||
142 | * @param double|string $width |
||
143 | * @param double|string $height |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function setProportions($width, $height) |
||
147 | { |
||
148 | 1 | $this->width = $width; |
|
149 | 1 | $this->height = $height; |
|
150 | 1 | return $this; |
|
151 | } |
||
152 | |||
153 | |||
154 | /** |
||
155 | * @param string $key |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function setKey($key) |
||
159 | { |
||
160 | 1 | $this->key = $key; |
|
161 | 1 | return $this; |
|
162 | } |
||
163 | |||
164 | |||
165 | /** |
||
166 | * @param int $zoom <0, 19> |
||
167 | * @return $this |
||
168 | * @throws InvalidArgumentException |
||
169 | * @throws LogicException |
||
170 | */ |
||
171 | public function setZoom($zoom) |
||
172 | { |
||
173 | 1 | if (!is_int($zoom)) |
|
174 | 1 | { |
|
175 | 1 | throw new InvalidArgumentException("type must be integer, $zoom (".gettype($zoom).") was given"); |
|
176 | } |
||
177 | |||
178 | 1 | if ($zoom < 0 || $zoom > 19) |
|
179 | 1 | { |
|
180 | 1 | throw new LogicException('Zoom must be betwen <0, 19>.'); |
|
181 | } |
||
182 | |||
183 | 1 | $this->zoom = (int) $zoom; |
|
184 | 1 | return $this; |
|
185 | } |
||
186 | |||
187 | |||
188 | /** |
||
189 | * @param string $type |
||
190 | * @return $this |
||
191 | * @throws InvalidArgumentException |
||
192 | */ |
||
193 | public function setType($type) |
||
194 | { |
||
195 | 1 | if($type !== self::HYBRID && $type !== self::ROADMAP && $type !== self::SATELLITE && |
|
196 | 1 | $type !== self::TERRAIN) |
|
197 | 1 | { |
|
198 | 1 | throw new InvalidArgumentException; |
|
199 | } |
||
200 | 1 | $this->type = $type; |
|
201 | 1 | return $this; |
|
202 | } |
||
203 | |||
204 | |||
205 | /** |
||
206 | * @param string $key |
||
207 | * @param array $waypoint |
||
208 | * @return $this |
||
209 | * @throws InvalidArgumentException |
||
210 | */ |
||
211 | public function setWaypoint($key, array $waypoint) |
||
212 | { |
||
213 | 1 | if (!in_array($key, ['start', 'end', 'waypoint'])) |
|
214 | 1 | { |
|
215 | 1 | throw new InvalidArgumentException('First argument must be "start|end|waypoint", ' . $key . ' was given'); |
|
216 | } |
||
217 | |||
218 | 1 | if($key === 'waypoint') |
|
219 | 1 | { |
|
220 | 1 | $this->waypoints['waypoints'][] = $waypoint; |
|
221 | |||
222 | 1 | } else |
|
223 | { |
||
224 | 1 | $this->waypoints[$key] = $waypoint; |
|
225 | } |
||
226 | 1 | return $this; |
|
227 | } |
||
228 | |||
229 | |||
230 | /** |
||
231 | * @param array $direction |
||
232 | * @return $this |
||
233 | * @throws InvalidArgumentException |
||
234 | */ |
||
235 | public function setDirection(array $direction) |
||
236 | { |
||
237 | 1 | $this->direction = $direction; |
|
238 | 1 | if(!array_key_exists('travelmode', $this->direction)) |
|
239 | 1 | { |
|
240 | 1 | $this->direction['travelmode'] = 'DRIVING'; |
|
241 | 1 | } else if (!in_array($direction['travelmode'], [ |
|
242 | 1 | self::BICYCLING, self::DRIVING, self::WALKING, self::TRANSIT |
|
243 | 1 | ])) |
|
244 | 1 | { |
|
245 | 1 | throw new InvalidArgumentException; |
|
246 | } |
||
247 | 1 | return $this; |
|
248 | } |
||
249 | |||
250 | |||
251 | /** |
||
252 | * @return array Width and Height of the map. |
||
253 | */ |
||
254 | public function getProportions() |
||
255 | { |
||
256 | 1 | return array('width' => $this->width, 'height' => $this->height); |
|
257 | } |
||
258 | |||
259 | |||
260 | /** |
||
261 | * @return array Center of the map |
||
262 | */ |
||
263 | public function getCoordinates() |
||
264 | { |
||
265 | 1 | return $this->coordinates; |
|
266 | } |
||
267 | |||
268 | |||
269 | /** |
||
270 | * @return int |
||
271 | */ |
||
272 | public function getZoom() |
||
273 | { |
||
274 | 1 | return $this->zoom; |
|
275 | } |
||
276 | |||
277 | |||
278 | /** |
||
279 | * @return string Which map type will be show |
||
280 | */ |
||
281 | public function getType() |
||
282 | { |
||
283 | 1 | return $this->type; |
|
284 | } |
||
285 | |||
286 | |||
287 | /** |
||
288 | * @return array |
||
289 | */ |
||
290 | public function getWaypoints() |
||
291 | { |
||
292 | 1 | return $this->waypoints; |
|
293 | } |
||
294 | |||
295 | |||
296 | /** |
||
297 | * @return array |
||
298 | */ |
||
299 | public function getDirection() |
||
300 | { |
||
301 | 1 | return $this->direction; |
|
302 | } |
||
303 | |||
304 | |||
305 | /** |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getKey() |
||
309 | { |
||
310 | 1 | return $this->key; |
|
311 | } |
||
312 | |||
313 | |||
314 | /** |
||
315 | * @param bool $staticMap |
||
316 | * @return $this |
||
317 | * @throws InvalidArgumentException |
||
318 | */ |
||
319 | View Code Duplication | public function isStaticMap($staticMap = TRUE) |
|
320 | { |
||
321 | 1 | if (!is_bool($staticMap)) |
|
322 | 1 | { |
|
323 | 1 | throw new InvalidArgumentException("staticMap must be boolean, $staticMap (".gettype($staticMap).") was given"); |
|
324 | } |
||
325 | |||
326 | 1 | $this->staticMap = $staticMap; |
|
327 | 1 | return $this; |
|
328 | } |
||
329 | |||
330 | |||
331 | /** |
||
332 | * @return bool |
||
333 | */ |
||
334 | public function getIsStaticMap() |
||
335 | { |
||
336 | 1 | return $this->staticMap; |
|
337 | } |
||
338 | |||
339 | |||
340 | /** |
||
341 | * @param bool|callable $clickable |
||
342 | * @return $this |
||
343 | * @throws InvalidArgumentException |
||
344 | */ |
||
345 | public function isClickable($clickable = TRUE) |
||
346 | { |
||
347 | 1 | if (!$this->staticMap) |
|
348 | 1 | { |
|
349 | 1 | throw new InvalidArgumentException("the 'clickable' option only applies to static map"); |
|
350 | } |
||
351 | |||
352 | 1 | View Code Duplication | if (!is_bool($clickable) && !is_callable($clickable)) |
353 | 1 | { |
|
354 | 1 | throw new InvalidArgumentException( |
|
355 | 1 | "clickable must be boolean or callable, $clickable (".gettype($clickable).") was given" |
|
356 | 1 | ); |
|
357 | } |
||
358 | |||
359 | 1 | if (is_callable($clickable)) { |
|
360 | 1 | $this->clickable = $clickable; |
|
361 | |||
362 | 1 | } else if ($clickable !== FALSE) |
|
363 | 1 | { |
|
364 | 1 | $this->clickable = '<a href="https://maps.google.com/maps/place/' . |
|
365 | 1 | $this->coordinates[0] . ',' . $this->coordinates[1] . '/">'; |
|
366 | 1 | } |
|
367 | |||
368 | 1 | return $this; |
|
369 | } |
||
370 | |||
371 | |||
372 | /** |
||
373 | * @return bool |
||
374 | */ |
||
375 | public function getIsClicable() |
||
376 | { |
||
377 | 1 | return $this->clickable; |
|
378 | } |
||
379 | |||
380 | |||
381 | /** |
||
382 | * @param bool $scrollable |
||
383 | * @return $this |
||
384 | * @throws InvalidArgumentException |
||
385 | */ |
||
386 | View Code Duplication | public function isScrollable($scrollable = TRUE) |
|
387 | { |
||
388 | 1 | if (!is_bool($scrollable)) |
|
389 | 1 | { |
|
390 | 1 | throw new InvalidArgumentException("staticMap must be boolean, $scrollable (".gettype($scrollable).") was given"); |
|
391 | } |
||
392 | |||
393 | 1 | $this->scrollable = $scrollable; |
|
394 | 1 | return $this; |
|
395 | } |
||
396 | |||
397 | |||
398 | /** |
||
399 | * @return bool |
||
400 | */ |
||
401 | public function getIsScrollable() |
||
402 | { |
||
403 | 1 | return $this->scrollable; |
|
404 | } |
||
405 | |||
406 | |||
407 | /** |
||
408 | * @param Markers $markers |
||
409 | * @return $this |
||
410 | */ |
||
411 | public function addMarkers(Markers $markers) |
||
412 | { |
||
413 | $this->markers = $markers->getMarkers(); |
||
414 | $this->bound = $markers->getBound(); |
||
415 | $this->markerClusterer = $markers->getMarkerClusterer(); |
||
416 | $this->clusterOptions = $markers->getClusterOptions(); |
||
417 | return $this; |
||
418 | } |
||
419 | |||
420 | |||
421 | /** |
||
422 | * Alias to handleMarkers() |
||
423 | */ |
||
424 | public function invalidateMarkers() |
||
425 | { |
||
426 | $this->handleMarkers(); |
||
427 | } |
||
428 | |||
429 | |||
430 | /** |
||
431 | * @throws \Nette\Utils\JsonException |
||
432 | */ |
||
433 | public function render() |
||
434 | { |
||
435 | if ($this->staticMap) |
||
436 | { |
||
437 | if (is_callable($this->clickable)) |
||
438 | { |
||
439 | $this->clickable = call_user_func_array($this->clickable, [ |
||
440 | 'https://maps.google.com/maps/place/' . $this->coordinates[0] . ',' . |
||
441 | $this->coordinates[1] . '/', |
||
442 | $this->getCoordinates() |
||
443 | ]); |
||
444 | } |
||
445 | |||
446 | $this->template->height = $this->height; |
||
0 ignored issues
–
show
|
|||
447 | $this->template->width = $this->width; |
||
0 ignored issues
–
show
Accessing
width on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
448 | $this->template->zoom = $this->zoom; |
||
0 ignored issues
–
show
Accessing
zoom on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
449 | $this->template->position = $this->coordinates; |
||
0 ignored issues
–
show
Accessing
position on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
450 | $this->template->markers = $this->markers; |
||
0 ignored issues
–
show
Accessing
markers on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
451 | $this->template->clickable = $this->clickable instanceof Html ? $this->clickable->startTag() : |
||
0 ignored issues
–
show
Accessing
clickable on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
452 | $this->clickable; |
||
453 | $this->template->setFile(dirname(__FILE__) . '/static.latte'); |
||
454 | } else |
||
455 | { |
||
456 | $map = array( |
||
457 | 'position' => $this->coordinates, |
||
458 | 'height' => $this->height, |
||
459 | 'width' => $this->width, |
||
460 | 'zoom' => $this->zoom, |
||
461 | 'type' => $this->type, |
||
462 | 'scrollable' => $this->scrollable, |
||
463 | 'key' => $this->key, |
||
464 | 'bound' => $this->bound, |
||
465 | 'cluster' => $this->markerClusterer, |
||
466 | 'clusterOptions' => $this->clusterOptions, |
||
467 | 'waypoint' => !is_null($this->waypoints) ? array_merge($this->waypoints, $this->direction) : NULL |
||
468 | ); |
||
469 | $this->template->map = Json::encode($map); |
||
0 ignored issues
–
show
Accessing
map on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
470 | $this->template->setFile(dirname(__FILE__) . '/template.latte'); |
||
471 | } |
||
472 | $this->template->render(); |
||
473 | } |
||
474 | |||
475 | |||
476 | /** |
||
477 | * Send markers to template as JSON |
||
478 | * @internal |
||
479 | */ |
||
480 | public function handleMarkers() |
||
481 | { |
||
482 | $this->getPresenter()->sendResponse(new JsonResponse($this->markers)); |
||
483 | } |
||
484 | } |
||
485 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: