1
|
|
|
<?php namespace Arcanedev\GeoLocation\Google\DistanceMatrix; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoLocation\Contracts\Entities\Position; |
4
|
|
|
use Arcanedev\GeoLocation\Google\AbstractWebService; |
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DistanceMatrixService |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\GeoLocation\Google\DistanceMatrix |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @link https://developers.google.com/maps/documentation/distance-matrix/intro |
16
|
|
|
*/ |
17
|
|
|
class DistanceMatrixService extends AbstractWebService |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Constants |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
const BASE_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json'; |
25
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
27
|
|
|
| Properties |
28
|
|
|
| ----------------------------------------------------------------- |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $language = 'en'; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $mode = 'driving'; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
protected $units = 'metric'; |
39
|
|
|
|
40
|
|
|
/* ----------------------------------------------------------------- |
41
|
|
|
| Constructor |
42
|
|
|
| ----------------------------------------------------------------- |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* GoogleDistanceMatrix constructor. |
47
|
|
|
* |
48
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
49
|
|
|
*/ |
50
|
21 |
|
public function __construct(ClientInterface $client) |
51
|
|
|
{ |
52
|
21 |
|
parent::__construct($client); |
53
|
|
|
|
54
|
21 |
|
$this->setKey(getenv('GOOGLE_MAPS_DISTANCE_MATRIX_KEY')); |
55
|
21 |
|
} |
56
|
|
|
|
57
|
|
|
/* ----------------------------------------------------------------- |
58
|
|
|
| Getters & Setters |
59
|
|
|
| ----------------------------------------------------------------- |
60
|
|
|
*/ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the language. |
64
|
|
|
* |
65
|
|
|
* @param string $language |
66
|
|
|
* |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
3 |
|
public function setLanguage($language) |
70
|
|
|
{ |
71
|
3 |
|
$this->language = $language; |
72
|
|
|
|
73
|
3 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the mode of transport. |
78
|
|
|
* |
79
|
|
|
* @param string $mode |
80
|
|
|
* |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
6 |
|
public function setMode($mode) |
84
|
|
|
{ |
85
|
6 |
|
$this->mode = $this->checkMode($mode); |
86
|
|
|
|
87
|
3 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the unit system. |
92
|
|
|
* |
93
|
|
|
* @param string $units |
94
|
|
|
* |
95
|
|
|
* @return self |
96
|
|
|
*/ |
97
|
6 |
|
public function setUnits($units) |
98
|
|
|
{ |
99
|
6 |
|
$this->units = $this->checkUnits($units); |
100
|
|
|
|
101
|
3 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/* ----------------------------------------------------------------- |
105
|
|
|
| Main Methods |
106
|
|
|
| ----------------------------------------------------------------- |
107
|
|
|
*/ |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Calculate the distance. |
111
|
|
|
* |
112
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Position $start |
113
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Position $end |
114
|
|
|
* @param array $options |
115
|
|
|
* |
116
|
|
|
* @return \Arcanedev\GeoLocation\Google\DistanceMatrix\DistanceMatrixResponse |
117
|
|
|
*/ |
118
|
12 |
|
public function calculate(Position $start, Position $end, array $options = []) |
119
|
|
|
{ |
120
|
12 |
|
$url = static::BASE_URL.'?'.$this->prepareQuery([ |
121
|
12 |
|
'origins' => $this->parsePosition($start), |
122
|
12 |
|
'destinations' => $this->parsePosition($end), |
123
|
4 |
|
]); |
124
|
|
|
|
125
|
12 |
|
return $this->get($url, $options); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/* ----------------------------------------------------------------- |
129
|
|
|
| Other Methods |
130
|
|
|
| ----------------------------------------------------------------- |
131
|
|
|
*/ |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the default query params. |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
12 |
|
protected function getDefaultQueryParams() |
139
|
|
|
{ |
140
|
|
|
return [ |
141
|
12 |
|
'mode' => $this->mode, |
142
|
12 |
|
'language' => $this->language, |
143
|
12 |
|
'units' => $this->units, |
144
|
12 |
|
'key' => $this->key, |
145
|
4 |
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Prepare the response. |
150
|
|
|
* |
151
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
152
|
|
|
* |
153
|
|
|
* @return \Arcanedev\GeoLocation\Google\DistanceMatrix\DistanceMatrixResponse |
154
|
|
|
*/ |
155
|
12 |
|
protected function prepareResponse(ResponseInterface $response) |
156
|
|
|
{ |
157
|
12 |
|
return new DistanceMatrixResponse( |
158
|
12 |
|
json_decode($response->getBody(), true) |
159
|
4 |
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Check the mode of transport. |
164
|
|
|
* |
165
|
|
|
* @param string $mode |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
6 |
|
private function checkMode($mode) |
170
|
|
|
{ |
171
|
6 |
|
$mode = strtolower($mode); |
172
|
|
|
|
173
|
6 |
|
if ( ! in_array($mode, ['driving', 'walking', 'bicycling', 'transit'])) { |
174
|
3 |
|
throw new InvalidArgumentException( |
175
|
3 |
|
"The given mode of transport [{$mode}] is invalid. Please check the supported travel modes: ". |
176
|
2 |
|
"https://developers.google.com/maps/documentation/distance-matrix/intro#travel_modes" |
177
|
1 |
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
3 |
|
return $mode; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Check the unit system. |
185
|
|
|
* |
186
|
|
|
* @param string $units |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
6 |
|
private function checkUnits($units) |
191
|
|
|
{ |
192
|
6 |
|
$units = strtolower($units); |
193
|
|
|
|
194
|
6 |
|
if ( ! in_array($units, ['metric', 'imperial'])) { |
195
|
3 |
|
throw new InvalidArgumentException( |
196
|
3 |
|
"The given unit system [{$units}] is invalid. Please check the supported units: ". |
197
|
2 |
|
"https://developers.google.com/maps/documentation/distance-matrix/intro#unit_systems" |
198
|
1 |
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
3 |
|
return $units; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|