diff --git a/src/Balikobot/Balikobot.php b/src/Balikobot/Balikobot.php index 0815c3f..f3bde24 100644 --- a/src/Balikobot/Balikobot.php +++ b/src/Balikobot/Balikobot.php @@ -432,6 +432,7 @@ class Balikobot { */ const EXCEPTION_INVALID_REQUEST = 400, /*< Invalid request */ + EXCEPTION_NOT_SUPPORTED = 401, /*< Not supported */ EXCEPTION_SERVER_ERROR = 500; /*< Unexpected response from the server */ @@ -711,6 +712,8 @@ public function getServices($shipper) { $response = $this->call(self::REQUEST_SERVICES, $shipper); + if (isset($response['status']) && ($response['status'] == 409)) + throw new \InvalidArgumentException("The $shipper shipper is not supported.", self::EXCEPTION_NOT_SUPPORTED); if (!isset($response['status']) || ($response['status'] != 200)) { $code = isset($response['status']) ? $response['status'] : 0; throw new \UnexpectedValueException("Unexpected server response, code = $code.", self::EXCEPTION_SERVER_ERROR); @@ -731,6 +734,8 @@ public function getManipulationUnits($shipper) { $response = $this->call(self::REQUEST_MANIPULATIONUNITS, $shipper); + if (isset($response['status']) && ($response['status'] == 409)) + throw new \InvalidArgumentException("The $shipper shipper is not supported.", self::EXCEPTION_NOT_SUPPORTED); if (!isset($response['status']) || ($response['status'] != 200)) { $code = isset($response['status']) ? $response['status'] : 0; throw new \UnexpectedValueException("Unexpected server response, code = $code.", self::EXCEPTION_SERVER_ERROR); @@ -762,6 +767,8 @@ public function getBranches($shipper, $service = null, $full = false) { $response = $this->call($full ? self::REQUEST_FULLBRANCHES : self::REQUEST_BRANCHES, $shipper, [], $service); + if (isset($response['status']) && ($response['status'] == 409)) + throw new \InvalidArgumentException("The $shipper shipper is not supported.", self::EXCEPTION_NOT_SUPPORTED); if (!isset($response['status']) || ($response['status'] != 200)) { $code = isset($response['status']) ? $response['status'] : 0; throw new \UnexpectedValueException("Unexpected server response, code = $code.", self::EXCEPTION_SERVER_ERROR); @@ -798,6 +805,8 @@ public function getCountriesForService($shipper) { $response = $this->call(self::REQUEST_COUNTRIES4SERVICE, $shipper); + if (isset($response['status']) && ($response['status'] == 409)) + throw new \InvalidArgumentException("The $shipper shipper is not supported.", self::EXCEPTION_NOT_SUPPORTED); if (!isset($response['status']) || ($response['status'] != 200)) { $code = isset($response['status']) ? $response['status'] : 0; throw new \UnexpectedValueException("Unexpected server response, code = $code.", self::EXCEPTION_SERVER_ERROR); @@ -830,6 +839,8 @@ public function getZipCodes($shipper, $service, $country = self::COUNTRY_CZECHIA $response = $this->call(self::REQUEST_ZIPCODES, $shipper, [], "$service/$country"); + if (isset($response['status']) && ($response['status'] == 409)) + throw new \InvalidArgumentException("The $shipper shipper is not supported.", self::EXCEPTION_NOT_SUPPORTED); if (!isset($response['status']) || ($response['status'] != 200)) { $code = isset($response['status']) ? $response['status'] : 0; throw new \UnexpectedValueException("Unexpected server response, code = $code.", self::EXCEPTION_SERVER_ERROR); @@ -1305,8 +1316,6 @@ private function call($request, $shipper, array $data = [], $url = null) { if (empty($request) || empty ($shipper)) throw new \InvalidArgumentException('Invalid argument has been entered.'); - //printf("\nRequest: $this->apiUrl/$shipper/$request\n"); - $r = curl_init(); curl_setopt($r, CURLOPT_URL, $url ? "$this->apiUrl/$shipper/$request/$url" : "$this->apiUrl/$shipper/$request"); curl_setopt($r, CURLOPT_RETURNTRANSFER, true); @@ -1321,7 +1330,7 @@ private function call($request, $shipper, array $data = [], $url = null) { ]); $response = curl_exec($r); curl_close($r); - //var_dump($response); + return json_decode($response, true); } diff --git a/test/balikobot.php b/test/balikobot.php index 04c182b..53d4070 100644 --- a/test/balikobot.php +++ b/test/balikobot.php @@ -22,7 +22,15 @@ if ($testGetters) { printf("Shippers & its services\n"); foreach ($shippers as $itemShippers) { - printf("%s: %s\n", $itemShippers, arrayToString($b->getServices($itemShippers))); + try { + printf("%s: %s\n", $itemShippers, arrayToString($b->getServices($itemShippers))); + } catch (\InvalidArgumentException $e) { + if ($e->getCode() == Balikobot::EXCEPTION_NOT_SUPPORTED) { + printf("%s: %s\n", $itemShippers, 'Not supported'); + } else { + throw $e; + } + } } printf("\n\n"); @@ -39,19 +47,36 @@ // shippers + services printf("Countries For Service\n"); foreach ($shippers as $itemShippers) { - printf("%s: %s\n", $itemShippers, arrayToString($b->getCountriesForService($itemShippers))); + try { + printf("%s: %s\n", $itemShippers, arrayToString($b->getCountriesForService($itemShippers))); + } catch (\InvalidArgumentException $e) { + if ($e->getCode() == Balikobot::EXCEPTION_NOT_SUPPORTED) { + printf("%s: %s\n", $itemShippers, 'Not supported'); + } else { + throw $e; + } + } } printf("\n\n"); // branches printf("Branches\n"); foreach ($shippers as $itemShippers) { - $services = $b->getServices($itemShippers); - foreach ($services as $servicesKey => $servicesItem) { - try { - printf("%s:%s: %s\n", $itemShippers, $servicesKey, arrayToString($b->getBranches($itemShippers, $servicesKey))); - } catch (\Exception $e) { - printf("\nException: %d: %s\n", $e->getCode(), $e->getMessage()); + try { + $services = $b->getServices($itemShippers); + + foreach ($services as $servicesKey => $servicesItem) { + try { + printf("%s:%s: %s\n", $itemShippers, $servicesKey, arrayToString($b->getBranches($itemShippers, $servicesKey))); + } catch (\Exception $e) { + printf("\nException: %d: %s\n", $e->getCode(), $e->getMessage()); + } + } + } catch (\InvalidArgumentException $e) { + if ($e->getCode() == Balikobot::EXCEPTION_NOT_SUPPORTED) { + printf("%s: %s\n", $itemShippers, 'Not supported'); + } else { + throw $e; } } } @@ -60,12 +85,21 @@ // zip printf("Zip\n"); foreach ($shippers as $itemShippers) { - $services = $b->getServices($itemShippers); - foreach ($services as $servicesKey => $servicesItem) { - try { - printf("%s:%s: %s\n", $itemShippers, $servicesKey, arrayToString($b->getZipCodes($itemShippers, $servicesKey))); - } catch (\Exception $e) { - printf("\nException: %d: %s\n", $e->getCode(), $e->getMessage()); + try { + $services = $b->getServices($itemShippers); + + foreach ($services as $servicesKey => $servicesItem) { + try { + printf("%s:%s: %s\n", $itemShippers, $servicesKey, arrayToString($b->getZipCodes($itemShippers, $servicesKey))); + } catch (\Exception $e) { + printf("\nException: %d: %s\n", $e->getCode(), $e->getMessage()); + } + } + } catch (\InvalidArgumentException $e) { + if ($e->getCode() == Balikobot::EXCEPTION_NOT_SUPPORTED) { + printf("%s: %s\n", $itemShippers, 'Not supported'); + } else { + throw $e; } } } @@ -86,7 +120,15 @@ // manipulation units printf("Manipulation units\n"); foreach ($shippers as $itemShippers) { - printf("%s: %s\n", $itemShippers, arrayToString($b->getManipulationUnits($itemShippers))); + try { + printf("%s: %s\n", $itemShippers, arrayToString($b->getManipulationUnits($itemShippers))); + } catch (\InvalidArgumentException $e) { + if ($e->getCode() == Balikobot::EXCEPTION_NOT_SUPPORTED) { + printf("%s: %s\n", $itemShippers, 'Not supported'); + } else { + throw $e; + } + } } printf("\n\n"); }