1 | <?php namespace Arcanedev\GeoLocation\Google\DistanceMatrix; |
||
17 | class DistanceMatrixService extends AbstractService |
||
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 | public function __construct(ClientInterface $client) |
||
51 | { |
||
52 | parent::__construct($client); |
||
53 | |||
54 | 21 | $this->setKey(getenv('GOOGLE_MAPS_DISTANCE_MATRIX_KEY')); |
|
55 | } |
||
56 | 21 | ||
57 | 21 | /* ----------------------------------------------------------------- |
|
58 | 21 | | Getters & Setters |
|
59 | | ----------------------------------------------------------------- |
||
60 | */ |
||
61 | |||
62 | /** |
||
63 | * Set the language. |
||
64 | * |
||
65 | * @param string $language |
||
66 | * |
||
67 | * @return self |
||
68 | */ |
||
69 | public function setLanguage($language) |
||
70 | { |
||
71 | $this->language = $language; |
||
72 | 21 | ||
73 | return $this; |
||
74 | 21 | } |
|
75 | |||
76 | 21 | /** |
|
77 | * Set the mode of transport. |
||
78 | * |
||
79 | * @param string $mode |
||
80 | * |
||
81 | * @return self |
||
82 | */ |
||
83 | public function setMode($mode) |
||
84 | { |
||
85 | $this->mode = $this->checkMode($mode); |
||
86 | 21 | ||
87 | return $this; |
||
88 | 21 | } |
|
89 | |||
90 | 21 | /** |
|
91 | * Set the unit system. |
||
92 | * |
||
93 | * @param string $units |
||
94 | * |
||
95 | * @return self |
||
96 | */ |
||
97 | public function setUnits($units) |
||
98 | { |
||
99 | $this->units = $this->checkUnits($units); |
||
100 | 3 | ||
101 | return $this; |
||
102 | 3 | } |
|
103 | |||
104 | 3 | /* ----------------------------------------------------------------- |
|
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 | 6 | * @param array $options |
|
115 | * |
||
116 | 6 | * @return \Arcanedev\GeoLocation\Google\DistanceMatrix\DistanceMatrixResponse |
|
117 | */ |
||
118 | 3 | public function calculate(Position $start, Position $end, array $options = []) |
|
119 | { |
||
120 | $url = static::BASE_URL.$this->prepareQuery([ |
||
121 | 'origins' => $this->parsePosition($start), |
||
122 | 'destinations' => $this->parsePosition($end), |
||
123 | ]); |
||
124 | |||
125 | return $this->get($url, $options); |
||
126 | } |
||
127 | |||
128 | 6 | /* ----------------------------------------------------------------- |
|
129 | | Other Methods |
||
130 | 6 | | ----------------------------------------------------------------- |
|
131 | */ |
||
132 | 3 | ||
133 | /** |
||
134 | * Get the default query params. |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | protected function getDefaultQueryParams() |
||
139 | { |
||
140 | return [ |
||
141 | 'mode' => $this->mode, |
||
142 | 'language' => $this->language, |
||
143 | 'units' => $this->units, |
||
144 | 'key' => $this->key, |
||
145 | ]; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | 12 | * Prepare the response. |
|
150 | * |
||
151 | * @param \Psr\Http\Message\ResponseInterface $response |
||
152 | * |
||
153 | 12 | * @return \Arcanedev\GeoLocation\Google\DistanceMatrix\DistanceMatrixResponse |
|
154 | */ |
||
155 | 12 | protected function prepareResponse(ResponseInterface $response) |
|
161 | |||
162 | /** |
||
163 | * Check the mode of transport. |
||
164 | * |
||
165 | * @param string $mode |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | private function checkMode($mode) |
||
182 | |||
183 | /** |
||
184 | * Check the unit system. |
||
185 | * |
||
186 | * @param string $units |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | private function checkUnits($units) |
||
203 | } |
||
204 |