1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Redbox\Distance; |
4
|
|
|
|
5
|
|
|
class CalculateDistance |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* We will use this url to test our distance information from. |
9
|
|
|
* |
10
|
|
|
* @see \Redbox\Distance\CalculateDistance::calculateDistance() |
11
|
|
|
*/ |
12
|
|
|
const MAPS_DISTANCE_MATRIX_API_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Kilometer to miles conversion rate. |
16
|
|
|
*/ |
17
|
|
|
const KM_TO_MILES_CONVERTER = 0.62137; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Kilometer to yards conversion rate. |
21
|
|
|
*/ |
22
|
16 |
|
const KM_TO_YARD_CONVERTER = 1093.6133; |
23
|
16 |
|
|
24
|
16 |
|
/** |
25
|
16 |
|
* The useragent string we use in the curl request. |
26
|
16 |
|
*/ |
27
|
16 |
|
const USER_AGENT = 'Calculate Distance V1.2.3'; |
28
|
16 |
|
|
29
|
16 |
|
/** |
30
|
|
|
* The source string. |
31
|
16 |
|
* |
32
|
16 |
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $source = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The destination string. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
16 |
|
*/ |
41
|
|
|
protected $destination = ''; |
42
|
16 |
|
|
43
|
16 |
|
/** |
44
|
|
|
* The Google API key. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $googleAPIkey = ''; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Define the request options. |
52
|
|
|
* |
53
|
2 |
|
* @var array |
54
|
|
|
*/ |
55
|
2 |
|
protected $urlOptions = []; |
56
|
2 |
|
|
57
|
|
|
/** |
58
|
|
|
* Option to disable SSL verification, |
59
|
|
|
* i would suggest enabling it. |
60
|
|
|
* |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
protected $disable_ssl_verifier = true; |
64
|
|
|
|
65
|
6 |
|
/** |
66
|
|
|
* The API url. |
67
|
6 |
|
* |
68
|
6 |
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $api_url = ''; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* CalculateDistance constructor. |
74
|
|
|
*/ |
75
|
|
|
public function __construct() |
76
|
|
|
{ |
77
|
14 |
|
$this->urlOptions = [ |
78
|
|
|
'origins' => '', |
79
|
14 |
|
'destinations' => '', |
80
|
14 |
|
'mode' => 'driving', |
81
|
|
|
'language' => 'en-EN', |
82
|
|
|
'sensor' => 'false', |
83
|
|
|
'key' => '', |
84
|
|
|
]; |
85
|
|
|
$this->setApiUrl(self::MAPS_DISTANCE_MATRIX_API_URL); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
14 |
|
* Set the ApiUrl |
90
|
|
|
* |
91
|
14 |
|
* @param string $api_url |
92
|
14 |
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function setApiUrl($api_url) |
95
|
|
|
{ |
96
|
|
|
$this->api_url = $api_url; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
14 |
|
|
101
|
|
|
/** |
102
|
14 |
|
* This is not required as the documentation states but if it becomes required |
103
|
|
|
* you can set it with this function. |
104
|
|
|
* |
105
|
|
|
* @param $googleAPIkey |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function setGoogleAPIkey($googleAPIkey) |
109
|
|
|
{ |
110
|
14 |
|
$this->googleAPIkey = $googleAPIkey; |
111
|
|
|
|
112
|
14 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Should we use SSL Verifier (Curl) yes or no. |
117
|
|
|
* |
118
|
|
|
* @param $disable_ssl_verifier |
119
|
|
|
* @return $this |
120
|
14 |
|
*/ |
121
|
|
|
public function setUseSslVerifier($disable_ssl_verifier) |
122
|
14 |
|
{ |
123
|
|
|
$this->disable_ssl_verifier = $disable_ssl_verifier; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the destination. |
130
|
14 |
|
* |
131
|
|
|
* @param string $destination |
132
|
14 |
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function setDestination($destination) |
135
|
|
|
{ |
136
|
|
|
$this->destination = $destination; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
14 |
|
|
141
|
|
|
/** |
142
|
14 |
|
* Set the source. |
143
|
|
|
* |
144
|
|
|
* @param $source |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function setSource($source) |
148
|
|
|
{ |
149
|
|
|
$this->source = $source; |
150
|
|
|
|
151
|
14 |
|
return $this; |
152
|
|
|
} |
153
|
14 |
|
|
154
|
14 |
|
/** |
155
|
14 |
|
* Return the destination. |
156
|
14 |
|
* |
157
|
14 |
|
* @return string |
158
|
14 |
|
*/ |
159
|
14 |
|
private function getDestination() |
160
|
14 |
|
{ |
161
|
14 |
|
return $this->destination; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get the API Url. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
14 |
|
public function getApiUrl() |
170
|
|
|
{ |
171
|
|
|
return $this->api_url; |
172
|
14 |
|
} |
173
|
14 |
|
|
174
|
14 |
|
/** |
175
|
14 |
|
* Are we using SSL verifier? |
176
|
|
|
* |
177
|
14 |
|
* @return bool |
178
|
14 |
|
*/ |
179
|
14 |
|
private function useSslVerifier() |
180
|
14 |
|
{ |
181
|
14 |
|
return $this->disable_ssl_verifier; |
182
|
14 |
|
} |
183
|
|
|
|
184
|
14 |
|
/** |
185
|
14 |
|
* Return the source. |
186
|
|
|
* |
187
|
14 |
|
* @return string |
188
|
14 |
|
*/ |
189
|
|
|
private function getSource() |
190
|
14 |
|
{ |
191
|
6 |
|
return $this->source; |
192
|
6 |
|
} |
193
|
|
|
|
194
|
8 |
|
/** |
195
|
|
|
* Return the url options. |
196
|
|
|
* |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
private function getUrlOptions() |
200
|
|
|
{ |
201
|
|
|
return $this->urlOptions; |
202
|
12 |
|
} |
203
|
|
|
|
204
|
12 |
|
/** |
205
|
12 |
|
* Execute the request to Google Services. |
206
|
6 |
|
* |
207
|
6 |
|
* @param string $url |
208
|
|
|
* @return mixed |
209
|
|
|
*/ |
210
|
6 |
|
private function requestData($url = "") |
211
|
|
|
{ |
212
|
|
|
$curl = curl_init(); |
213
|
|
|
curl_setopt_array($curl, [ |
214
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
215
|
|
|
CURLOPT_URL => $url, |
216
|
|
|
CURLOPT_USERAGENT => self::USER_AGENT, |
217
|
|
|
]); |
218
|
4 |
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->useSslVerifier()); |
219
|
|
|
$resp = curl_exec($curl); |
220
|
4 |
|
curl_close($curl); |
221
|
|
|
|
222
|
|
|
return utf8_encode($resp); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Query the distance from Google Services. |
227
|
|
|
* |
228
|
4 |
|
* @return mixed|null |
229
|
|
|
*/ |
230
|
4 |
|
private function calculateDistance() |
231
|
|
|
{ |
232
|
|
|
|
233
|
|
|
$data = [ |
234
|
|
|
'origins' => urlencode($this->getSource()), |
235
|
|
|
'destinations' => urlencode($this->getDestination()), |
236
|
|
|
'key' => $this->googleAPIkey, |
237
|
|
|
] + $this->getUrlOptions(); |
238
|
|
|
|
239
|
8 |
|
$request_string = ''; |
240
|
|
|
$cnt = 0; |
241
|
8 |
|
foreach ($data as $key => $val) { |
242
|
8 |
|
$request_string .= ($cnt > 0 ? '&' : '').$key.'='.$val; |
243
|
4 |
|
$cnt++; |
244
|
|
|
} |
245
|
4 |
|
|
246
|
|
|
$url = $this->getApiUrl().'?'.$request_string; |
247
|
|
|
$response = $this->requestData($url); |
248
|
|
|
|
249
|
|
|
$route = json_decode($response); |
250
|
|
|
|
251
|
|
|
if ($route && ($rows = current($route->rows))) { |
252
|
|
|
$elements = current($rows->elements); |
253
|
|
|
|
254
|
|
|
return $elements; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return null; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Return the distance calculated to Kilometers. |
262
|
|
|
* |
263
|
|
|
* @return float|int |
264
|
|
|
*/ |
265
|
|
|
public function getDistanceInKM() |
266
|
|
|
{ |
267
|
|
|
$route = $this->calculateDistance(); |
268
|
|
|
if (is_null($route) === false) { |
269
|
|
|
if (isset($route->distance->value)) { |
270
|
|
|
return round($route->distance->value / 1000); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return -1; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Return the distance calculated to Miles. |
279
|
|
|
* |
280
|
|
|
* @return float|int |
281
|
|
|
*/ |
282
|
|
|
public function getDistanceInMiles() |
283
|
|
|
{ |
284
|
|
|
return $this->convertResult(self::KM_TO_MILES_CONVERTER); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Return the distance calculated to Yards. |
289
|
|
|
* |
290
|
|
|
* @return float|int |
291
|
|
|
*/ |
292
|
|
|
public function getDistanceInYards() |
293
|
|
|
{ |
294
|
|
|
return $this->convertResult(self::KM_TO_YARD_CONVERTER); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* This function will do the sum and calculate the distance correctly. |
299
|
|
|
* |
300
|
|
|
* @param $type |
301
|
|
|
* @return float|int |
302
|
|
|
*/ |
303
|
|
|
private function convertResult($type) |
304
|
|
|
{ |
305
|
|
|
$result = $this->getDistanceInKM(); |
306
|
|
|
if ($result > -1) { |
307
|
|
|
return ($result * $type); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return $result; |
311
|
|
|
} |
312
|
|
|
} |