diff --git a/src/Exceptions.php b/src/Exceptions.php deleted file mode 100755 index f239656..0000000 --- a/src/Exceptions.php +++ /dev/null @@ -1,8 +0,0 @@ -validate(); + $this->validateRequest(); $data = [ 'key' => $this->apiKey, 'language' => $this->language, @@ -224,11 +226,9 @@ public function sendRequest() $parameters = http_build_query($data); $url = self::URL.'?'.$parameters; $response = $this->request('GET', $url); - if ($response->getStatusCode() != 200) { - throw new \Exception('Response with status code '.$response->getStatusCode()); - } - return json_decode(($response->getBody()->getContents())); + + return $response; } /** @@ -240,16 +240,52 @@ private function request($type = 'GET', $url) { $client = new Client(); $response = $client->request($type, $url); - return $response; + + if ($response->getStatusCode() != 200) { + throw new \Exception('Response with status code '.$response->getStatusCode()); + } + + $responseObject = new GoogleDistanceMatrixResponse(json_decode($response->getBody()->getContents())); + + $this->validateResponse($responseObject); + + return $responseObject; + } + + private function validateResponse(GoogleDistanceMatrixResponse $response) + { + + switch ($response->getStatus()) { + case GoogleDistanceMatrixResponse::RESPONSE_STATUS_OK: + break; + case GoogleDistanceMatrixResponse::RESPONSE_STATUS_INVALID_REQUEST: + throw new Exception\ResponseException("Invalid request.", 1); + break; + case GoogleDistanceMatrixResponse::RESPONSE_STATUS_MAX_ELEMENTS_EXCEEDED: + throw new Exception\ResponseException("The product of the origin and destination exceeds the limit per request.", 2); + break; + case GoogleDistanceMatrixResponse::RESPONSE_STATUS_OVER_QUERY_LIMIT: + throw new Exception\ResponseException("The service has received too many requests from your application in the allowed time range.", 3); + break; + case GoogleDistanceMatrixResponse::RESPONSE_STATUS_REQUEST_DENIED: + throw new Exception\ResponseException("The service denied the use of the Distance Matrix API service by your application.", 4); + break; + case GoogleDistanceMatrixResponse::RESPONSE_STATUS_UNKNOWN_ERROR: + throw new Exception\ResponseException("Unknown error.", 5); + break; + default: + throw new Exception\ResponseException("Unknown status code.", 6); + break; + } } - private function validate() + private function validateRequest() { if (empty($this->getOrigin())) { - throw new Exception('Origin must be set.'); + throw new Exception\OriginException('Origin must be set.'); } if (empty($this->getDestination())) { - throw new Exception('Destination must be set.'); + throw new Exception\DestinationException('Destination must be set.'); } } } diff --git a/src/Response/Address.php b/src/Response/Address.php new file mode 100644 index 0000000..6be3004 --- /dev/null +++ b/src/Response/Address.php @@ -0,0 +1,29 @@ +address = $address; + } + + /** + * @return string + */ + public function __toString() + { + return $this->address; + } +} \ No newline at end of file diff --git a/src/Response/Distance.php b/src/Response/Distance.php new file mode 100644 index 0000000..c03d445 --- /dev/null +++ b/src/Response/Distance.php @@ -0,0 +1,44 @@ +text = $text; + $this->value = $value; + } + + public function __toString() + { + return $this->text; + } + + /** + * @return mixed + */ + public function getText() + { + return $this->text; + } + + /** + * @return mixed + */ + public function getValue() + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Response/Duration.php b/src/Response/Duration.php new file mode 100644 index 0000000..9031ffc --- /dev/null +++ b/src/Response/Duration.php @@ -0,0 +1,43 @@ +text = $text; + $this->value = $value; + } + + public function __toString() + { + return $this->text; + } + + /** + * @return mixed + */ + public function getText() + { + return $this->text; + } + + /** + * @return mixed + */ + public function getValue() + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Response/Element.php b/src/Response/Element.php new file mode 100644 index 0000000..d79e01e --- /dev/null +++ b/src/Response/Element.php @@ -0,0 +1,67 @@ +status = $status; + $this->duration = $duration; + $this->distance = $distance; + } + + /** + * @return mixed + */ + public function getStatus() + { + return $this->status; + } + + /** + * @return Duration + */ + public function getDuration() + { + return $this->duration; + } + + /** + * @return Distance + */ + public function getDistance() + { + return $this->distance; + } +} \ No newline at end of file