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