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; |
13
|
|
|
|
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use Http\Message\MessageFactory; |
16
|
|
|
use Ivory\GoogleMap\Base\Bound; |
17
|
|
|
use Ivory\GoogleMap\Base\Coordinate; |
18
|
|
|
use Ivory\GoogleMap\Overlay\EncodedPolyline; |
19
|
|
|
use Ivory\GoogleMap\Service\AbstractService; |
20
|
|
|
use Ivory\GoogleMap\Service\Base\Distance; |
21
|
|
|
use Ivory\GoogleMap\Service\Base\Duration; |
22
|
|
|
use Ivory\GoogleMap\Service\Base\Fare; |
23
|
|
|
use Ivory\GoogleMap\Service\Direction\Request\DirectionRequestInterface; |
24
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\DirectionGeocoded; |
25
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\DirectionLeg; |
26
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\DirectionResponse; |
27
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\DirectionRoute; |
28
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\DirectionStep; |
29
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitAgency; |
30
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitDetails; |
31
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitLine; |
32
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitStop; |
33
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\Transit\DirectionTransitVehicle; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @author GeLo <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
class Direction extends AbstractService |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @param HttpClient $client |
42
|
|
|
* @param MessageFactory $messageFactory |
43
|
|
|
*/ |
44
|
|
|
public function __construct(HttpClient $client, MessageFactory $messageFactory) |
45
|
|
|
{ |
46
|
|
|
parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/directions'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param DirectionRequestInterface $request |
51
|
|
|
* |
52
|
|
|
* @return DirectionResponse |
53
|
|
|
*/ |
54
|
|
|
public function route(DirectionRequestInterface $request) |
55
|
|
|
{ |
56
|
|
|
$response = $this->getClient()->sendRequest($this->createRequest($request->build())); |
57
|
|
|
$data = $this->parse((string) $response->getBody()); |
58
|
|
|
|
59
|
|
|
return $this->buildResponse($data); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $data |
64
|
|
|
* |
65
|
|
|
* @return mixed[] |
66
|
|
|
*/ |
67
|
|
|
private function parse($data) |
68
|
|
|
{ |
69
|
|
|
if ($this->getFormat() === self::FORMAT_JSON) { |
70
|
|
|
return json_decode($data, true); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->getXmlParser()->parse($data, [ |
74
|
|
|
'leg' => 'legs', |
75
|
|
|
'route' => 'routes', |
76
|
|
|
'step' => 'steps', |
77
|
|
|
'waypoint_index' => 'waypoint_order', |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed[] $data |
83
|
|
|
* |
84
|
|
|
* @return DirectionResponse |
85
|
|
|
*/ |
86
|
|
|
private function buildResponse(array $data) |
87
|
|
|
{ |
88
|
|
|
$response = new DirectionResponse(); |
89
|
|
|
$response->setStatus($data['status']); |
90
|
|
|
|
91
|
|
|
if (isset($data['routes'])) { |
92
|
|
|
$response->setRoutes($this->buildRoutes($data['routes'])); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (isset($data['geocoded_waypoints'])) { |
96
|
|
|
$response->setGeocodedWaypoints($this->buildGeocodedWaypoints($data['geocoded_waypoints'])); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (isset($data['available_travel_modes'])) { |
100
|
|
|
$response->setAvailableTravelModes($data['available_travel_modes']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $response; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed[] $data |
108
|
|
|
* |
109
|
|
|
* @return DirectionRoute[] |
110
|
|
|
*/ |
111
|
|
|
private function buildRoutes(array $data) |
112
|
|
|
{ |
113
|
|
|
$routes = []; |
114
|
|
|
|
115
|
|
|
foreach ($data as $route) { |
116
|
|
|
$routes[] = $this->buildRoute($route); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $routes; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param mixed[] $data |
124
|
|
|
* |
125
|
|
|
* @return DirectionRoute |
126
|
|
|
*/ |
127
|
|
|
private function buildRoute(array $data) |
128
|
|
|
{ |
129
|
|
|
$route = new DirectionRoute(); |
130
|
|
|
$route->setBound($this->buildBound($data['bounds'])); |
131
|
|
|
$route->setLegs($this->buildLegs($data['legs'])); |
132
|
|
|
$route->setOverviewPolyline($this->buildEncodedPolyline($data['overview_polyline'])); |
133
|
|
|
|
134
|
|
|
if (isset($data['copyrights'])) { |
135
|
|
|
$route->setCopyrights($data['copyrights']); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (isset($data['summary'])) { |
139
|
|
|
$route->setSummary($data['summary']); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (isset($data['fare'])) { |
143
|
|
|
$route->setFare($this->buildFare($data['fare'])); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (isset($data['warnings'])) { |
147
|
|
|
$route->setWarnings($data['warnings']); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (isset($data['waypoint_order'])) { |
151
|
|
|
$route->setWaypointOrders($data['waypoint_order']); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $route; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param mixed[] $data |
159
|
|
|
* |
160
|
|
|
* @return DirectionGeocoded[] |
161
|
|
|
*/ |
162
|
|
|
private function buildGeocodedWaypoints(array $data) |
163
|
|
|
{ |
164
|
|
|
$waypoints = []; |
165
|
|
|
|
166
|
|
|
foreach ($data as $waypoint) { |
167
|
|
|
$waypoints[] = $this->buildGeocodedWaypoint($waypoint); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $waypoints; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param mixed[] $data |
175
|
|
|
* |
176
|
|
|
* @return DirectionGeocoded |
177
|
|
|
*/ |
178
|
|
|
private function buildGeocodedWaypoint(array $data) |
179
|
|
|
{ |
180
|
|
|
$geocodedWaypoint = new DirectionGeocoded(); |
181
|
|
|
$geocodedWaypoint->setStatus($data['geocoder_status']); |
182
|
|
|
$geocodedWaypoint->setTypes($data['types']); |
183
|
|
|
|
184
|
|
|
if (isset($data['place_id'])) { |
185
|
|
|
$geocodedWaypoint->setPlaceId($data['place_id']); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (isset($data['partial_match'])) { |
189
|
|
|
$geocodedWaypoint->setPartialMatch($data['partial_match']); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $geocodedWaypoint; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param mixed[] $data |
197
|
|
|
* |
198
|
|
|
* @return DirectionLeg[] |
199
|
|
|
*/ |
200
|
|
|
private function buildLegs(array $data) |
201
|
|
|
{ |
202
|
|
|
$legs = []; |
203
|
|
|
|
204
|
|
|
foreach ($data as $leg) { |
205
|
|
|
$legs[] = $this->buildLeg($leg); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $legs; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param mixed[] $data |
213
|
|
|
* |
214
|
|
|
* @return DirectionLeg |
215
|
|
|
*/ |
216
|
|
|
private function buildLeg(array $data) |
217
|
|
|
{ |
218
|
|
|
$leg = new DirectionLeg(); |
219
|
|
|
$leg->setDistance($this->buildDistance($data['distance'])); |
220
|
|
|
$leg->setDuration($this->buildDuration($data['duration'])); |
221
|
|
|
$leg->setStartLocation($this->buildCoordinate($data['start_location'])); |
222
|
|
|
$leg->setEndLocation($this->buildCoordinate($data['end_location'])); |
223
|
|
|
$leg->setStartAddress($data['start_address']); |
224
|
|
|
$leg->setEndAddress($data['end_address']); |
225
|
|
|
$leg->setSteps($this->buildSteps($data['steps'])); |
226
|
|
|
|
227
|
|
|
if (isset($data['departure_time'])) { |
228
|
|
|
$leg->setDepartureTime($this->buildDateTime($data['departure_time'])); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if (isset($data['arrival_time'])) { |
232
|
|
|
$leg->setArrivalTime($this->buildDateTime($data['arrival_time'])); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if (isset($data['via_waypoint'])) { |
236
|
|
|
$leg->setViaWaypoints($data['via_waypoint']); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
if (isset($data['duration_in_traffic'])) { |
240
|
|
|
$leg->setDurationInTraffic($this->buildDuration($data['duration_in_traffic'])); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $leg; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param mixed[] $data |
248
|
|
|
* |
249
|
|
|
* @return DirectionStep[] |
250
|
|
|
*/ |
251
|
|
|
private function buildSteps(array $data) |
252
|
|
|
{ |
253
|
|
|
$steps = []; |
254
|
|
|
|
255
|
|
|
foreach ($data as $step) { |
256
|
|
|
$steps[] = $this->buildStep($step); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $steps; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param mixed[] $data |
264
|
|
|
* |
265
|
|
|
* @return DirectionStep |
266
|
|
|
*/ |
267
|
|
|
private function buildStep(array $data) |
268
|
|
|
{ |
269
|
|
|
$step = new DirectionStep(); |
270
|
|
|
$step->setDistance($this->buildDistance($data['distance'])); |
271
|
|
|
$step->setDuration($this->buildDuration($data['duration'])); |
272
|
|
|
$step->setEndLocation($this->buildCoordinate($data['end_location'])); |
273
|
|
|
$step->setEncodedPolyline($this->buildEncodedPolyline($data['polyline'])); |
274
|
|
|
$step->setStartLocation($this->buildCoordinate($data['start_location'])); |
275
|
|
|
$step->setInstructions($data['html_instructions']); |
276
|
|
|
$step->setTravelMode($data['travel_mode']); |
277
|
|
|
|
278
|
|
|
if (isset($data['transit_details'])) { |
279
|
|
|
$step->setTransitDetails($this->buildTransitDetails($data['transit_details'])); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $step; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param mixed[] $data |
287
|
|
|
* |
288
|
|
|
* @return DirectionTransitDetails |
289
|
|
|
*/ |
290
|
|
|
private function buildTransitDetails(array $data) |
291
|
|
|
{ |
292
|
|
|
$transitDetails = new DirectionTransitDetails(); |
293
|
|
|
$transitDetails->setDepartureStop($this->buildTransitStop($data['departure_stop'])); |
294
|
|
|
$transitDetails->setArrivalStop($this->buildTransitStop($data['arrival_stop'])); |
295
|
|
|
$transitDetails->setDepartureTime($this->buildDateTime($data['departure_time'])); |
296
|
|
|
$transitDetails->setArrivalTime($this->buildDateTime($data['arrival_time'])); |
297
|
|
|
$transitDetails->setLine($this->buildTransitLine($data['line'])); |
298
|
|
|
$transitDetails->setHeadSign($data['headsign']); |
299
|
|
|
$transitDetails->setNumStops($data['num_stops']); |
300
|
|
|
|
301
|
|
|
if (isset($data['headway'])) { |
302
|
|
|
$transitDetails->setHeadWay($data['headway']); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $transitDetails; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param mixed[] $data |
310
|
|
|
* |
311
|
|
|
* @return DirectionTransitLine |
312
|
|
|
*/ |
313
|
|
|
private function buildTransitLine(array $data) |
314
|
|
|
{ |
315
|
|
|
$transitLine = new DirectionTransitLine(); |
316
|
|
|
$transitLine->setName($data['name']); |
317
|
|
|
$transitLine->setShortName($data['short_name']); |
318
|
|
|
$transitLine->setVehicle($this->buildTransitVehicle($data['vehicle'])); |
319
|
|
|
$transitLine->setAgencies($this->buildTransitAgencies($data['agencies'])); |
320
|
|
|
|
321
|
|
|
if (isset($data['color'])) { |
322
|
|
|
$transitLine->setColor($data['color']); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if (isset($data['url'])) { |
326
|
|
|
$transitLine->setUrl($data['url']); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
if (isset($data['icon'])) { |
330
|
|
|
$transitLine->setIcon($data['icon']); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
if (isset($data['text_color'])) { |
334
|
|
|
$transitLine->setTextColor($data['text_color']); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $transitLine; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param mixed[] $data |
342
|
|
|
* |
343
|
|
|
* @return DirectionTransitAgency[] |
344
|
|
|
*/ |
345
|
|
|
private function buildTransitAgencies(array $data) |
346
|
|
|
{ |
347
|
|
|
$agencies = []; |
348
|
|
|
|
349
|
|
|
foreach ($data as $agency) { |
350
|
|
|
$agencies[] = $this->buildTransitAgency($agency); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return $agencies; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @param mixed[] $data |
358
|
|
|
* |
359
|
|
|
* @return DirectionTransitAgency |
360
|
|
|
*/ |
361
|
|
|
private function buildTransitAgency(array $data) |
362
|
|
|
{ |
363
|
|
|
$transitAgency = new DirectionTransitAgency(); |
364
|
|
|
$transitAgency->setName($data['name']); |
365
|
|
|
$transitAgency->setPhone($data['phone']); |
366
|
|
|
$transitAgency->setUrl($data['url']); |
367
|
|
|
|
368
|
|
|
return $transitAgency; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @param mixed[] $data |
373
|
|
|
* |
374
|
|
|
* @return DirectionTransitStop |
375
|
|
|
*/ |
376
|
|
|
private function buildTransitStop(array $data) |
377
|
|
|
{ |
378
|
|
|
$transitStop = new DirectionTransitStop(); |
379
|
|
|
$transitStop->setName($data['name']); |
380
|
|
|
$transitStop->setLocation($this->buildCoordinate($data['location'])); |
381
|
|
|
|
382
|
|
|
return $transitStop; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @param mixed[] $data |
387
|
|
|
* |
388
|
|
|
* @return DirectionTransitVehicle |
389
|
|
|
*/ |
390
|
|
|
private function buildTransitVehicle(array $data) |
391
|
|
|
{ |
392
|
|
|
$transitVehicle = new DirectionTransitVehicle(); |
393
|
|
|
$transitVehicle->setName($data['name']); |
394
|
|
|
$transitVehicle->setIcon($data['icon']); |
395
|
|
|
$transitVehicle->setType($data['type']); |
396
|
|
|
|
397
|
|
|
return $transitVehicle; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* @param mixed[] $data |
402
|
|
|
* |
403
|
|
|
* @return \DateTime |
404
|
|
|
*/ |
405
|
|
|
private function buildDateTime(array $data) |
406
|
|
|
{ |
407
|
|
|
return new \DateTime('@'.$data['value'], new \DateTimeZone($data['time_zone'])); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @param mixed[] $data |
412
|
|
|
* |
413
|
|
|
* @return Bound |
414
|
|
|
*/ |
415
|
|
|
private function buildBound(array $data) |
416
|
|
|
{ |
417
|
|
|
return new Bound( |
418
|
|
|
$this->buildCoordinate($data['southwest']), |
419
|
|
|
$this->buildCoordinate($data['northeast']) |
420
|
|
|
); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @param mixed[] $data |
425
|
|
|
* |
426
|
|
|
* @return Coordinate |
427
|
|
|
*/ |
428
|
|
|
private function buildCoordinate(array $data) |
429
|
|
|
{ |
430
|
|
|
return new Coordinate($data['lat'], $data['lng']); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* @param mixed[] $data |
435
|
|
|
* |
436
|
|
|
* @return Distance |
437
|
|
|
*/ |
438
|
|
|
private function buildDistance(array $data) |
439
|
|
|
{ |
440
|
|
|
return new Distance($data['value'], $data['text']); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @param mixed[] $data |
445
|
|
|
* |
446
|
|
|
* @return Duration |
447
|
|
|
*/ |
448
|
|
|
private function buildDuration(array $data) |
449
|
|
|
{ |
450
|
|
|
return new Duration($data['value'], $data['text']); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* @param string[] $data |
455
|
|
|
* |
456
|
|
|
* @return EncodedPolyline |
457
|
|
|
*/ |
458
|
|
|
private function buildEncodedPolyline(array $data) |
459
|
|
|
{ |
460
|
|
|
return new EncodedPolyline($data['points']); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* @param mixed[] $data |
465
|
|
|
* |
466
|
|
|
* @return Fare |
467
|
|
|
*/ |
468
|
|
|
private function buildFare(array $data) |
469
|
|
|
{ |
470
|
|
|
return new Fare($data['value'], $data['currency'], $data['text']); |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|