diff --git a/README.md b/README.md index c0019939..2d28dc4b 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,13 @@ Installation ####Composer installation - [Download composer](https://getcomposer.org/doc/00-intro.md#installation-nix) -- Add the `"messagebird/php-rest-api": "1.2.x"` into the `require` section of your `composer.json`. +- Add the `"messagebird/php-rest-api": "1.3.x"` into the `require` section of your `composer.json`. - Run `composer install`. ``` { "require": { - "messagebird/php-rest-api": "1.2.x" + "messagebird/php-rest-api": "1.3.x" } } ``` diff --git a/composer.json b/composer.json index 8c94f531..162085af 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "messagebird/php-rest-api", "description": "MessageBird REST API client for PHP", - "version": "1.2.0", + "version": "1.3.0", "type": "library", "homepage": "https://github.com/messagebird/php-rest-api", "license": "BSD-2-Clause", diff --git a/examples/lookup-hlr-view.php b/examples/lookup-hlr-view.php new file mode 100644 index 00000000..db0e774e --- /dev/null +++ b/examples/lookup-hlr-view.php @@ -0,0 +1,35 @@ +msisdn = '31624971134'; + $Hlr->reference = "yoloswag3001"; + + // create a new hlr request + $hlr = $MessageBird->lookupHlr->create($Hlr); + var_dump($hlr); + + // pool for the results + $poolCount = 10; + while($poolCount--) { + $hlr = $MessageBird->lookupHlr->read($Hlr->msisdn); + if ($hlr->status != \MessageBird\Objects\Hlr::STATUS_SENT) { + // we have something + var_dump($hlr); + break; + } + sleep(0.5); + } + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + var_dump($e->getMessage()); + +} diff --git a/examples/lookup-view.php b/examples/lookup-view.php new file mode 100644 index 00000000..ea9c9db1 --- /dev/null +++ b/examples/lookup-view.php @@ -0,0 +1,20 @@ +lookup->read(31624971134); + var_dump($Lookup); + + $Lookup = $MessageBird->lookup->read("624971134", "NL"); + var_dump($Lookup); +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + var_dump($e->getMessage()); + +} diff --git a/src/MessageBird/Client.php b/src/MessageBird/Client.php index d20516d2..1397ba4f 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -12,7 +12,7 @@ class Client const ENDPOINT = 'https://rest.messagebird.com'; - const CLIENT_VERSION = '1.2.0'; + const CLIENT_VERSION = '1.3.0'; /** * @var string @@ -44,6 +44,16 @@ class Client */ public $balance; + /** + * @var Resources\Lookup + */ + public $lookup; + + /** + * @var Resources\LookupHlr + */ + public $lookupHlr; + /** * @var Common\HttpClient */ @@ -72,6 +82,8 @@ public function __construct($accessKey = null, Common\HttpClient $httpClient = n $this->verify = new Resources\Verify($this->HttpClient); $this->balance = new Resources\Balance($this->HttpClient); $this->voicemessages = new Resources\VoiceMessage($this->HttpClient); + $this->lookup = new Resources\Lookup($this->HttpClient); + $this->lookupHlr = new Resources\LookupHlr($this->HttpClient); } /** diff --git a/src/MessageBird/Objects/Lookup.php b/src/MessageBird/Objects/Lookup.php new file mode 100644 index 00000000..34c8429a --- /dev/null +++ b/src/MessageBird/Objects/Lookup.php @@ -0,0 +1,153 @@ +href; + } + + /** + * Get the href + * + * @return mixed + */ + public function getCountryCode() + { + return $this->countryCode; + } + + /** + * Get the href + * + * @return mixed + */ + public function getCountryPrefix() + { + return $this->countryPrefix; + } + + /** + * Get the href + * + * @return mixed + */ + public function getPhoneNumber() + { + return $this->phoneNumber; + } + + /** + * Get the href + * + * @return mixed + */ + public function getType() + { + return $this->type; + } + + /** + * Get the href + * + * @return mixed + */ + public function getFormats() + { + return $this->formats; + } + + /** + * Get the href + * + * @return mixed + */ + public function getHLR() + { + return $this->hlr; + } +} diff --git a/src/MessageBird/Objects/Message.php b/src/MessageBird/Objects/Message.php index b5c3c2c1..47663f18 100644 --- a/src/MessageBird/Objects/Message.php +++ b/src/MessageBird/Objects/Message.php @@ -89,7 +89,7 @@ class Message extends Base public $gateway; /** - * A hash with extra information. Is only used when a binary or premium + * An associative array with extra information. Is only used when a binary or premium * message is sent. * * @var array diff --git a/src/MessageBird/Objects/Verify.php b/src/MessageBird/Objects/Verify.php index a43db6fc..6467f4ca 100644 --- a/src/MessageBird/Objects/Verify.php +++ b/src/MessageBird/Objects/Verify.php @@ -46,7 +46,7 @@ class Verify extends Base public $reference; /** - * A hash containing one href entry referring to the URL of the created object. + * An associative array containing one href entry referring to the URL of the created object. * The entry can either refer to either the messages or the voicemessages endpoint * * @var object diff --git a/src/MessageBird/Resources/Lookup.php b/src/MessageBird/Resources/Lookup.php new file mode 100644 index 00000000..906a05f5 --- /dev/null +++ b/src/MessageBird/Resources/Lookup.php @@ -0,0 +1,50 @@ +setObject(new Objects\Lookup); + $this->setResourceName('lookup'); + + parent::__construct($HttpClient); + } + + /** + * @param $phoneNumber + * + * @return $this->Object + * + * @throws \MessageBird\Exceptions\HttpException + * @throws \MessageBird\Exceptions\RequestException + * @throws \MessageBird\Exceptions\ServerException + */ + public function read($phoneNumber = null, $countryCode = null) + { + if(empty($phoneNumber)) { + throw new InvalidArgumentException('The phone number cannot be empty.'); + } + $query = null; + if ($countryCode != null) { + $query = array("countryCode" => $countryCode); + } + $ResourceName = $this->resourceName . '/' . $phoneNumber; + list(, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, $ResourceName, $query); + return $this->processRequest($body); + } +} diff --git a/src/MessageBird/Resources/LookupHlr.php b/src/MessageBird/Resources/LookupHlr.php new file mode 100644 index 00000000..9b2a13ac --- /dev/null +++ b/src/MessageBird/Resources/LookupHlr.php @@ -0,0 +1,77 @@ +setObject(new Objects\HLR); + $this->setResourceName('lookup'); + + parent::__construct($HttpClient); + } + + /** + * @param $phoneNumber + * + * @return $this->Object + * + * @throws \MessageBird\Exceptions\HttpException + * @throws \MessageBird\Exceptions\RequestException + * @throws \MessageBird\Exceptions\ServerException + */ + public function create($hlr, $countryCode = null) + { + if(empty($hlr->msisdn)) { + throw new InvalidArgumentException('The phone number ($hlr->msisdn) cannot be empty.'); + } + + $query = null; + if ($countryCode != null) { + $query = array("countryCode" => $countryCode); + } + $ResourceName = $this->resourceName . '/' . ($hlr->msisdn) . '/hlr' ; + list(, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_POST, $ResourceName, $query, json_encode($hlr)); + return $this->processRequest($body); + } + + /** + * @param $phoneNumber + * + * @return $this->Object + * + * @throws \MessageBird\Exceptions\HttpException + * @throws \MessageBird\Exceptions\RequestException + * @throws \MessageBird\Exceptions\ServerException + */ + public function read($phoneNumber = null, $countryCode = null) + { + if(empty($phoneNumber)) { + throw new InvalidArgumentException('The phone number cannot be empty.'); + } + + $query = null; + if ($countryCode != null) { + $query = array("countryCode" => $countryCode); + } + $ResourceName = $this->resourceName . '/' . $phoneNumber . '/hlr' ; + list(, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, $ResourceName, $query, null); + return $this->processRequest($body); + } + + +} diff --git a/tests/integration/lookup/LookupTest.php b/tests/integration/lookup/LookupTest.php new file mode 100644 index 00000000..4e14685c --- /dev/null +++ b/tests/integration/lookup/LookupTest.php @@ -0,0 +1,104 @@ +client = new \MessageBird\Client('YOUR_ACCESS_KEY', $this->mockClient); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadLookup() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'lookup/31612345678', null, null); + $this->client->lookup->read(31612345678); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testReadLookupWithEmptyNumber() + { + $this->client->lookup->read(null); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadLookupWithCountryCode() + { + $params = array("countryCode" => "NL"); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'lookup/612345678', $params, null); + $this->client->lookup->read(612345678, $params["countryCode"]); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testCreateLookupHlr() + { + $Hlr = new \MessageBird\Objects\Hlr(); + $Hlr->msisdn = 31612345678; + $Hlr->reference = 'Yoloswag3007'; + + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'lookup/'.$Hlr->msisdn.'/hlr', null, json_encode($Hlr)); + + $this->client->lookupHlr->create($Hlr); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testCreateLookupHlrWithEmptyNumber() + { + $Hlr = new \MessageBird\Objects\Hlr(); + $Hlr->msisdn = null; + $this->client->lookupHlr->create($Hlr); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testCreateLookupHlrWithCountryCode() + { + $Hlr = new \MessageBird\Objects\Hlr(); + $Hlr->msisdn = 612345678; + $Hlr->reference = "CoolReference"; + + $params = array("countryCode" => "NL"); + + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'lookup/'.$Hlr->msisdn.'/hlr', $params, json_encode($Hlr)); + + $this->client->lookupHlr->create($Hlr, $params["countryCode"]); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadLookupHlr() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'lookup/31612345678/hlr', null, null); + $this->client->lookupHlr->read(31612345678); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testReadLookupHlrWithEmptyNumber() + { + $this->client->lookupHlr->read(null); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadLookupHlrWithCountryCode() + { + $params = array("countryCode" => "NL"); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'lookup/612345678/hlr', $params, null); + $this->client->lookupHlr->read(612345678, $params["countryCode"]); + } + +}