From c20fc28d1089aa22761a9e3b3218c0164c41d8b5 Mon Sep 17 00:00:00 2001 From: Quinn Comendant Date: Sun, 30 Aug 2015 16:05:49 -0500 Subject: [PATCH] Nexmo's API is inconsitent when dealing with edge cases. I've had to relax the validation to avoid throwing exceptions during perfectly valid API responses. * In Account/Numbers.php we end validation if the count is zero, which can occur if the account has no numbers attached. Also, removed the validation for moHttpUrl because this value may not exist if a number has been added without one (in which case the account's default moHttpUrl will be used). * In Account/Pricing/Country.php the country parameter should always be uppercase (Nexmo will error if it is not). Also, some countries to not have a 'mt' price value set (e.g., BV) so we can't freak out if it is missing. --- src/Service/Account/Numbers.php | 7 ++++--- src/Service/Account/Pricing/Country.php | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Service/Account/Numbers.php b/src/Service/Account/Numbers.php index 8a86bc9..93acfe1 100644 --- a/src/Service/Account/Numbers.php +++ b/src/Service/Account/Numbers.php @@ -51,6 +51,10 @@ protected function validateResponse(array $json) if (!isset($json['count'])) { throw new Exception('count property expected'); } + if (0 == $json['count']) { + // If there are no numbers on the account, stop validating. + return; + } if (!isset($json['numbers']) || !is_array($json['numbers'])) { throw new Exception('numbers array property expected'); } @@ -67,9 +71,6 @@ protected function validateResponse(array $json) if (!isset($number['features']) || !is_array($number['features'])) { throw new Exception('number.features array property expected'); } - if (!isset($number['moHttpUrl'])) { - throw new Exception('number.moHttpUrl property expected'); - } } } } diff --git a/src/Service/Account/Pricing/Country.php b/src/Service/Account/Pricing/Country.php index d88ebb1..1fa60df 100644 --- a/src/Service/Account/Pricing/Country.php +++ b/src/Service/Account/Pricing/Country.php @@ -34,7 +34,8 @@ public function invoke($country = null) } return new Entity\Pricing($this->exec([ - 'country' => $country, + // Nexmo API requires $country value to be uppercase. + 'country' => strtoupper($country), ])); } @@ -43,6 +44,10 @@ public function invoke($country = null) */ protected function validateResponse(array $json) { + if (!isset($json['mt'])) { + // Some countries don't have any values, e.g., BV. + return; + } if (!isset($json['country'])) { throw new Exception('country property expected'); }