package updates

This commit is contained in:
2018-12-19 23:27:43 -06:00
parent aa1da4d4fb
commit 1ffd21369f
1181 changed files with 51194 additions and 11046 deletions
+290
View File
@@ -0,0 +1,290 @@
<?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\Numbers;
use Http\Client\Common\Exception\ClientErrorException;
use Nexmo\Client\ClientAwareInterface;
use Nexmo\Client\ClientAwareTrait;
use Psr\Http\Message\ResponseInterface;
use Nexmo\Client\Exception;
use Zend\Diactoros\Request;
class Client implements ClientAwareInterface
{
use ClientAwareTrait;
public function update($number, $id = null)
{
if(!is_null($id)){
$update = $this->get($id);
}
if($number instanceof Number){
$body = $number->getRequestData();
if(!isset($update) AND !isset($body['country'])){
$data = $this->get($number->getId());
$body['msisdn'] = $data->getId();
$body['country'] = $data->getCountry();
}
} else {
$body = $number;
}
if(isset($update)){
$body['msisdn'] = $update->getId();
$body['country'] = $update->getCountry();
}
$request = new Request(
$this->client->getRestUrl() . '/number/update',
'POST',
'php://temp',
[
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded'
]
);
$request->getBody()->write(http_build_query($body));
$response = $this->client->send($request);
if('200' != $response->getStatusCode()){
throw $this->getException($response);
}
if(isset($update) AND ($number instanceof Number)){
return $this->get($number);
}
if($number instanceof Number){
return $this->get($number);
}
return $this->get($body['msisdn']);
}
public function get($number = null)
{
$items = $this->search($number);
// This is legacy behaviour, so we need to keep it even though
// it isn't technically the correct message
if (count($items) != 1) {
throw new Exception\Request('number not found', 404);
}
return $items[0];
}
/**
* @param null|string $number
* @return array []Number
* @deprecated Use `searchOwned` instead
*/
public function search($number = null)
{
return $this->searchOwned($number);
}
public function searchAvailable($country, $options = [])
{
$query = [
'country' => $country
];
// These are all optional parameters
$possibleParameters = [
'pattern',
'search_pattern',
'features',
'size',
'type',
'index'
];
foreach ($possibleParameters as $param) {
if (isset($options[$param])) {
$query[$param] = $options[$param];
}
}
$request = new Request(
$this->client->getRestUrl() . '/number/search?' . http_build_query($query),
'GET',
'php://temp'
);
$response = $this->client->send($request);
return $this->handleNumberSearchResult($response, null);
}
public function searchOwned($number = null, $options = [])
{
$query = [];
if ($number !== null) {
if($number instanceof Number){
$query = ['pattern' => $number->getId()];
} else {
$query = ['pattern' => $number];
}
}
// These are all optional parameters
$possibleParameters = [
'search_pattern',
'size',
'index'
];
foreach ($options as $param => $value) {
if (!in_array($param, $possibleParameters)) {
throw new Exception\Request("Unknown option: '".$param."'");
}
$query[$param] = $value;
}
$queryString = http_build_query($query);
$request = new Request(
$this->client->getRestUrl() . '/account/numbers?' . $queryString,
'GET',
'php://temp'
);
$response = $this->client->send($request);
return $this->handleNumberSearchResult($response, $number);
}
private function handleNumberSearchResult($response, $number)
{
if($response->getStatusCode() != '200'){
throw $this->getException($response);
}
$searchResults = json_decode($response->getBody()->getContents(), true);
if(empty($searchResults)){
throw new Exception\Request('number not found', 404);
}
if(!isset($searchResults['count']) OR !isset($searchResults['numbers'])){
throw new Exception\Exception('unexpected response format');
}
// We're going to return a list of numbers
$numbers = [];
// If they provided a number initially, we'll only get one response
// so let's reuse the object
if($number instanceof Number){
$number->jsonUnserialize($searchResults['numbers'][0]);
$numbers[] = $number;
} else {
// Otherwise, we return everything that matches
foreach ($searchResults['numbers'] as $returnedNumber) {
$number = new Number();
$number->jsonUnserialize($returnedNumber);
$numbers[] = $number;
}
}
return $numbers;
}
public function purchase($number, $country = null) {
// We cheat here and fetch a number using the API so that we have the country code which is required
// to make a cancel request
if (!$number instanceof Number) {
if (!$country) {
throw new Exception\Exception("You must supply a country in addition to a number to purchase a number");
}
$number = new Number($number, $country);
}
$body = [
'msisdn' => $number->getMsisdn(),
'country' => $number->getCountry()
];
$request = new Request(
$this->client->getRestUrl() . '/number/buy',
'POST',
'php://temp',
[
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded'
]
);
$request->getBody()->write(http_build_query($body));
$response = $this->client->send($request);
// Sadly we can't distinguish *why* purchasing fails, just that it
// has failed. Here are a few of the tests I attempted and their associated
// error codes + body
//
// Mismatch number/country :: 420 :: method failed
// Already own number :: 420 :: method failed
// Someone else owns the number :: 420 :: method failed
if('200' != $response->getStatusCode()){
throw $this->getException($response);
}
}
public function cancel($number) {
// We cheat here and fetch a number using the API so that we have the country code which is required
// to make a cancel request
if (!$number instanceof Number) {
$number = $this->get($number);
}
$body = [
'msisdn' => $number->getMsisdn(),
'country' => $number->getCountry()
];
$request = new Request(
$this->client->getRestUrl() . '/number/cancel',
'POST',
'php://temp',
[
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded'
]
);
$request->getBody()->write(http_build_query($body));
$response = $this->client->send($request);
// Sadly we can't distinguish *why* purchasing fails, just that it
// has failed.
if('200' != $response->getStatusCode()){
throw $this->getException($response);
}
}
protected function getException(ResponseInterface $response)
{
$body = json_decode($response->getBody()->getContents(), true);
$status = $response->getStatusCode();
if($status >= 400 AND $status < 500) {
$e = new Exception\Request($body['error-code-label'], $status);
} elseif($status >= 500 AND $status < 600) {
$e = new Exception\Server($body['error-code-label'], $status);
} else {
$e = new Exception\Exception('Unexpected HTTP Status Code');
throw $e;
}
return $e;
}
}
+191
View File
@@ -0,0 +1,191 @@
<?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\Numbers;
use Nexmo\Application\Application;
use Nexmo\Entity\EntityInterface;
use Nexmo\Entity\JsonResponseTrait;
use Nexmo\Entity\JsonSerializableInterface;
use Nexmo\Entity\JsonSerializableTrait;
use Nexmo\Entity\JsonUnserializableInterface;
use Nexmo\Entity\NoRequestResponseTrait;
class Number implements EntityInterface, JsonSerializableInterface, JsonUnserializableInterface
{
use JsonSerializableTrait;
use NoRequestResponseTrait;
use JsonResponseTrait;
const TYPE_MOBILE = 'mobile-lvn';
const TYPE_FIXED = 'landline';
const FEATURE_VOICE = 'VOICE';
const FEATURE_SMS = 'SMS';
const WEBHOOK_MESSAGE = 'moHttpUrl';
const WEBHOOK_VOICE_STATUS = 'voiceStatusCallbackUrl';
const ENDPOINT_SIP = 'sip';
const ENDPOINT_TEL = 'tel';
const ENDPOINT_VXML = 'vxml';
const ENDPOINT_APP = 'app';
protected $data = [];
public function __construct($number = null, $country = null)
{
$this->data['msisdn'] = $number;
$this->data['country'] = $country;
}
public function getId()
{
return $this->fromData('msisdn');
}
public function getMsisdn()
{
return $this->getId();
}
public function getNumber()
{
return $this->getId();
}
public function getCountry()
{
return $this->fromData('country');
}
public function getType()
{
return $this->fromData('type');
}
public function getCost()
{
return $this->fromData('cost');
}
public function hasFeature($feature)
{
if(!isset($this->data['features'])){
return false;
}
return in_array($feature, $this->data['features']);
}
public function getFeatures()
{
return $this->fromData('features');
}
public function setWebhook($type, $url)
{
if(!in_array($type, [self::WEBHOOK_MESSAGE, self::WEBHOOK_VOICE_STATUS])){
throw new \InvalidArgumentException("invalid webhook type `$type`");
}
$this->data[$type] = $url;
return $this;
}
public function getWebhook($type)
{
return $this->fromData($type);
}
public function hasWebhook($type)
{
return isset($this->data[$type]);
}
public function setVoiceDestination($endpoint, $type = null)
{
if(is_null($type)){
$type = $this->autoType($endpoint);
}
if(self::ENDPOINT_APP == $type AND !($endpoint instanceof Application)){
$endpoint = new Application($endpoint);
}
$this->data['voiceCallbackValue'] = $endpoint;
$this->data['voiceCallbackType'] = $type;
return $this;
}
protected function autoType($endpoint)
{
if($endpoint instanceof Application){
return self::ENDPOINT_APP;
}
if(false !== strpos($endpoint, '@')){
return self::ENDPOINT_SIP;
}
if(0 === strpos(strtolower($endpoint), 'http')){
return self::ENDPOINT_VXML;
}
if(preg_match('#[a-z]+#', $endpoint)){
return self::ENDPOINT_APP;
}
return self::ENDPOINT_TEL;
}
public function getVoiceDestination()
{
return $this->fromData('voiceCallbackValue');
}
public function getVoiceType()
{
if(!isset($this->data['voiceCallbackType'])){
return null;
}
return $this->data['voiceCallbackType'];
}
protected function fromData($name)
{
if(!isset($this->data[$name])){
throw new \RuntimeException("`{$name}` has not been set");
}
return $this->data[$name];
}
public function jsonUnserialize(array $json)
{
$this->data = $json;
}
function jsonSerialize()
{
$json = $this->data;
if(isset($json['voiceCallbackValue']) AND ($json['voiceCallbackValue'] instanceof Application)){
$json['voiceCallbackValue'] = $json['voiceCallbackValue']->getId();
}
return $json;
}
public function __toString()
{
return (string) $this->getId();
}
}