1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Google Map package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMap\Service\Directions; |
13
|
|
|
|
14
|
|
|
use Ivory\GoogleMap\Base\Coordinate; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @see http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsRequest |
18
|
|
|
* |
19
|
|
|
* @author GeLo <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class DirectionsRequest |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Coordinate|string |
25
|
|
|
*/ |
26
|
|
|
private $origin; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Coordinate|string |
30
|
|
|
*/ |
31
|
|
|
private $destination; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \DateTime|null |
35
|
|
|
*/ |
36
|
|
|
private $departureTime; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \DateTime|null |
40
|
|
|
*/ |
41
|
|
|
private $arrivalTime; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var DirectionsWaypoint[] |
45
|
|
|
*/ |
46
|
|
|
private $waypoints = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool|null |
50
|
|
|
*/ |
51
|
|
|
private $optimizeWaypoints; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string|null |
55
|
|
|
*/ |
56
|
|
|
private $travelMode; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string|null |
60
|
|
|
*/ |
61
|
|
|
private $avoid; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var bool|null |
65
|
|
|
*/ |
66
|
|
|
private $provideRouteAlternatives; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string|null |
70
|
|
|
*/ |
71
|
|
|
private $region; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string|null |
75
|
|
|
*/ |
76
|
|
|
private $unitSystem; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var string|null |
80
|
|
|
*/ |
81
|
|
|
private $language; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Coordinate|string $origin |
85
|
|
|
* @param Coordinate|string $destination |
86
|
|
|
*/ |
87
|
|
|
public function __construct($origin, $destination) |
88
|
|
|
{ |
89
|
|
|
$this->setOrigin($origin); |
90
|
|
|
$this->setDestination($destination); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return Coordinate|string |
95
|
|
|
*/ |
96
|
|
|
public function getOrigin() |
97
|
|
|
{ |
98
|
|
|
return $this->origin; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Coordinate|string $origin |
103
|
|
|
*/ |
104
|
|
|
public function setOrigin($origin) |
105
|
|
|
{ |
106
|
|
|
$this->origin = $origin; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return Coordinate|string |
111
|
|
|
*/ |
112
|
|
|
public function getDestination() |
113
|
|
|
{ |
114
|
|
|
return $this->destination; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Coordinate|string $destination |
119
|
|
|
*/ |
120
|
|
|
public function setDestination($destination) |
121
|
|
|
{ |
122
|
|
|
$this->destination = $destination; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function hasDepartureTime() |
129
|
|
|
{ |
130
|
|
|
return $this->departureTime !== null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return \DateTime|null |
135
|
|
|
*/ |
136
|
|
|
public function getDepartureTime() |
137
|
|
|
{ |
138
|
|
|
return $this->departureTime; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param \DateTime|null $departureTime |
143
|
|
|
*/ |
144
|
|
|
public function setDepartureTime(\DateTime $departureTime = null) |
145
|
|
|
{ |
146
|
|
|
$this->departureTime = $departureTime; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function hasArrivalTime() |
153
|
|
|
{ |
154
|
|
|
return $this->arrivalTime !== null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return \DateTime|null |
159
|
|
|
*/ |
160
|
|
|
public function getArrivalTime() |
161
|
|
|
{ |
162
|
|
|
return $this->arrivalTime; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param \DateTime|null $arrivalTime |
167
|
|
|
*/ |
168
|
|
|
public function setArrivalTime(\DateTime $arrivalTime = null) |
169
|
|
|
{ |
170
|
|
|
$this->arrivalTime = $arrivalTime; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function hasWaypoints() |
177
|
|
|
{ |
178
|
|
|
return !empty($this->waypoints); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return DirectionsWaypoint[] |
183
|
|
|
*/ |
184
|
|
|
public function getWaypoints() |
185
|
|
|
{ |
186
|
|
|
return $this->waypoints; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param DirectionsWaypoint[] $waypoints |
191
|
|
|
*/ |
192
|
|
|
public function setWaypoints(array $waypoints) |
193
|
|
|
{ |
194
|
|
|
$this->waypoints = []; |
195
|
|
|
$this->addWaypoints($waypoints); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param DirectionsWaypoint[] $waypoints |
200
|
|
|
*/ |
201
|
|
|
public function addWaypoints(array $waypoints) |
202
|
|
|
{ |
203
|
|
|
foreach ($waypoints as $waypoint) { |
204
|
|
|
$this->addWaypoint($waypoint); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param DirectionsWaypoint $waypoint |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
|
public function hasWaypoint(DirectionsWaypoint $waypoint) |
214
|
|
|
{ |
215
|
|
|
return in_array($waypoint, $this->waypoints, true); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param DirectionsWaypoint $waypoint |
220
|
|
|
*/ |
221
|
|
|
public function addWaypoint(DirectionsWaypoint $waypoint) |
222
|
|
|
{ |
223
|
|
|
if (!$this->hasWaypoint($waypoint)) { |
224
|
|
|
$this->waypoints[] = $waypoint; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param DirectionsWaypoint $waypoint |
230
|
|
|
*/ |
231
|
|
|
public function removeWaypoint(DirectionsWaypoint $waypoint) |
232
|
|
|
{ |
233
|
|
|
unset($this->waypoints[array_search($waypoint, $this->waypoints, true)]); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return bool |
238
|
|
|
*/ |
239
|
|
|
public function hasOptimizeWaypoints() |
240
|
|
|
{ |
241
|
|
|
return $this->optimizeWaypoints !== null; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return bool|null |
246
|
|
|
*/ |
247
|
|
|
public function getOptimizeWaypoints() |
248
|
|
|
{ |
249
|
|
|
return $this->optimizeWaypoints; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param bool|null $optimizeWaypoints |
254
|
|
|
*/ |
255
|
|
|
public function setOptimizeWaypoints($optimizeWaypoints = null) |
256
|
|
|
{ |
257
|
|
|
$this->optimizeWaypoints = $optimizeWaypoints; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return bool |
262
|
|
|
*/ |
263
|
|
|
public function hasTravelMode() |
264
|
|
|
{ |
265
|
|
|
return $this->travelMode !== null; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return string|null |
270
|
|
|
*/ |
271
|
|
|
public function getTravelMode() |
272
|
|
|
{ |
273
|
|
|
return $this->travelMode; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param string|null $travelMode |
278
|
|
|
*/ |
279
|
|
|
public function setTravelMode($travelMode = null) |
280
|
|
|
{ |
281
|
|
|
$this->travelMode = $travelMode; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return bool |
286
|
|
|
*/ |
287
|
|
|
public function hasAvoid() |
288
|
|
|
{ |
289
|
|
|
return $this->avoid !== null; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return string|null |
294
|
|
|
*/ |
295
|
|
|
public function getAvoid() |
296
|
|
|
{ |
297
|
|
|
return $this->avoid; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param string|null $avoid |
302
|
|
|
*/ |
303
|
|
|
public function setAvoid($avoid = null) |
304
|
|
|
{ |
305
|
|
|
$this->avoid = $avoid; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @return bool |
310
|
|
|
*/ |
311
|
|
|
public function hasProvideRouteAlternatives() |
312
|
|
|
{ |
313
|
|
|
return $this->provideRouteAlternatives !== null; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @return bool|null |
318
|
|
|
*/ |
319
|
|
|
public function getProvideRouteAlternatives() |
320
|
|
|
{ |
321
|
|
|
return $this->provideRouteAlternatives; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @param bool|null $provideRouteAlternatives |
326
|
|
|
*/ |
327
|
|
|
public function setProvideRouteAlternatives($provideRouteAlternatives = null) |
328
|
|
|
{ |
329
|
|
|
$this->provideRouteAlternatives = $provideRouteAlternatives; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @return bool |
334
|
|
|
*/ |
335
|
|
|
public function hasRegion() |
336
|
|
|
{ |
337
|
|
|
return $this->region !== null; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @return string|null |
342
|
|
|
*/ |
343
|
|
|
public function getRegion() |
344
|
|
|
{ |
345
|
|
|
return $this->region; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @param string|null $region |
350
|
|
|
*/ |
351
|
|
|
public function setRegion($region = null) |
352
|
|
|
{ |
353
|
|
|
$this->region = $region; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @return bool |
358
|
|
|
*/ |
359
|
|
|
public function hasUnitSystem() |
360
|
|
|
{ |
361
|
|
|
return $this->unitSystem !== null; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @return string|null |
366
|
|
|
*/ |
367
|
|
|
public function getUnitSystem() |
368
|
|
|
{ |
369
|
|
|
return $this->unitSystem; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @param string|null $unitSystem |
374
|
|
|
*/ |
375
|
|
|
public function setUnitSystem($unitSystem = null) |
376
|
|
|
{ |
377
|
|
|
$this->unitSystem = $unitSystem; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @return bool |
382
|
|
|
*/ |
383
|
|
|
public function hasLanguage() |
384
|
|
|
{ |
385
|
|
|
return $this->language !== null; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @return string|null |
390
|
|
|
*/ |
391
|
|
|
public function getLanguage() |
392
|
|
|
{ |
393
|
|
|
return $this->language; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* @param string|null $language |
398
|
|
|
*/ |
399
|
|
|
public function setLanguage($language = null) |
400
|
|
|
{ |
401
|
|
|
$this->language = $language; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @return mixed[] |
406
|
|
|
*/ |
407
|
|
|
public function buildQuery() |
408
|
|
|
{ |
409
|
|
|
$query = [ |
410
|
|
|
'origin' => $this->buildPlace($this->origin), |
411
|
|
|
'destination' => $this->buildPlace($this->destination), |
412
|
|
|
]; |
413
|
|
|
|
414
|
|
|
if ($this->hasDepartureTime()) { |
415
|
|
|
$query['departure_time'] = $this->departureTime->getTimestamp(); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
if ($this->hasArrivalTime()) { |
419
|
|
|
$query['arrival_time'] = $this->arrivalTime->getTimestamp(); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
if ($this->hasWaypoints()) { |
423
|
|
|
$waypoints = []; |
424
|
|
|
|
425
|
|
|
if ($this->optimizeWaypoints) { |
426
|
|
|
$waypoints[] = 'optimize:true'; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
foreach ($this->waypoints as $waypoint) { |
430
|
|
|
$stopover = $waypoint->getStopover() ? 'via:' : ''; |
431
|
|
|
$waypoints[] = $stopover.$this->buildPlace($waypoint->getLocation()); |
|
|
|
|
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
$query['waypoints'] = implode('|', $waypoints); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
if ($this->hasTravelMode()) { |
438
|
|
|
$query['mode'] = strtolower($this->travelMode); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
if ($this->hasAvoid()) { |
442
|
|
|
$query['avoid'] = $this->avoid; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
if ($this->hasProvideRouteAlternatives()) { |
446
|
|
|
$query['alternatives'] = $this->provideRouteAlternatives ? 'true' : 'false'; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
if ($this->hasRegion()) { |
450
|
|
|
$query['region'] = $this->region; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
if ($this->hasUnitSystem()) { |
454
|
|
|
$query['units'] = strtolower($this->unitSystem); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
if ($this->hasLanguage()) { |
458
|
|
|
$query['language'] = $this->language; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
return $query; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* @param Coordinate|string $place |
466
|
|
|
* |
467
|
|
|
* @return string |
468
|
|
|
*/ |
469
|
|
|
private function buildPlace($place) |
470
|
|
|
{ |
471
|
|
|
if ($place instanceof Coordinate) { |
472
|
|
|
return $place->getLatitude().','.$place->getLongitude(); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
return $place; |
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.