1
|
|
|
<?php namespace Arcanedev\GeoLocation\Google\Directions; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position; |
4
|
|
|
use Arcanedev\GeoLocation\Google\AbstractService; |
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DirectionsService |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\GeoLocation\Google\Directions |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @link https://developers.google.com/maps/documentation/directions/intro |
15
|
|
|
*/ |
16
|
|
|
class DirectionsService extends AbstractService |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Constants |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
const BASE_URL = 'https://maps.googleapis.com/maps/api/directions/json'; |
24
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
26
|
|
|
| Properties |
27
|
|
|
| ----------------------------------------------------------------- |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Start point. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $origin; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* End point. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $destination; |
43
|
|
|
|
44
|
|
|
/* ----------------------------------------------------------------- |
45
|
|
|
| Constructor |
46
|
|
|
| ----------------------------------------------------------------- |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* DirectionsService constructor. |
51
|
|
|
* |
52
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
53
|
|
|
*/ |
54
|
15 |
|
public function __construct(ClientInterface $client) |
55
|
|
|
{ |
56
|
15 |
|
parent::__construct($client); |
57
|
|
|
|
58
|
15 |
|
$this->setKey(getenv('GOOGLE_MAPS_DIRECTIONS_KEY')); |
59
|
15 |
|
} |
60
|
|
|
|
61
|
|
|
/* ----------------------------------------------------------------- |
62
|
|
|
| Getters & Setters |
63
|
|
|
| ----------------------------------------------------------------- |
64
|
|
|
*/ |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the start point. |
68
|
|
|
* |
69
|
|
|
* @param string $origin |
70
|
|
|
* |
71
|
|
|
* @return self |
72
|
|
|
*/ |
73
|
12 |
|
public function setOrigin($origin) |
74
|
|
|
{ |
75
|
12 |
|
$this->origin = $origin; |
76
|
|
|
|
77
|
12 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set the origin point with an address (alias). |
82
|
|
|
* |
83
|
|
|
* @param string $address |
84
|
|
|
* |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
3 |
|
public function from($address) |
88
|
|
|
{ |
89
|
3 |
|
return $this->setOrigin($address); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the origin point with position object. |
94
|
|
|
* |
95
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position $position |
96
|
|
|
* |
97
|
|
|
* @return self |
98
|
|
|
*/ |
99
|
3 |
|
public function fromPosition(Position $position) |
100
|
|
|
{ |
101
|
3 |
|
return $this->fromCoordinates( |
102
|
3 |
|
$position->lat()->value(), |
103
|
3 |
|
$position->lng()->value() |
104
|
1 |
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the origin point with coordinates. |
109
|
|
|
* |
110
|
|
|
* @param float $lat |
111
|
|
|
* @param float $long |
112
|
|
|
* |
113
|
|
|
* @return self |
114
|
|
|
*/ |
115
|
6 |
|
public function fromCoordinates($lat, $long) |
116
|
|
|
{ |
117
|
6 |
|
return $this->setOrigin("{$lat},{$long}"); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the origin point with place id. |
122
|
|
|
* |
123
|
|
|
* @param string $placeId |
124
|
|
|
* |
125
|
|
|
* @return self |
126
|
|
|
*/ |
127
|
3 |
|
public function fromPlace($placeId) |
128
|
|
|
{ |
129
|
3 |
|
return $this->setOrigin("place_id:{$placeId}"); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the end point. |
134
|
|
|
* |
135
|
|
|
* @param string $destination |
136
|
|
|
* |
137
|
|
|
* @return self |
138
|
|
|
*/ |
139
|
12 |
|
public function setDestination($destination) |
140
|
|
|
{ |
141
|
12 |
|
$this->destination = $destination; |
142
|
|
|
|
143
|
12 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set the destination point with an address. |
148
|
|
|
* |
149
|
|
|
* @param string $address |
150
|
|
|
* |
151
|
|
|
* @return self |
152
|
|
|
*/ |
153
|
3 |
|
public function to($address) |
154
|
|
|
{ |
155
|
3 |
|
return $this->setDestination($address); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set the destination point with position object. |
160
|
|
|
* |
161
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position $position |
162
|
|
|
* |
163
|
|
|
* @return self |
164
|
|
|
*/ |
165
|
3 |
|
public function toPosition(Position $position) |
166
|
|
|
{ |
167
|
3 |
|
return $this->toCoordinates( |
168
|
3 |
|
$position->lat()->value(), |
169
|
3 |
|
$position->lng()->value() |
170
|
1 |
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set the destination point with coordinates. |
175
|
|
|
* |
176
|
|
|
* @param float $lat |
177
|
|
|
* @param float $long |
178
|
|
|
* |
179
|
|
|
* @return self |
180
|
|
|
*/ |
181
|
6 |
|
public function toCoordinates($lat, $long) |
182
|
|
|
{ |
183
|
6 |
|
return $this->setDestination("{$lat},{$long}"); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the destination point with place id. |
188
|
|
|
* |
189
|
|
|
* @param string $placeId |
190
|
|
|
* |
191
|
|
|
* @return self |
192
|
|
|
*/ |
193
|
3 |
|
public function toPlace($placeId) |
194
|
|
|
{ |
195
|
3 |
|
return $this->setDestination("place_id:{$placeId}"); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/* ----------------------------------------------------------------- |
199
|
|
|
| Main Methods |
200
|
|
|
| ----------------------------------------------------------------- |
201
|
|
|
*/ |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the directions. |
205
|
|
|
* |
206
|
|
|
* @param array $options |
207
|
|
|
* |
208
|
|
|
* @return \Arcanedev\GeoLocation\Google\Directions\DirectionsResponse |
209
|
|
|
*/ |
210
|
12 |
|
public function directions(array $options = []) |
211
|
|
|
{ |
212
|
12 |
|
$url = static::BASE_URL.$this->prepareQuery([ |
213
|
12 |
|
'origin' => $this->origin, |
214
|
12 |
|
'destination' => $this->destination, |
215
|
4 |
|
]); |
216
|
|
|
|
217
|
12 |
|
return $this->get($url, $options); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/* ----------------------------------------------------------------- |
221
|
|
|
| Other Methods |
222
|
|
|
| ----------------------------------------------------------------- |
223
|
|
|
*/ |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get the default query params. |
227
|
|
|
* |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
12 |
|
protected function getDefaultQueryParams() |
231
|
|
|
{ |
232
|
|
|
return [ |
233
|
12 |
|
'key' => $this->key, |
234
|
4 |
|
]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Prepare the response. |
239
|
|
|
* |
240
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
241
|
|
|
* |
242
|
|
|
* @return \Arcanedev\GeoLocation\Google\Directions\DirectionsResponse |
243
|
|
|
*/ |
244
|
12 |
|
protected function prepareResponse(ResponseInterface $response) |
245
|
|
|
{ |
246
|
12 |
|
return new DirectionsResponse( |
247
|
12 |
|
json_decode($response->getBody(), true) |
248
|
4 |
|
); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|