package updates
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Network\Number;
|
||||
use Nexmo\Client\Callback\CallbackInterface;
|
||||
use Nexmo\Client\Callback\Callback as BaseCallback;
|
||||
|
||||
/**
|
||||
* @method null|string getType()
|
||||
* @method bool hasType()
|
||||
* @method null|string getNetwork()
|
||||
* @method bool hasNetwork()
|
||||
* @method null|string getNetworkName()
|
||||
* @method bool hasNetworkName()
|
||||
* @method null|string getValid()
|
||||
* @method bool hasValid()
|
||||
* @method null|string getPorted()
|
||||
* @method bool hasPorted()
|
||||
* @method null|string getReachable()
|
||||
* @method bool hasReachable()
|
||||
* @method null|string getRoaming()
|
||||
* @method bool hasRoaming()
|
||||
* @method null|string getRoamingCountry()
|
||||
* @method bool hasRoamingCountry()
|
||||
* @method null|string getRoamingNetwork()
|
||||
* @method bool hasRoamingNetwork()
|
||||
*/
|
||||
class Callback extends BaseCallback implements CallbackInterface
|
||||
{
|
||||
protected $expected = array('request_id', 'callback_part', 'callback_total_parts', 'number', 'status');
|
||||
protected $optional = array(
|
||||
'Type' => 'number_type',
|
||||
'Network' => 'carrier_network_code',
|
||||
'NetworkName' => 'carrier_network_name',
|
||||
'Valid' => 'valid',
|
||||
'Ported' => 'ported',
|
||||
'Reachable' => 'reachable',
|
||||
'Roaming' => 'roaming',
|
||||
'RoamingCountry' => 'roaming_country_code',
|
||||
'RoamingNetwork' => 'roaming_network_code',
|
||||
);
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->data['request_id'];
|
||||
}
|
||||
|
||||
public function getCallbackTotal()
|
||||
{
|
||||
return $this->data['callback_total_parts'];
|
||||
}
|
||||
|
||||
public function getCallbackIndex()
|
||||
{
|
||||
return $this->data['callback_part'];
|
||||
}
|
||||
|
||||
public function getNumber()
|
||||
{
|
||||
return $this->data['number'];
|
||||
}
|
||||
|
||||
public function __call($name, $args)
|
||||
{
|
||||
$type = substr($name, 0, 3);
|
||||
$property = substr($name, 3);
|
||||
|
||||
if(!isset($this->optional[$property])){
|
||||
throw new \BadMethodCallException('property does not exist: ' . $property);
|
||||
}
|
||||
|
||||
$property = $this->optional[$property];
|
||||
|
||||
switch($type){
|
||||
case 'get':
|
||||
if(isset($this->data[$property])){
|
||||
return $this->data[$property];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
case 'has':
|
||||
return isset($this->data[$property]);
|
||||
break;
|
||||
}
|
||||
|
||||
throw new \BadMethodCallException('method does not exist: ' . $name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Network\Number;
|
||||
|
||||
use Nexmo\Client\Request\AbstractRequest;
|
||||
use Nexmo\Client\Request\RequestInterface;
|
||||
use Nexmo\Client\Request\WrapResponseInterface;
|
||||
use Nexmo\Client\Response\Error;
|
||||
use Nexmo\Client\Response\ResponseInterface;
|
||||
|
||||
class Request extends AbstractRequest implements RequestInterface, WrapResponseInterface
|
||||
{
|
||||
const FEATURE_TYPE = 'type';
|
||||
const FEATURE_VALID = 'valid';
|
||||
const FEATURE_REACHABLE = 'reachable';
|
||||
const FEATURE_CARRIER = 'carrier';
|
||||
const FEATURE_PORTED = 'ported';
|
||||
const FEATURE_ROAMING = 'roaming';
|
||||
const FEATURE_SUBSCRIBER = 'subscriber';
|
||||
|
||||
protected $params = array();
|
||||
|
||||
public function __construct($number, $callback, $features = array(), $timeout = null, $method = null, $ref = null)
|
||||
{
|
||||
$this->params['number'] = $number;
|
||||
$this->params['callback'] = $callback;
|
||||
$this->params['callback_timeout'] = $timeout;
|
||||
$this->params['callback_method'] = $method;
|
||||
$this->params['client_ref'] = $ref;
|
||||
|
||||
if(!empty($features)){
|
||||
$this->params['features'] = implode(',', $features);
|
||||
}
|
||||
}
|
||||
|
||||
public function getURI()
|
||||
{
|
||||
return '/ni/json';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function wrapResponse(ResponseInterface $response)
|
||||
{
|
||||
if($response->isError()){
|
||||
return new Error($response->getData());
|
||||
}
|
||||
|
||||
return new Response($response->getData());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Network\Number;
|
||||
use Nexmo\Client\Response\Response as BaseResponse;
|
||||
use Nexmo\Client\Response\ResponseInterface;
|
||||
|
||||
class Response extends BaseResponse implements ResponseInterface
|
||||
{
|
||||
protected $callbacks = array();
|
||||
|
||||
public function __construct(array $data, $callbacks = array())
|
||||
{
|
||||
//add expected keys
|
||||
$this->expected = array_merge($this->expected, array(
|
||||
'request_id', 'number', 'request_price', 'remaining_balance', 'callback_total_parts'
|
||||
));
|
||||
|
||||
parent::__construct($data);
|
||||
|
||||
foreach($callbacks as $callback){
|
||||
if(!($callback instanceof Callback)){
|
||||
throw new \InvalidArgumentException('callback must be of type: Nexmo\Network\Number\Callback');
|
||||
}
|
||||
|
||||
if($callback->getId() !== $this->getId()){
|
||||
throw new \InvalidArgumentException('callback id must match request id');
|
||||
}
|
||||
}
|
||||
|
||||
$this->callbacks = $callbacks;
|
||||
}
|
||||
|
||||
public function getCallbackTotal()
|
||||
{
|
||||
return $this->data['callback_total_parts'];
|
||||
}
|
||||
|
||||
public function isComplete()
|
||||
{
|
||||
return count($this->callbacks) == $this->getCallbackTotal();
|
||||
}
|
||||
|
||||
public function getPrice()
|
||||
{
|
||||
return $this->data['request_price'];
|
||||
}
|
||||
|
||||
public function getBalance()
|
||||
{
|
||||
return $this->data['remaining_balance'];
|
||||
}
|
||||
|
||||
public function getNumber()
|
||||
{
|
||||
return $this->data['number'];
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->data['request_id'];
|
||||
}
|
||||
|
||||
public function getStatus()
|
||||
{
|
||||
return $this->data['status'];
|
||||
}
|
||||
|
||||
public function __call($name, $args)
|
||||
{
|
||||
if(empty($this->callbacks)){
|
||||
throw new \BadMethodCallException('can not check for response data without callback data');
|
||||
}
|
||||
|
||||
foreach($this->callbacks as $callback){
|
||||
if($last = $callback->$name()){
|
||||
return $last;
|
||||
}
|
||||
}
|
||||
return $last;
|
||||
}
|
||||
|
||||
public function getCallbacks()
|
||||
{
|
||||
return $this->callbacks;
|
||||
}
|
||||
|
||||
public static function addCallback(Response $response, Callback $callback)
|
||||
{
|
||||
$callbacks = $response->getCallbacks();
|
||||
$callbacks[] = $callback;
|
||||
|
||||
return new static($response->getData(), $callbacks);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user