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