|
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; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\GoogleMap\Base\Coordinate; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @see http://code.google.com/apis/maps/documentation/javascript/reference.html#DistanceMatrixRequest |
|
18
|
|
|
* |
|
19
|
|
|
* @author GeLo <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class DistanceMatrixRequest |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Coordinate[]|string[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $origins = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Coordinate[]|string[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $destinations = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \DateTime|null |
|
35
|
|
|
*/ |
|
36
|
|
|
private $departureTime; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var \DateTime|null |
|
40
|
|
|
*/ |
|
41
|
|
|
private $arrivalTime; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string|null |
|
45
|
|
|
*/ |
|
46
|
|
|
private $travelMode; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var bool|null |
|
50
|
|
|
*/ |
|
51
|
|
|
private $avoidHighways; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var bool|null |
|
55
|
|
|
*/ |
|
56
|
|
|
private $avoidTolls; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string|null |
|
60
|
|
|
*/ |
|
61
|
|
|
private $region; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string|null |
|
65
|
|
|
*/ |
|
66
|
|
|
private $unitSystem; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var string|null |
|
70
|
|
|
*/ |
|
71
|
|
|
private $language; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Coordinate[]|string[] $origins |
|
75
|
|
|
* @param Coordinate[]|string[] $destinations |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct(array $origins, array $destinations) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->setOrigins($origins); |
|
80
|
|
|
$this->setDestinations($destinations); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function hasOrigins() |
|
87
|
|
|
{ |
|
88
|
|
|
return !empty($this->origins); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return Coordinate[]|string[] |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getOrigins() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->origins; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Coordinate[]|string[] $origins |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setOrigins(array $origins) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->origins = []; |
|
105
|
|
|
$this->addOrigins($origins); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param Coordinate[]|string[] $origins |
|
110
|
|
|
*/ |
|
111
|
|
|
public function addOrigins(array $origins) |
|
112
|
|
|
{ |
|
113
|
|
|
foreach ($origins as $origin) { |
|
114
|
|
|
$this->addOrigin($origin); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param Coordinate|string $origin |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function hasOrigin($origin) |
|
124
|
|
|
{ |
|
125
|
|
|
return in_array($origin, $this->origins, true); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param Coordinate|string $origin |
|
130
|
|
|
*/ |
|
131
|
|
|
public function addOrigin($origin) |
|
132
|
|
|
{ |
|
133
|
|
|
if (!$this->hasOrigin($origin)) { |
|
134
|
|
|
$this->origins[] = $origin; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param Coordinate|string $origin |
|
140
|
|
|
*/ |
|
141
|
|
|
public function removeOrigin($origin) |
|
142
|
|
|
{ |
|
143
|
|
|
unset($this->origins[array_search($origin, $this->origins, true)]); |
|
144
|
|
|
$this->origins = array_values($this->origins); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
|
|
public function hasDestinations() |
|
151
|
|
|
{ |
|
152
|
|
|
return !empty($this->destinations); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return Coordinate[]|string[] |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getDestinations() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->destinations; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param Coordinate[]|string[] $destinations |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setDestinations(array $destinations) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->destinations = []; |
|
169
|
|
|
$this->addDestinations($destinations); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param Coordinate[]|string[] $destinations |
|
174
|
|
|
*/ |
|
175
|
|
|
public function addDestinations(array $destinations) |
|
176
|
|
|
{ |
|
177
|
|
|
foreach ($destinations as $destination) { |
|
178
|
|
|
$this->addDestination($destination); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param Coordinate|string $destination |
|
184
|
|
|
* |
|
185
|
|
|
* @return bool |
|
186
|
|
|
*/ |
|
187
|
|
|
public function hasDestination($destination) |
|
188
|
|
|
{ |
|
189
|
|
|
return in_array($destination, $this->destinations, true); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param Coordinate|string $destination |
|
194
|
|
|
*/ |
|
195
|
|
|
public function addDestination($destination) |
|
196
|
|
|
{ |
|
197
|
|
|
if (!$this->hasDestination($destination)) { |
|
198
|
|
|
$this->destinations[] = $destination; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param Coordinate|string $destination |
|
204
|
|
|
*/ |
|
205
|
|
|
public function removeDestination($destination) |
|
206
|
|
|
{ |
|
207
|
|
|
unset($this->destinations[array_search($destination, $this->destinations, true)]); |
|
208
|
|
|
$this->destinations = array_values($this->destinations); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return bool |
|
213
|
|
|
*/ |
|
214
|
|
|
public function hasDepartureTime() |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->departureTime !== null; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @return \DateTime|null |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getDepartureTime() |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->departureTime; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @param \DateTime|null $departureTime |
|
229
|
|
|
*/ |
|
230
|
|
|
public function setDepartureTime(\DateTime $departureTime = null) |
|
231
|
|
|
{ |
|
232
|
|
|
$this->departureTime = $departureTime; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @return bool |
|
237
|
|
|
*/ |
|
238
|
|
|
public function hasArrivalTime() |
|
239
|
|
|
{ |
|
240
|
|
|
return $this->arrivalTime !== null; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @return \DateTime|null |
|
245
|
|
|
*/ |
|
246
|
|
|
public function getArrivalTime() |
|
247
|
|
|
{ |
|
248
|
|
|
return $this->arrivalTime; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param \DateTime|null $arrivalTime |
|
253
|
|
|
*/ |
|
254
|
|
|
public function setArrivalTime(\DateTime $arrivalTime = null) |
|
255
|
|
|
{ |
|
256
|
|
|
$this->arrivalTime = $arrivalTime; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @return bool |
|
261
|
|
|
*/ |
|
262
|
|
|
public function hasTravelMode() |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->travelMode !== null; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @return string|null |
|
269
|
|
|
*/ |
|
270
|
|
|
public function getTravelMode() |
|
271
|
|
|
{ |
|
272
|
|
|
return $this->travelMode; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @param string|null $travelMode |
|
277
|
|
|
*/ |
|
278
|
|
|
public function setTravelMode($travelMode = null) |
|
279
|
|
|
{ |
|
280
|
|
|
$this->travelMode = $travelMode; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @return bool |
|
285
|
|
|
*/ |
|
286
|
|
|
public function hasAvoidHighways() |
|
287
|
|
|
{ |
|
288
|
|
|
return $this->avoidHighways !== null; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* @return bool|null |
|
293
|
|
|
*/ |
|
294
|
|
|
public function getAvoidHighways() |
|
295
|
|
|
{ |
|
296
|
|
|
return $this->avoidHighways; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* @param bool|null $avoidHighways |
|
301
|
|
|
*/ |
|
302
|
|
|
public function setAvoidHighways($avoidHighways = null) |
|
303
|
|
|
{ |
|
304
|
|
|
$this->avoidHighways = $avoidHighways; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @return bool |
|
309
|
|
|
*/ |
|
310
|
|
|
public function hasAvoidTolls() |
|
311
|
|
|
{ |
|
312
|
|
|
return $this->avoidTolls !== null; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* @return bool|null |
|
317
|
|
|
*/ |
|
318
|
|
|
public function getAvoidTolls() |
|
319
|
|
|
{ |
|
320
|
|
|
return $this->avoidTolls; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @param bool|null $avoidTolls |
|
325
|
|
|
*/ |
|
326
|
|
|
public function setAvoidTolls($avoidTolls = null) |
|
327
|
|
|
{ |
|
328
|
|
|
$this->avoidTolls = $avoidTolls; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* @return bool |
|
333
|
|
|
*/ |
|
334
|
|
|
public function hasRegion() |
|
335
|
|
|
{ |
|
336
|
|
|
return $this->region !== null; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* @return string|null |
|
341
|
|
|
*/ |
|
342
|
|
|
public function getRegion() |
|
343
|
|
|
{ |
|
344
|
|
|
return $this->region; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @param string|null $region |
|
349
|
|
|
*/ |
|
350
|
|
|
public function setRegion($region = null) |
|
351
|
|
|
{ |
|
352
|
|
|
$this->region = $region; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* @return bool |
|
357
|
|
|
*/ |
|
358
|
|
|
public function hasUnitSystem() |
|
359
|
|
|
{ |
|
360
|
|
|
return $this->unitSystem !== null; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* @return string|null |
|
365
|
|
|
*/ |
|
366
|
|
|
public function getUnitSystem() |
|
367
|
|
|
{ |
|
368
|
|
|
return $this->unitSystem; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* @param string|null $unitSystem |
|
373
|
|
|
*/ |
|
374
|
|
|
public function setUnitSystem($unitSystem = null) |
|
375
|
|
|
{ |
|
376
|
|
|
$this->unitSystem = $unitSystem; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* @return bool |
|
381
|
|
|
*/ |
|
382
|
|
|
public function hasLanguage() |
|
383
|
|
|
{ |
|
384
|
|
|
return $this->language !== null; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* @return string|null |
|
389
|
|
|
*/ |
|
390
|
|
|
public function getLanguage() |
|
391
|
|
|
{ |
|
392
|
|
|
return $this->language; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* @param string|null $language |
|
397
|
|
|
*/ |
|
398
|
|
|
public function setLanguage($language = null) |
|
399
|
|
|
{ |
|
400
|
|
|
$this->language = $language; |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* @return mixed[] |
|
405
|
|
|
*/ |
|
406
|
|
|
public function buildQuery() |
|
407
|
|
|
{ |
|
408
|
|
|
$query = [ |
|
409
|
|
|
'origins' => implode('|', $this->buildPlaces($this->origins)), |
|
410
|
|
|
'destinations' => implode('|', $this->buildPlaces($this->destinations)), |
|
411
|
|
|
]; |
|
412
|
|
|
|
|
413
|
|
|
if ($this->hasDepartureTime()) { |
|
414
|
|
|
$query['departure_time'] = $this->departureTime->getTimestamp(); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
if ($this->hasArrivalTime()) { |
|
418
|
|
|
$query['arrival_time'] = $this->arrivalTime->getTimestamp(); |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
if ($this->hasTravelMode()) { |
|
422
|
|
|
$query['mode'] = strtolower($this->travelMode); |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
View Code Duplication |
if ($this->avoidTolls) { |
|
|
|
|
|
|
426
|
|
|
$query['avoid'] = 'tolls'; |
|
427
|
|
|
} elseif ($this->avoidHighways) { |
|
428
|
|
|
$query['avoid'] = 'highways'; |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
if ($this->hasUnitSystem()) { |
|
432
|
|
|
$query['units'] = strtolower($this->unitSystem); |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
if ($this->hasRegion()) { |
|
436
|
|
|
$query['region'] = $this->region; |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
if ($this->hasLanguage()) { |
|
440
|
|
|
$query['language'] = $this->language; |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
return $query; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* @param Coordinate[]|string[] $places |
|
448
|
|
|
* |
|
449
|
|
|
* @return string[] |
|
450
|
|
|
*/ |
|
451
|
|
|
private function buildPlaces(array $places) |
|
452
|
|
|
{ |
|
453
|
|
|
$result = []; |
|
454
|
|
|
|
|
455
|
|
|
foreach ($places as $place) { |
|
456
|
|
|
$result[] = $this->buildPlace($place); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
return $result; |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
/** |
|
463
|
|
|
* @param Coordinate|string $place |
|
464
|
|
|
* |
|
465
|
|
|
* @return string |
|
466
|
|
|
*/ |
|
467
|
|
|
private function buildPlace($place) |
|
468
|
|
|
{ |
|
469
|
|
|
if ($place instanceof Coordinate) { |
|
470
|
|
|
return $place->getLatitude().','.$place->getLongitude(); |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
return $place; |
|
474
|
|
|
} |
|
475
|
|
|
} |
|
476
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.