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