1
|
|
|
<?php |
2
|
|
|
namespace Redbox\Distance; |
3
|
|
|
|
4
|
|
|
class CalculateDistance |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
const MAPS_DISTANCE_MATRIX_API_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json'; |
8
|
|
|
const KM_TO_MILES_CONVERTER = 0.62137; |
9
|
|
|
const KM_TO_YARD_CONVERTER = 1093.6133; |
10
|
|
|
const CURRENT_VERSION = '1.0'; |
11
|
|
|
const USER_AGENT = 'Calculate Distance V'; |
12
|
|
|
|
13
|
|
|
protected $source = ''; |
14
|
|
|
protected $destination = ''; |
15
|
|
|
protected $googleAPIkey = ''; |
16
|
|
|
protected $urlOptions = []; |
17
|
|
|
protected $disable_ssl_verifier = true; |
18
|
|
|
protected $api_url = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* CalculateDistance constructor. |
22
|
|
|
*/ |
23
|
|
|
public function __construct() { |
24
|
|
|
$this->urlOptions = array( |
25
|
|
|
'origins' => '', |
26
|
|
|
'destinations' => '', |
27
|
|
|
'mode' => 'driving', |
28
|
|
|
'language' => 'en-EN', |
29
|
|
|
'sensor' => 'false', |
30
|
|
|
'key' => '', |
31
|
|
|
); |
32
|
|
|
$this->setApiUrl(self::MAPS_DISTANCE_MATRIX_API_URL); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $api_url |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function setApiUrl($api_url) |
40
|
|
|
{ |
41
|
|
|
$this->api_url = $api_url; |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $googleAPIkey |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function setGoogleAPIkey($googleAPIkey) |
50
|
|
|
{ |
51
|
|
|
$this->googleAPIkey = $googleAPIkey; |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $disable_ssl_verifier |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function setUseSslVerifier($disable_ssl_verifier) |
60
|
|
|
{ |
61
|
|
|
$this->disable_ssl_verifier = $disable_ssl_verifier; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $destination |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function setDestination($destination) |
70
|
|
|
{ |
71
|
|
|
$this->destination = $destination; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $source |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function setSource($source) |
80
|
|
|
{ |
81
|
|
|
$this->source = $source; |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
private function getDestination() |
89
|
|
|
{ |
90
|
|
|
return $this->destination; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getApiUrl() |
97
|
|
|
{ |
98
|
|
|
return $this->api_url; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
private function useSslVerifier() |
105
|
|
|
{ |
106
|
|
|
return $this->disable_ssl_verifier; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
private function getSource() |
113
|
|
|
{ |
114
|
|
|
return $this->source; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
private function getUrlOptions() |
121
|
|
|
{ |
122
|
|
|
return $this->urlOptions; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $url |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
private function requestData($url="") |
130
|
|
|
{ |
131
|
|
|
$curl = curl_init(); |
132
|
|
|
curl_setopt_array($curl, array( |
133
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
134
|
|
|
CURLOPT_URL => $url, |
135
|
|
|
CURLOPT_USERAGENT => self::USER_AGENT.self::CURRENT_VERSION, |
136
|
|
|
)); |
137
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->useSslVerifier()); |
138
|
|
|
$resp = curl_exec($curl); |
139
|
|
|
return $resp; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return mixed|null |
144
|
|
|
*/ |
145
|
|
|
private function calculateDistance() |
146
|
|
|
{ |
147
|
|
|
|
148
|
|
|
$data = $this->getUrlOptions(); |
149
|
|
|
$data['origins'] = urlencode($this->getSource()); |
150
|
|
|
$data['destinations'] = urlencode($this->getDestination()); |
151
|
|
|
$data['key'] = $this->googleAPIkey; |
152
|
|
|
|
153
|
|
|
$request_string = ''; |
154
|
|
|
$cnt = 0; |
155
|
|
|
foreach($data as $key => $val) { |
156
|
|
|
if ($cnt > 0) { |
157
|
|
|
$request_string .= '&'; |
158
|
|
|
} |
159
|
|
|
$request_string .= $key.'='.$val; |
160
|
|
|
$cnt++; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$url = $this->getApiUrl().'?' . $request_string; |
164
|
|
|
$response = $this->requestData($url); |
165
|
|
|
|
166
|
|
|
$response = utf8_encode($response); |
167
|
|
|
$route = json_decode($response); |
168
|
|
|
|
169
|
|
|
if ($route) { |
170
|
|
|
$rows = current($route->rows); |
171
|
|
|
if (is_null($rows) === false) { |
172
|
|
|
$elements = current($rows->elements); |
173
|
|
|
return $elements; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
return NULL; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return float|int |
181
|
|
|
*/ |
182
|
|
|
public function getDistanceInKM() |
183
|
|
|
{ |
184
|
|
|
$route = $this->calculateDistance(); |
185
|
|
|
if( is_null($route) === FALSE) { |
186
|
|
|
if(isset($route->distance->value)){ |
187
|
|
|
return round($route->distance->value/1000); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
return -1; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return float|int |
195
|
|
|
*/ |
196
|
|
|
public function getDistanceInMiles() |
197
|
|
|
{ |
198
|
|
|
return $this->convertResult(self::KM_TO_MILES_CONVERTER); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return float|int |
203
|
|
|
*/ |
204
|
|
|
public function getDistanceInYards() |
205
|
|
|
{ |
206
|
|
|
return $this->convertResult(self::KM_TO_YARD_CONVERTER); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param $type |
211
|
|
|
* @return float|int |
212
|
|
|
*/ |
213
|
|
|
private function convertResult($type) |
214
|
|
|
{ |
215
|
|
|
$result = $this->getDistanceInKM(); |
216
|
|
|
if ($result > -1) { |
217
|
|
|
return ($result * $type); |
218
|
|
|
} |
219
|
|
|
return $result; |
220
|
|
|
} |
221
|
|
|
} |