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\Direction\Request; |
13
|
|
|
|
14
|
|
|
use Ivory\GoogleMap\Service\Base\Location\LocationInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @see http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionRequest |
18
|
|
|
* |
19
|
|
|
* @author GeLo <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class DirectionRequest implements DirectionRequestInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var LocationInterface |
25
|
|
|
*/ |
26
|
|
|
private $origin; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var LocationInterface |
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 DirectionWaypoint[] |
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 $trafficModel; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string[] |
75
|
|
|
*/ |
76
|
|
|
private $transitModes = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var string|null |
80
|
|
|
*/ |
81
|
|
|
private $transitRoutingPreference; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var string|null |
85
|
|
|
*/ |
86
|
|
|
private $region; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var string|null |
90
|
|
|
*/ |
91
|
|
|
private $unitSystem; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var string|null |
95
|
|
|
*/ |
96
|
|
|
private $language; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param LocationInterface $origin |
100
|
|
|
* @param LocationInterface $destination |
101
|
|
|
*/ |
102
|
|
|
public function __construct(LocationInterface $origin, LocationInterface $destination) |
103
|
|
|
{ |
104
|
|
|
$this->setOrigin($origin); |
105
|
|
|
$this->setDestination($destination); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return LocationInterface |
110
|
|
|
*/ |
111
|
|
|
public function getOrigin() |
112
|
|
|
{ |
113
|
|
|
return $this->origin; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param LocationInterface $origin |
118
|
|
|
*/ |
119
|
|
|
public function setOrigin(LocationInterface $origin) |
120
|
|
|
{ |
121
|
|
|
$this->origin = $origin; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return LocationInterface |
126
|
|
|
*/ |
127
|
|
|
public function getDestination() |
128
|
|
|
{ |
129
|
|
|
return $this->destination; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param LocationInterface $destination |
134
|
|
|
*/ |
135
|
|
|
public function setDestination(LocationInterface $destination) |
136
|
|
|
{ |
137
|
|
|
$this->destination = $destination; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function hasDepartureTime() |
144
|
|
|
{ |
145
|
|
|
return $this->departureTime !== null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return \DateTime|null |
150
|
|
|
*/ |
151
|
|
|
public function getDepartureTime() |
152
|
|
|
{ |
153
|
|
|
return $this->departureTime; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param \DateTime|null $departureTime |
158
|
|
|
*/ |
159
|
|
|
public function setDepartureTime(\DateTime $departureTime = null) |
160
|
|
|
{ |
161
|
|
|
$this->departureTime = $departureTime; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function hasArrivalTime() |
168
|
|
|
{ |
169
|
|
|
return $this->arrivalTime !== null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return \DateTime|null |
174
|
|
|
*/ |
175
|
|
|
public function getArrivalTime() |
176
|
|
|
{ |
177
|
|
|
return $this->arrivalTime; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param \DateTime|null $arrivalTime |
182
|
|
|
*/ |
183
|
|
|
public function setArrivalTime(\DateTime $arrivalTime = null) |
184
|
|
|
{ |
185
|
|
|
$this->arrivalTime = $arrivalTime; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
public function hasWaypoints() |
192
|
|
|
{ |
193
|
|
|
return !empty($this->waypoints); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return DirectionWaypoint[] |
198
|
|
|
*/ |
199
|
|
|
public function getWaypoints() |
200
|
|
|
{ |
201
|
|
|
return $this->waypoints; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param DirectionWaypoint[] $waypoints |
206
|
|
|
*/ |
207
|
|
|
public function setWaypoints(array $waypoints) |
208
|
|
|
{ |
209
|
|
|
$this->waypoints = []; |
210
|
|
|
$this->addWaypoints($waypoints); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param DirectionWaypoint[] $waypoints |
215
|
|
|
*/ |
216
|
|
|
public function addWaypoints(array $waypoints) |
217
|
|
|
{ |
218
|
|
|
foreach ($waypoints as $waypoint) { |
219
|
|
|
$this->addWaypoint($waypoint); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param DirectionWaypoint $waypoint |
225
|
|
|
* |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function hasWaypoint(DirectionWaypoint $waypoint) |
229
|
|
|
{ |
230
|
|
|
return in_array($waypoint, $this->waypoints, true); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param DirectionWaypoint $waypoint |
235
|
|
|
*/ |
236
|
|
|
public function addWaypoint(DirectionWaypoint $waypoint) |
237
|
|
|
{ |
238
|
|
|
if (!$this->hasWaypoint($waypoint)) { |
239
|
|
|
$this->waypoints[] = $waypoint; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param DirectionWaypoint $waypoint |
245
|
|
|
*/ |
246
|
|
|
public function removeWaypoint(DirectionWaypoint $waypoint) |
247
|
|
|
{ |
248
|
|
|
unset($this->waypoints[array_search($waypoint, $this->waypoints, true)]); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return bool |
253
|
|
|
*/ |
254
|
|
|
public function hasOptimizeWaypoints() |
255
|
|
|
{ |
256
|
|
|
return $this->optimizeWaypoints !== null; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return bool|null |
261
|
|
|
*/ |
262
|
|
|
public function getOptimizeWaypoints() |
263
|
|
|
{ |
264
|
|
|
return $this->optimizeWaypoints; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param bool|null $optimizeWaypoints |
269
|
|
|
*/ |
270
|
|
|
public function setOptimizeWaypoints($optimizeWaypoints = null) |
271
|
|
|
{ |
272
|
|
|
$this->optimizeWaypoints = $optimizeWaypoints; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return bool |
277
|
|
|
*/ |
278
|
|
|
public function hasTravelMode() |
279
|
|
|
{ |
280
|
|
|
return $this->travelMode !== null; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return string|null |
285
|
|
|
*/ |
286
|
|
|
public function getTravelMode() |
287
|
|
|
{ |
288
|
|
|
return $this->travelMode; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param string|null $travelMode |
293
|
|
|
*/ |
294
|
|
|
public function setTravelMode($travelMode = null) |
295
|
|
|
{ |
296
|
|
|
$this->travelMode = $travelMode; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return bool |
301
|
|
|
*/ |
302
|
|
|
public function hasAvoid() |
303
|
|
|
{ |
304
|
|
|
return $this->avoid !== null; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return string|null |
309
|
|
|
*/ |
310
|
|
|
public function getAvoid() |
311
|
|
|
{ |
312
|
|
|
return $this->avoid; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param string|null $avoid |
317
|
|
|
*/ |
318
|
|
|
public function setAvoid($avoid = null) |
319
|
|
|
{ |
320
|
|
|
$this->avoid = $avoid; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @return bool |
325
|
|
|
*/ |
326
|
|
|
public function hasProvideRouteAlternatives() |
327
|
|
|
{ |
328
|
|
|
return $this->provideRouteAlternatives !== null; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @return bool|null |
333
|
|
|
*/ |
334
|
|
|
public function getProvideRouteAlternatives() |
335
|
|
|
{ |
336
|
|
|
return $this->provideRouteAlternatives; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param bool|null $provideRouteAlternatives |
341
|
|
|
*/ |
342
|
|
|
public function setProvideRouteAlternatives($provideRouteAlternatives = null) |
343
|
|
|
{ |
344
|
|
|
$this->provideRouteAlternatives = $provideRouteAlternatives; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @return bool |
349
|
|
|
*/ |
350
|
|
|
public function hasTrafficModel() |
351
|
|
|
{ |
352
|
|
|
return $this->trafficModel !== null; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @return string|null |
357
|
|
|
*/ |
358
|
|
|
public function getTrafficModel() |
359
|
|
|
{ |
360
|
|
|
return $this->trafficModel; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param string|null $trafficModel |
365
|
|
|
*/ |
366
|
|
|
public function setTrafficModel($trafficModel) |
367
|
|
|
{ |
368
|
|
|
$this->trafficModel = $trafficModel; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @return bool |
373
|
|
|
*/ |
374
|
|
|
public function hasTransitModes() |
375
|
|
|
{ |
376
|
|
|
return !empty($this->transitModes); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @return string[] |
381
|
|
|
*/ |
382
|
|
|
public function getTransitModes() |
383
|
|
|
{ |
384
|
|
|
return $this->transitModes; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @param string[] $transitModes |
389
|
|
|
*/ |
390
|
|
|
public function setTransitModes(array $transitModes) |
391
|
|
|
{ |
392
|
|
|
$this->transitModes = []; |
393
|
|
|
$this->addTransitModes($transitModes); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* @param string[] $transitModes |
398
|
|
|
*/ |
399
|
|
|
public function addTransitModes(array $transitModes) |
400
|
|
|
{ |
401
|
|
|
foreach ($transitModes as $transitMode) { |
402
|
|
|
$this->addTransitMode($transitMode); |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @param string $transitMode |
408
|
|
|
* |
409
|
|
|
* @return bool |
410
|
|
|
*/ |
411
|
|
|
public function hasTransitMode($transitMode) |
412
|
|
|
{ |
413
|
|
|
return in_array($transitMode, $this->transitModes, true); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* @param string $transitMode |
418
|
|
|
*/ |
419
|
|
|
public function addTransitMode($transitMode) |
420
|
|
|
{ |
421
|
|
|
if (!$this->hasTransitMode($transitMode)) { |
422
|
|
|
$this->transitModes[] = $transitMode; |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* @param string $transitMode |
428
|
|
|
*/ |
429
|
|
|
public function removeTransitMode($transitMode) |
430
|
|
|
{ |
431
|
|
|
unset($this->transitModes[array_search($transitMode, $this->transitModes, true)]); |
432
|
|
|
$this->transitModes = array_values($this->transitModes); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* @return bool |
437
|
|
|
*/ |
438
|
|
|
public function hasTransitRoutingPreference() |
439
|
|
|
{ |
440
|
|
|
return $this->transitRoutingPreference !== null; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @return string|null |
445
|
|
|
*/ |
446
|
|
|
public function getTransitRoutingPreference() |
447
|
|
|
{ |
448
|
|
|
return $this->transitRoutingPreference; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @param string|null $transitRoutingPreference |
453
|
|
|
*/ |
454
|
|
|
public function setTransitRoutingPreference($transitRoutingPreference) |
455
|
|
|
{ |
456
|
|
|
$this->transitRoutingPreference = $transitRoutingPreference; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* @return bool |
461
|
|
|
*/ |
462
|
|
|
public function hasRegion() |
463
|
|
|
{ |
464
|
|
|
return $this->region !== null; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* @return string|null |
469
|
|
|
*/ |
470
|
|
|
public function getRegion() |
471
|
|
|
{ |
472
|
|
|
return $this->region; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* @param string|null $region |
477
|
|
|
*/ |
478
|
|
|
public function setRegion($region = null) |
479
|
|
|
{ |
480
|
|
|
$this->region = $region; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* @return bool |
485
|
|
|
*/ |
486
|
|
|
public function hasUnitSystem() |
487
|
|
|
{ |
488
|
|
|
return $this->unitSystem !== null; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* @return string|null |
493
|
|
|
*/ |
494
|
|
|
public function getUnitSystem() |
495
|
|
|
{ |
496
|
|
|
return $this->unitSystem; |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* @param string|null $unitSystem |
501
|
|
|
*/ |
502
|
|
|
public function setUnitSystem($unitSystem = null) |
503
|
|
|
{ |
504
|
|
|
$this->unitSystem = $unitSystem; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* @return bool |
509
|
|
|
*/ |
510
|
|
|
public function hasLanguage() |
511
|
|
|
{ |
512
|
|
|
return $this->language !== null; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* @return string|null |
517
|
|
|
*/ |
518
|
|
|
public function getLanguage() |
519
|
|
|
{ |
520
|
|
|
return $this->language; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* @param string|null $language |
525
|
|
|
*/ |
526
|
|
|
public function setLanguage($language = null) |
527
|
|
|
{ |
528
|
|
|
$this->language = $language; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* {@inheritdoc} |
533
|
|
|
*/ |
534
|
|
|
public function build() |
535
|
|
|
{ |
536
|
|
|
$query = [ |
537
|
|
|
'origin' => $this->origin->build(), |
538
|
|
|
'destination' => $this->destination->build(), |
539
|
|
|
]; |
540
|
|
|
|
541
|
|
|
if ($this->hasDepartureTime()) { |
542
|
|
|
$query['departure_time'] = $this->departureTime->getTimestamp(); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
if ($this->hasArrivalTime()) { |
546
|
|
|
$query['arrival_time'] = $this->arrivalTime->getTimestamp(); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
if ($this->hasWaypoints()) { |
550
|
|
|
$waypoints = []; |
551
|
|
|
|
552
|
|
|
if ($this->optimizeWaypoints) { |
553
|
|
|
$waypoints[] = 'optimize:true'; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
foreach ($this->waypoints as $waypoint) { |
557
|
|
|
$waypoints[] = ($waypoint->getStopover() ? 'via:' : '').$waypoint->getLocation()->build(); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
$query['waypoints'] = implode('|', $waypoints); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
if ($this->hasTravelMode()) { |
564
|
|
|
$query['mode'] = strtolower($this->travelMode); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
if ($this->hasAvoid()) { |
568
|
|
|
$query['avoid'] = $this->avoid; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
if ($this->hasProvideRouteAlternatives()) { |
572
|
|
|
$query['alternatives'] = $this->provideRouteAlternatives ? 'true' : 'false'; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
if ($this->hasTrafficModel()) { |
576
|
|
|
$query['traffic_model'] = $this->trafficModel; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
if ($this->hasTransitModes()) { |
580
|
|
|
$query['transit_mode'] = implode('|', $this->transitModes); |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
if ($this->hasTransitRoutingPreference()) { |
584
|
|
|
$query['transit_routing_preference'] = $this->transitRoutingPreference; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
if ($this->hasRegion()) { |
588
|
|
|
$query['region'] = $this->region; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
if ($this->hasUnitSystem()) { |
592
|
|
|
$query['units'] = strtolower($this->unitSystem); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
if ($this->hasLanguage()) { |
596
|
|
|
$query['language'] = $this->language; |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
return $query; |
600
|
|
|
} |
601
|
|
|
} |
602
|
|
|
|