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 string|null |
50
|
|
|
*/ |
51
|
|
|
private $avoid; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string|null |
55
|
|
|
*/ |
56
|
|
|
private $region; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string|null |
60
|
|
|
*/ |
61
|
|
|
private $unitSystem; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string|null |
65
|
|
|
*/ |
66
|
|
|
private $language; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Coordinate[]|string[] $origins |
70
|
|
|
* @param Coordinate[]|string[] $destinations |
71
|
|
|
*/ |
72
|
|
|
public function __construct(array $origins, array $destinations) |
73
|
|
|
{ |
74
|
|
|
$this->setOrigins($origins); |
75
|
|
|
$this->setDestinations($destinations); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function hasOrigins() |
82
|
|
|
{ |
83
|
|
|
return !empty($this->origins); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return Coordinate[]|string[] |
88
|
|
|
*/ |
89
|
|
|
public function getOrigins() |
90
|
|
|
{ |
91
|
|
|
return $this->origins; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Coordinate[]|string[] $origins |
96
|
|
|
*/ |
97
|
|
|
public function setOrigins(array $origins) |
98
|
|
|
{ |
99
|
|
|
$this->origins = []; |
100
|
|
|
$this->addOrigins($origins); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Coordinate[]|string[] $origins |
105
|
|
|
*/ |
106
|
|
|
public function addOrigins(array $origins) |
107
|
|
|
{ |
108
|
|
|
foreach ($origins as $origin) { |
109
|
|
|
$this->addOrigin($origin); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Coordinate|string $origin |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function hasOrigin($origin) |
119
|
|
|
{ |
120
|
|
|
return in_array($origin, $this->origins, true); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Coordinate|string $origin |
125
|
|
|
*/ |
126
|
|
|
public function addOrigin($origin) |
127
|
|
|
{ |
128
|
|
|
if (!$this->hasOrigin($origin)) { |
129
|
|
|
$this->origins[] = $origin; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Coordinate|string $origin |
135
|
|
|
*/ |
136
|
|
|
public function removeOrigin($origin) |
137
|
|
|
{ |
138
|
|
|
unset($this->origins[array_search($origin, $this->origins, true)]); |
139
|
|
|
$this->origins = array_values($this->origins); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function hasDestinations() |
146
|
|
|
{ |
147
|
|
|
return !empty($this->destinations); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return Coordinate[]|string[] |
152
|
|
|
*/ |
153
|
|
|
public function getDestinations() |
154
|
|
|
{ |
155
|
|
|
return $this->destinations; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param Coordinate[]|string[] $destinations |
160
|
|
|
*/ |
161
|
|
|
public function setDestinations(array $destinations) |
162
|
|
|
{ |
163
|
|
|
$this->destinations = []; |
164
|
|
|
$this->addDestinations($destinations); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param Coordinate[]|string[] $destinations |
169
|
|
|
*/ |
170
|
|
|
public function addDestinations(array $destinations) |
171
|
|
|
{ |
172
|
|
|
foreach ($destinations as $destination) { |
173
|
|
|
$this->addDestination($destination); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param Coordinate|string $destination |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function hasDestination($destination) |
183
|
|
|
{ |
184
|
|
|
return in_array($destination, $this->destinations, true); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param Coordinate|string $destination |
189
|
|
|
*/ |
190
|
|
|
public function addDestination($destination) |
191
|
|
|
{ |
192
|
|
|
if (!$this->hasDestination($destination)) { |
193
|
|
|
$this->destinations[] = $destination; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param Coordinate|string $destination |
199
|
|
|
*/ |
200
|
|
|
public function removeDestination($destination) |
201
|
|
|
{ |
202
|
|
|
unset($this->destinations[array_search($destination, $this->destinations, true)]); |
203
|
|
|
$this->destinations = array_values($this->destinations); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return bool |
208
|
|
|
*/ |
209
|
|
|
public function hasDepartureTime() |
210
|
|
|
{ |
211
|
|
|
return $this->departureTime !== null; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return \DateTime|null |
216
|
|
|
*/ |
217
|
|
|
public function getDepartureTime() |
218
|
|
|
{ |
219
|
|
|
return $this->departureTime; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param \DateTime|null $departureTime |
224
|
|
|
*/ |
225
|
|
|
public function setDepartureTime(\DateTime $departureTime = null) |
226
|
|
|
{ |
227
|
|
|
$this->departureTime = $departureTime; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
public function hasArrivalTime() |
234
|
|
|
{ |
235
|
|
|
return $this->arrivalTime !== null; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return \DateTime|null |
240
|
|
|
*/ |
241
|
|
|
public function getArrivalTime() |
242
|
|
|
{ |
243
|
|
|
return $this->arrivalTime; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param \DateTime|null $arrivalTime |
248
|
|
|
*/ |
249
|
|
|
public function setArrivalTime(\DateTime $arrivalTime = null) |
250
|
|
|
{ |
251
|
|
|
$this->arrivalTime = $arrivalTime; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
|
|
public function hasTravelMode() |
258
|
|
|
{ |
259
|
|
|
return $this->travelMode !== null; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return string|null |
264
|
|
|
*/ |
265
|
|
|
public function getTravelMode() |
266
|
|
|
{ |
267
|
|
|
return $this->travelMode; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param string|null $travelMode |
272
|
|
|
*/ |
273
|
|
|
public function setTravelMode($travelMode = null) |
274
|
|
|
{ |
275
|
|
|
$this->travelMode = $travelMode; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return bool |
280
|
|
|
*/ |
281
|
|
|
public function hasAvoid() |
282
|
|
|
{ |
283
|
|
|
return $this->avoid !== null; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return string|null |
288
|
|
|
*/ |
289
|
|
|
public function getAvoid() |
290
|
|
|
{ |
291
|
|
|
return $this->avoid; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param string|null $avoid |
296
|
|
|
*/ |
297
|
|
|
public function setAvoid($avoid = null) |
298
|
|
|
{ |
299
|
|
|
$this->avoid = $avoid; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @return bool |
304
|
|
|
*/ |
305
|
|
|
public function hasRegion() |
306
|
|
|
{ |
307
|
|
|
return $this->region !== null; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @return string|null |
312
|
|
|
*/ |
313
|
|
|
public function getRegion() |
314
|
|
|
{ |
315
|
|
|
return $this->region; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param string|null $region |
320
|
|
|
*/ |
321
|
|
|
public function setRegion($region = null) |
322
|
|
|
{ |
323
|
|
|
$this->region = $region; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @return bool |
328
|
|
|
*/ |
329
|
|
|
public function hasUnitSystem() |
330
|
|
|
{ |
331
|
|
|
return $this->unitSystem !== null; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @return string|null |
336
|
|
|
*/ |
337
|
|
|
public function getUnitSystem() |
338
|
|
|
{ |
339
|
|
|
return $this->unitSystem; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param string|null $unitSystem |
344
|
|
|
*/ |
345
|
|
|
public function setUnitSystem($unitSystem = null) |
346
|
|
|
{ |
347
|
|
|
$this->unitSystem = $unitSystem; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @return bool |
352
|
|
|
*/ |
353
|
|
|
public function hasLanguage() |
354
|
|
|
{ |
355
|
|
|
return $this->language !== null; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @return string|null |
360
|
|
|
*/ |
361
|
|
|
public function getLanguage() |
362
|
|
|
{ |
363
|
|
|
return $this->language; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param string|null $language |
368
|
|
|
*/ |
369
|
|
|
public function setLanguage($language = null) |
370
|
|
|
{ |
371
|
|
|
$this->language = $language; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @return mixed[] |
376
|
|
|
*/ |
377
|
|
|
public function buildQuery() |
378
|
|
|
{ |
379
|
|
|
$query = [ |
380
|
|
|
'origins' => implode('|', $this->buildPlaces($this->origins)), |
381
|
|
|
'destinations' => implode('|', $this->buildPlaces($this->destinations)), |
382
|
|
|
]; |
383
|
|
|
|
384
|
|
|
if ($this->hasDepartureTime()) { |
385
|
|
|
$query['departure_time'] = $this->departureTime->getTimestamp(); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
if ($this->hasArrivalTime()) { |
389
|
|
|
$query['arrival_time'] = $this->arrivalTime->getTimestamp(); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
if ($this->hasTravelMode()) { |
393
|
|
|
$query['mode'] = strtolower($this->travelMode); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
if ($this->hasAvoid()) { |
397
|
|
|
$query['avoid'] = $this->avoid; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
if ($this->hasUnitSystem()) { |
401
|
|
|
$query['units'] = strtolower($this->unitSystem); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
if ($this->hasRegion()) { |
405
|
|
|
$query['region'] = $this->region; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
if ($this->hasLanguage()) { |
409
|
|
|
$query['language'] = $this->language; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
return $query; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @param Coordinate[]|string[] $places |
417
|
|
|
* |
418
|
|
|
* @return string[] |
419
|
|
|
*/ |
420
|
|
|
private function buildPlaces(array $places) |
421
|
|
|
{ |
422
|
|
|
$result = []; |
423
|
|
|
|
424
|
|
|
foreach ($places as $place) { |
425
|
|
|
$result[] = $this->buildPlace($place); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
return $result; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param Coordinate|string $place |
433
|
|
|
* |
434
|
|
|
* @return string |
435
|
|
|
*/ |
436
|
|
|
private function buildPlace($place) |
437
|
|
|
{ |
438
|
|
|
if ($place instanceof Coordinate) { |
439
|
|
|
return $place->getLatitude().','.$place->getLongitude(); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
return $place; |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|