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 | |||
14 | |||
15 | /** |
||
16 | * Description of GoogleAPI |
||
17 | * |
||
18 | * @author Petr Olišar <[email protected]> |
||
19 | */ |
||
20 | class MapAPI extends Control |
||
21 | { |
||
22 | |||
23 | const ROADMAP = 'ROADMAP', SATELLITE = 'SATELLITE', HYBRID = 'HYBRID', TERRAIN = 'TERRAIN', |
||
24 | BICYCLING = 'BICYCLING', DRIVING = 'DRIVING', TRANSIT = 'TRANSIT', WALKING = 'WALKING'; |
||
25 | |||
26 | /** @var String */ |
||
27 | private $width; |
||
28 | |||
29 | /** @var String */ |
||
30 | private $height; |
||
31 | |||
32 | /** @var array */ |
||
33 | private $coordinates; |
||
34 | |||
35 | /** @var Integer */ |
||
36 | private $zoom; |
||
37 | |||
38 | /** @var String */ |
||
39 | private $type; |
||
40 | |||
41 | /** @var Boolean */ |
||
42 | private $staticMap = FALSE; |
||
43 | |||
44 | /** @var Boolean */ |
||
45 | private $clickable = FALSE; |
||
46 | |||
47 | /** @var String */ |
||
48 | private $key; |
||
49 | |||
50 | /** @var array */ |
||
51 | private $markers = array(); |
||
52 | |||
53 | /** @var boolean */ |
||
54 | private $bound; |
||
55 | |||
56 | /** @var boolean */ |
||
57 | private $markerClusterer; |
||
58 | |||
59 | /** @var boolean */ |
||
60 | private $scrollable = FALSE; |
||
61 | |||
62 | /** |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | private $waypoints; |
||
67 | |||
68 | /** |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | private $direction = ['travelmode' => 'DRIVING']; |
||
73 | |||
74 | |||
75 | /** |
||
76 | * @internal |
||
77 | * @param array $config |
||
78 | */ |
||
79 | public function setup(array $config) |
||
80 | { |
||
81 | $this->width = $config['width']; |
||
82 | $this->height = $config['height']; |
||
83 | } |
||
84 | |||
85 | |||
86 | /** |
||
87 | * |
||
88 | * @param array $coordinates (latitude, longitude) - center of the map |
||
89 | * @return \Oli\GoogleAPI\MapAPI |
||
90 | */ |
||
91 | public function setCoordinates(array $coordinates) |
||
92 | { |
||
93 | if(!count($coordinates)) |
||
94 | { |
||
95 | $this->coordinates = array(NULL, NULL); |
||
96 | } else |
||
97 | { |
||
98 | $this->coordinates = array_values($coordinates); |
||
99 | } |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | |||
105 | /** |
||
106 | * @param double $width |
||
107 | * @param double $height |
||
108 | * @return MapAPI |
||
109 | */ |
||
110 | public function setProportions($width, $height) |
||
111 | { |
||
112 | $this->width = $width; |
||
0 ignored issues
–
show
|
|||
113 | $this->height = $height; |
||
0 ignored issues
–
show
The property
$height was declared of type string , but $height is of type double . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | |||
118 | /** |
||
119 | * |
||
120 | * @param string $key |
||
121 | * @return \Oli\GoogleAPI\MapAPI |
||
122 | */ |
||
123 | public function setKey($key) |
||
124 | { |
||
125 | $this->key = $key; |
||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | |||
130 | /** |
||
131 | * |
||
132 | * @param int $zoom <0, 19> |
||
133 | * @return \Oli\GoogleAPI\MapAPI |
||
134 | * @throws \InvalidArgumentException |
||
135 | * @throws \LogicException |
||
136 | */ |
||
137 | public function setZoom($zoom) |
||
138 | { |
||
139 | if (!is_int($zoom)) |
||
140 | { |
||
141 | throw new \InvalidArgumentException("type must be integer, $zoom (".gettype($zoom).") was given"); |
||
142 | } |
||
143 | |||
144 | if ($zoom < 0 || $zoom > 19) |
||
145 | { |
||
146 | throw new \LogicException('Zoom must be betwen <0, 19>.'); |
||
147 | } |
||
148 | |||
149 | $this->zoom = (int) $zoom; |
||
150 | return $this; |
||
151 | } |
||
152 | |||
153 | |||
154 | /** |
||
155 | * |
||
156 | * @param String $type |
||
157 | * @return \Oli\GoogleAPI\MapAPI |
||
158 | */ |
||
159 | public function setType($type) |
||
160 | { |
||
161 | if($type !== self::HYBRID && $type !== self::ROADMAP && $type !== self::SATELLITE && |
||
162 | $type !== self::TERRAIN) |
||
163 | { |
||
164 | throw new \InvalidArgumentException; |
||
165 | } |
||
166 | $this->type = $type; |
||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | |||
171 | public function setWaypoint($key, $waypoint) |
||
172 | { |
||
173 | if($key === 'waypoints') |
||
174 | { |
||
175 | $this->waypoints['waypoints'][] = $waypoint; |
||
176 | |||
177 | } else |
||
178 | { |
||
179 | $this->waypoints[$key] = $waypoint; |
||
180 | } |
||
181 | return $this; |
||
182 | } |
||
183 | |||
184 | |||
185 | public function setDirection(array $direction) |
||
186 | { |
||
187 | $this->direction = $direction; |
||
188 | if(!array_key_exists('travelmode', $this->direction)) |
||
189 | { |
||
190 | $this->direction['travelmode'] = 'DRIVING'; |
||
191 | } |
||
192 | return $this; |
||
193 | } |
||
194 | |||
195 | |||
196 | /** |
||
197 | * @return array Width and Height of the map. |
||
198 | */ |
||
199 | public function getProportions() |
||
200 | { |
||
201 | return array('width' => $this->width, 'height' => $this->height); |
||
202 | } |
||
203 | |||
204 | |||
205 | /** |
||
206 | * @return array Center of the map |
||
207 | */ |
||
208 | public function getCoordinates() |
||
209 | { |
||
210 | return $this->coordinates; |
||
211 | } |
||
212 | |||
213 | |||
214 | /** |
||
215 | * @return integer Zoom |
||
216 | */ |
||
217 | public function getZoom() |
||
218 | { |
||
219 | return $this->zoom; |
||
220 | } |
||
221 | |||
222 | |||
223 | /** |
||
224 | * @return String Which map type will be show |
||
225 | */ |
||
226 | public function getType() |
||
227 | { |
||
228 | return $this->type; |
||
229 | } |
||
230 | |||
231 | |||
232 | public function getKey() |
||
233 | { |
||
234 | return $this->key; |
||
235 | } |
||
236 | |||
237 | |||
238 | /** |
||
239 | * |
||
240 | * @param Boolean $staticMap |
||
241 | * @return \Oli\GoogleAPI\MapAPI |
||
242 | * @throws \InvalidArgumentException |
||
243 | */ |
||
244 | public function isStaticMap($staticMap = TRUE) |
||
245 | { |
||
246 | if (!is_bool($staticMap)) |
||
247 | { |
||
248 | throw new \InvalidArgumentException("staticMap must be boolean, $staticMap (".gettype($staticMap).") was given"); |
||
249 | } |
||
250 | |||
251 | $this->staticMap = $staticMap; |
||
252 | return $this; |
||
253 | } |
||
254 | |||
255 | |||
256 | public function getIsStaticMap() |
||
257 | { |
||
258 | return $this->staticMap; |
||
259 | } |
||
260 | |||
261 | |||
262 | /** |
||
263 | * |
||
264 | * @param Boolean $clickable |
||
265 | * @return \Oli\GoogleAPI\MapAPI |
||
266 | * @throws \InvalidArgumentException |
||
267 | */ |
||
268 | public function isClickable($clickable = TRUE) |
||
269 | { |
||
270 | if (!$this->staticMap) |
||
271 | { |
||
272 | throw new \InvalidArgumentException("the 'clickable' option only applies to static maps"); |
||
273 | } |
||
274 | |||
275 | if (!is_bool($clickable)) |
||
276 | { |
||
277 | throw new \InvalidArgumentException("clickable must be boolean, $clickable (".gettype($clickable).") was given"); |
||
278 | } |
||
279 | |||
280 | $this->clickable = $clickable; |
||
281 | return $this; |
||
282 | } |
||
283 | |||
284 | |||
285 | public function getIsClicable() |
||
286 | { |
||
287 | return $this->clickable; |
||
288 | } |
||
289 | |||
290 | |||
291 | public function isScrollable($scrollable = TRUE) |
||
292 | { |
||
293 | if (!is_bool($scrollable)) |
||
294 | { |
||
295 | throw new \InvalidArgumentException("staticMap must be boolean, $scrollable (".gettype($scrollable).") was given"); |
||
296 | } |
||
297 | |||
298 | $this->scrollable = $scrollable; |
||
299 | return $this; |
||
300 | } |
||
301 | |||
302 | |||
303 | public function getIsScrollable() |
||
304 | { |
||
305 | return $this->scrollable; |
||
306 | } |
||
307 | |||
308 | |||
309 | /** |
||
310 | * |
||
311 | * @param \Oli\GoogleAPI\Markers $markers |
||
312 | * @return \Oli\GoogleAPI\MapAPI |
||
313 | */ |
||
314 | public function addMarkers(Markers $markers) |
||
315 | { |
||
316 | $this->markers = $markers->getMarkers(); |
||
317 | $this->bound = $markers->getBound(); |
||
318 | $this->markerClusterer = $markers->getMarkerClusterer(); |
||
319 | return $this; |
||
320 | } |
||
321 | |||
322 | |||
323 | /** |
||
324 | * Alias to handleMarkers() |
||
325 | */ |
||
326 | public function invalidateMarkers() |
||
327 | { |
||
328 | $this->handleMarkers(); |
||
329 | } |
||
330 | |||
331 | |||
332 | /** |
||
333 | * @see Nette\Application\Control#render() |
||
334 | */ |
||
335 | public function render() |
||
336 | { |
||
337 | if ($this->staticMap) |
||
338 | { |
||
339 | $this->template->height = $this->height; |
||
340 | $this->template->width = $this->width; |
||
341 | $this->template->zoom = $this->zoom; |
||
342 | $this->template->position = $this->coordinates; |
||
343 | $this->template->markers = $this->markers; |
||
344 | $this->template->clickable = $this->clickable; |
||
345 | $this->template->setFile(dirname(__FILE__) . '/static.latte'); |
||
346 | } else |
||
347 | { |
||
348 | $map = array( |
||
349 | 'position' => $this->coordinates, |
||
350 | 'height' => $this->height, |
||
351 | 'width' => $this->width, |
||
352 | 'zoom' => $this->zoom, |
||
353 | 'type' => $this->type, |
||
354 | 'scrollable' => $this->scrollable, |
||
355 | 'key' => $this->key, |
||
356 | 'bound' => $this->bound, |
||
357 | 'cluster' => $this->markerClusterer, |
||
358 | 'waypoint' => !is_null($this->waypoints) ? array_merge($this->waypoints, $this->direction) : NULL |
||
359 | ); |
||
360 | $this->template->map = \Nette\Utils\Json::encode($map); |
||
361 | $this->template->setFile(dirname(__FILE__) . '/template.latte'); |
||
362 | } |
||
363 | $this->template->render(); |
||
364 | } |
||
365 | |||
366 | |||
367 | /** |
||
368 | * Send markers to template as JSON |
||
369 | * @internal |
||
370 | */ |
||
371 | public function handleMarkers() |
||
372 | { |
||
373 | $this->getPresenter()->sendResponse(new JsonResponse($this->markers)); |
||
374 | } |
||
375 | } |
||
376 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.