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