@@ -34,33 +34,32 @@ public function __construct(Common\HttpClient $HttpClient)
34
34
* @throws \MessageBird\Exceptions\RequestException
35
35
* @throws \MessageBird\Exceptions\ServerException
36
36
*/
37
- public function getList ($ countryCode , $ parameters = array ())
37
+ public function getList ($ countryCode , $ parameters = []): Objects \ BaseList
38
38
{
39
39
list ($ status , , $ body ) = $ this ->HttpClient ->performHttpRequest (
40
40
Common \HttpClient::REQUEST_GET ,
41
41
"available-phone-numbers/ $ countryCode " ,
42
42
$ parameters
43
43
);
44
44
45
- if ($ status === 200 ) {
46
- $ body = json_decode ($ body );
45
+ if ($ status !== 200 ) {
46
+ return $ this ->processRequest ($ body );
47
+ }
48
+ $ body = json_decode ($ body );
47
49
48
- $ items = $ body ->data ;
49
- unset($ body ->data );
50
+ $ items = $ body ->data ;
51
+ unset($ body ->data );
50
52
51
- $ baseList = new Objects \BaseList ();
52
- $ baseList ->loadFromArray ($ body );
53
+ $ baseList = new Objects \BaseList ();
54
+ $ baseList ->loadFromArray ($ body );
53
55
54
- foreach ($ items as $ item ) {
55
- $ object = new Objects \Number ($ this ->HttpClient );
56
+ foreach ($ items as $ item ) {
57
+ $ object = new Objects \Number ($ this ->HttpClient );
56
58
57
- $ itemObject = $ object ->loadFromArray ($ item );
58
- $ baseList ->items [] = $ itemObject ;
59
- }
60
- return $ baseList ;
59
+ $ itemObject = $ object ->loadFromArray ($ item );
60
+ $ baseList ->items [] = $ itemObject ;
61
61
}
62
-
63
- return $ this ->processRequest ($ body );
62
+ return $ baseList ;
64
63
}
65
64
66
65
/**
@@ -70,20 +69,20 @@ public function getList($countryCode, $parameters = array())
70
69
* @throws \MessageBird\Exceptions\RequestException
71
70
* @throws \MessageBird\Exceptions\ServerException
72
71
*/
73
- private function processRequest ($ body )
72
+ private function processRequest ($ body ): Objects \ Number
74
73
{
75
- $ body = @ json_decode ($ body );
74
+ $ body = json_decode ($ body );
76
75
77
- if ($ body === null or $ body === false ) {
76
+ if (json_last_error () ) {
78
77
throw new \MessageBird \Exceptions \ServerException ('Got an invalid JSON response from the server. ' );
79
78
}
80
79
81
- if (empty ($ body ->errors )) {
82
- return Objects \Number.loadFromArray ($ body ->data [0 ]);
80
+ if (!empty ($ body ->errors )) {
81
+ $ ResponseError = new Common \ResponseError ($ body );
82
+ throw new \MessageBird \Exceptions \RequestException ($ ResponseError ->getErrorString ());
83
83
}
84
84
85
- $ ResponseError = new Common \ResponseError ($ body );
86
- throw new \MessageBird \Exceptions \RequestException ($ ResponseError ->getErrorString ());
85
+ return Objects \Number.loadFromArray ($ body ->data [0 ]);
87
86
}
88
87
}
89
88
?>
0 commit comments