1 | <?php namespace Arcanedev\GeoLocation\Google\DistanceMatrix; |
||
15 | class DistanceMatrixService |
||
16 | { |
||
17 | /* ----------------------------------------------------------------- |
||
18 | | Constants |
||
19 | | ----------------------------------------------------------------- |
||
20 | */ |
||
21 | |||
22 | const BASE_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json'; |
||
23 | |||
24 | /* ----------------------------------------------------------------- |
||
25 | | Properties |
||
26 | | ----------------------------------------------------------------- |
||
27 | */ |
||
28 | |||
29 | /** @var \GuzzleHttp\ClientInterface */ |
||
30 | protected $client; |
||
31 | |||
32 | /** @var string|null */ |
||
33 | protected $key = null; |
||
34 | |||
35 | /** @var string */ |
||
36 | protected $language = 'en'; |
||
37 | |||
38 | /** @var string */ |
||
39 | protected $mode = 'driving'; |
||
40 | |||
41 | /** @var string */ |
||
42 | protected $units = 'metric'; |
||
43 | |||
44 | /* ----------------------------------------------------------------- |
||
45 | | Constructor |
||
46 | | ----------------------------------------------------------------- |
||
47 | */ |
||
48 | |||
49 | /** |
||
50 | * GoogleDistanceMatrix constructor. |
||
51 | * |
||
52 | * @param \GuzzleHttp\ClientInterface $client |
||
53 | */ |
||
54 | 21 | public function __construct(ClientInterface $client) |
|
59 | |||
60 | /* ----------------------------------------------------------------- |
||
61 | | Getters & Setters |
||
62 | | ----------------------------------------------------------------- |
||
63 | */ |
||
64 | |||
65 | /** |
||
66 | * Set the HTTP Client. |
||
67 | * |
||
68 | * @param \GuzzleHttp\ClientInterface $client |
||
69 | * |
||
70 | * @return self |
||
71 | */ |
||
72 | 21 | public function setHttpClient(ClientInterface $client) |
|
78 | |||
79 | /** |
||
80 | * Set the API Key. |
||
81 | * |
||
82 | * @param string $key |
||
83 | * |
||
84 | * @return self |
||
85 | */ |
||
86 | 21 | public function setKey($key) |
|
92 | |||
93 | /** |
||
94 | * Set the language. |
||
95 | * |
||
96 | * @param string $language |
||
97 | * |
||
98 | * @return self |
||
99 | */ |
||
100 | 3 | public function setLanguage($language) |
|
106 | |||
107 | /** |
||
108 | * Set the mode of transport. |
||
109 | * |
||
110 | * @param string $mode |
||
111 | * |
||
112 | * @return self |
||
113 | */ |
||
114 | 6 | public function setMode($mode) |
|
120 | |||
121 | /** |
||
122 | * Set the unit system. |
||
123 | * |
||
124 | * @param string $units |
||
125 | * |
||
126 | * @return self |
||
127 | */ |
||
128 | 6 | public function setUnits($units) |
|
134 | |||
135 | /* ----------------------------------------------------------------- |
||
136 | | Main Methods |
||
137 | | ----------------------------------------------------------------- |
||
138 | */ |
||
139 | |||
140 | /** |
||
141 | * Calculate the distance. |
||
142 | * |
||
143 | * @param \Arcanedev\GeoLocation\Contracts\Entities\Position $start |
||
144 | * @param \Arcanedev\GeoLocation\Contracts\Entities\Position $end |
||
145 | * @param array $options |
||
146 | * |
||
147 | * @return \Arcanedev\GeoLocation\Google\DistanceMatrix\DistanceMatrixResponse |
||
148 | */ |
||
149 | 12 | public function calculate(Position $start, Position $end, array $options = []) |
|
161 | |||
162 | /** |
||
163 | * Prepare the URL query. |
||
164 | * |
||
165 | * @param \Arcanedev\GeoLocation\Contracts\Entities\Position $start |
||
166 | * @param \Arcanedev\GeoLocation\Contracts\Entities\Position $end |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | 12 | private function prepareQuery(Position $start, Position $end) |
|
183 | |||
184 | /** |
||
185 | * Parse the position object. |
||
186 | * |
||
187 | * @param \Arcanedev\GeoLocation\Contracts\Entities\Position $position |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | 12 | private function parsePosition(Position $position) |
|
195 | |||
196 | /* ----------------------------------------------------------------- |
||
197 | | Other Methods |
||
198 | | ----------------------------------------------------------------- |
||
199 | */ |
||
200 | |||
201 | /** |
||
202 | * Check the mode of transport. |
||
203 | * |
||
204 | * @param string $mode |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | 6 | private function checkMode($mode) |
|
221 | |||
222 | /** |
||
223 | * Check the unit system. |
||
224 | * |
||
225 | * @param string $units |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 6 | private function checkUnits($units) |
|
242 | } |
||
243 |