Initial commit

This commit is contained in:
distinctm
2019-01-22 17:58:20 -05:00
commit d6ced07199
6519 changed files with 752220 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace Nexmo\Account;
use ArrayAccess;
use Nexmo\Client\Exception\Exception;
use Nexmo\Entity\JsonSerializableInterface;
use Nexmo\Entity\JsonUnserializableInterface;
class Balance implements JsonSerializableInterface, JsonUnserializableInterface, ArrayAccess {
public function __construct($balance, $autoReload)
{
$this->data['balance'] = $balance;
$this->data['auto_reload'] = $autoReload;
}
public function getBalance()
{
return $this['balance'];
}
public function getAutoReload()
{
return $this['auto_reload'];
}
public function jsonUnserialize(array $json)
{
$this->data = [
'balance' => $json['value'],
'auto_reload' => $json['autoReload']
];
}
function jsonSerialize()
{
return $this->data;
}
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
public function offsetGet($offset)
{
return $this->data[$offset];
}
public function offsetSet($offset, $value)
{
throw new Exception('Balance is read only');
}
public function offsetUnset($offset)
{
throw new Exception('Balance is read only');
}
}

View File

@@ -0,0 +1,228 @@
<?php
namespace Nexmo\Account;
use Nexmo\ApiErrorHandler;
use Nexmo\Client\ClientAwareInterface;
use Nexmo\Client\ClientAwareTrait;
use Nexmo\Network;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Request;
use Nexmo\Client\Exception;
class Client implements ClientAwareInterface
{
use ClientAwareTrait;
public function getPrefixPricing($prefix)
{
$queryString = http_build_query([
'prefix' => $prefix
]);
$request = new Request(
$this->getClient()->getRestUrl() . '/account/get-prefix-pricing/outbound?'.$queryString,
'GET',
'php://temp'
);
$response = $this->client->send($request);
$rawBody = $response->getBody()->getContents();
$body = json_decode($rawBody, true);
$codeCategory = (int) ($response->getStatusCode()/100);
if ($codeCategory != 2) {
if ($codeCategory == 4) {
throw new Exception\Request($body['error-code-label']);
}else if ($codeCategory == 5) {
throw new Exception\Server($body['error-code-label']);
}
}
if ($body['count'] == 0) {
return [];
}
// Multiple countries can match each prefix
$prices = [];
foreach ($body['prices'] as $p) {
$prefixPrice = new PrefixPrice();
$prefixPrice->jsonUnserialize($p);
$prices[] = $prefixPrice;
}
return $prices;
}
public function getSmsPrice($country)
{
$body = $this->makePricingRequest($country, 'sms');
$smsPrice = new SmsPrice();
$smsPrice->jsonUnserialize($body);
return $smsPrice;
}
public function getVoicePrice($country)
{
$body = $this->makePricingRequest($country, 'voice');
$voicePrice = new VoicePrice();
$voicePrice->jsonUnserialize($body);
return $voicePrice;
}
protected function makePricingRequest($country, $pricingType)
{
$queryString = http_build_query([
'country' => $country
]);
$request = new Request(
$this->getClient()->getRestUrl() . '/account/get-pricing/outbound/'.$pricingType.'?'.$queryString,
'GET',
'php://temp'
);
$response = $this->client->send($request);
$rawBody = $response->getBody()->getContents();
if ($rawBody === '') {
throw new Exception\Server('No results found');
}
return json_decode($rawBody, true);
}
public function getBalance()
{
$request = new Request(
$this->getClient()->getRestUrl() . '/account/get-balance',
'GET',
'php://temp'
);
$response = $this->client->send($request);
$rawBody = $response->getBody()->getContents();
if ($rawBody === '') {
throw new Exception\Server('No results found');
}
$body = json_decode($rawBody, true);
$balance = new Balance($body['value'], $body['autoReload']);
return $balance;
}
public function topUp($trx)
{
$body = [
'trx' => $trx
];
$request = new Request(
$this->getClient()->getRestUrl() . '/account/top-up'
,'POST'
, 'php://temp'
, ['content-type' => 'application/x-www-form-urlencoded']
);
$request->getBody()->write(http_build_query($body));
$response = $this->client->send($request);
if($response->getStatusCode() != '200'){
throw $this->getException($response);
}
}
public function listSecrets($apiKey)
{
$body = $this->get( $this->getClient()->getApiUrl() . '/accounts/'.$apiKey.'/secrets');
return SecretCollection::fromApi($body);
}
public function getSecret($apiKey, $secretId)
{
$body = $this->get( $this->getClient()->getApiUrl() . '/accounts/'.$apiKey.'/secrets/'. $secretId);
return Secret::fromApi($body);
}
public function createSecret($apiKey, $newSecret)
{
$body = [
'secret' => $newSecret
];
$request = new Request(
$this->getClient()->getApiUrl() . '/accounts/'.$apiKey.'/secrets'
,'POST'
, 'php://temp'
, ['content-type' => 'application/json']
);
$request->getBody()->write(json_encode($body));
$response = $this->client->send($request);
$rawBody = $response->getBody()->getContents();
$responseBody = json_decode($rawBody, true);
ApiErrorHandler::check($responseBody, $response->getStatusCode());
return Secret::fromApi($responseBody);
}
public function deleteSecret($apiKey, $secretId)
{
$request = new Request(
$this->getClient()->getApiUrl() . '/accounts/'.$apiKey.'/secrets/'. $secretId
,'DELETE'
, 'php://temp'
, ['content-type' => 'application/json']
);
$response = $this->client->send($request);
$rawBody = $response->getBody()->getContents();
$body = json_decode($rawBody, true);
// This will throw an exception on any error
ApiErrorHandler::check($body, $response->getStatusCode());
// This returns a 204, so no response body
}
protected function get($url) {
$request = new Request(
$url
,'GET'
, 'php://temp'
, ['content-type' => 'application/json']
);
$response = $this->client->send($request);
$rawBody = $response->getBody()->getContents();
$body = json_decode($rawBody, true);
// This will throw an exception on any error
ApiErrorHandler::check($body, $response->getStatusCode());
return $body;
}
protected function getException(ResponseInterface $response, $application = null)
{
$body = json_decode($response->getBody()->getContents(), true);
$status = $response->getStatusCode();
if($status >= 400 AND $status < 500) {
$e = new Exception\Request($body['error_title'], $status);
} elseif($status >= 500 AND $status < 600) {
$e = new Exception\Server($body['error_title'], $status);
} else {
$e = new Exception\Exception('Unexpected HTTP Status Code');
}
return $e;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Nexmo\Account;
class PrefixPrice extends Price {
protected $priceMethod = 'getPrefixPrice';
public function getCurrency()
{
throw new Exception('Currency is unavailable from this endpoint');
}
}

View File

@@ -0,0 +1,144 @@
<?php
namespace Nexmo\Account;
use ArrayAccess;
use Nexmo\Client\Exception\Exception;
use Nexmo\Network;
use Nexmo\Entity\EntityInterface;
use Nexmo\Entity\JsonSerializableInterface;
use Nexmo\Entity\JsonResponseTrait;
use Nexmo\Entity\JsonSerializableTrait;
use Nexmo\Entity\NoRequestResponseTrait;
use Nexmo\Entity\JsonUnserializableInterface;
abstract class Price implements EntityInterface, JsonSerializableInterface, JsonUnserializableInterface, ArrayAccess {
use JsonSerializableTrait;
use NoRequestResponseTrait;
use JsonResponseTrait;
protected $data = [];
public function getCountryCode()
{
return $this['country_code'];
}
public function getCountryDisplayName()
{
return $this['country_display_name'];
}
public function getCountryName()
{
return $this['country_name'];
}
public function getDialingPrefix()
{
return $this['dialing_prefix'];
}
public function getDefaultPrice()
{
if (isset($this['default_price'])) {
return $this['default_price'];
}
return $this['mt'];
}
public function getCurrency()
{
return $this['currency'];
}
public function getNetworks()
{
return $this['networks'];
}
public function getPriceForNetwork($networkCode)
{
$networks = $this->getNetworks();
if (isset($networks[$networkCode]))
{
return $networks[$networkCode]->{$this->priceMethod}();
}
return $this->getDefaultPrice();
}
public function jsonUnserialize(array $json)
{
// Convert CamelCase to snake_case as that's how we use array access in every other object
$data = [];
foreach ($json as $k => $v){
$k = ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '_$0', $k)), '_');
// PrefixPrice fixes
if ($k == 'country') {
$k = 'country_code';
}
if ($k == 'name') {
$data['country_display_name'] = $v;
$data['country_name'] = $v;
}
if ($k == 'prefix') {
$k = 'dialing_prefix';
}
$data[$k] = $v;
}
// Create objects for all the nested networks too
$networks = [];
if (isset($json['networks'])) {
foreach ($json['networks'] as $n){
if (isset($n['code'])) {
$n['networkCode'] = $n['code'];
unset ($n['code']);
}
if (isset($n['network'])) {
$n['networkName'] = $n['network'];
unset ($n['network']);
}
$network = new Network($n['networkCode'], $n['networkName']);
$network->jsonUnserialize($n);
$networks[$network->getCode()] = $network;
}
}
$data['networks'] = $networks;
$this->data = $data;
}
function jsonSerialize()
{
return $this->data;
}
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
public function offsetGet($offset)
{
return $this->data[$offset];
}
public function offsetSet($offset, $value)
{
throw new Exception('Price is read only');
}
public function offsetUnset($offset)
{
throw new Exception('Price is read only');
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Nexmo\Account;
use Nexmo\InvalidResponseException;
class Secret implements \ArrayAccess {
protected $data;
public function __construct($data) {
$this->data = $data;
}
public function getId() {
return $this['id'];
}
public function getCreatedAt() {
return $this['created_at'];
}
public function getLinks() {
return $this['_links'];
}
public static function fromApi($data) {
if (!isset($data['id'])) {
throw new InvalidResponseException("Missing key: 'id");
}
if (!isset($data['created_at'])) {
throw new InvalidResponseException("Missing key: 'created_at");
}
return new self($data);
}
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
public function offsetGet($offset)
{
return $this->data[$offset];
}
public function offsetSet($offset, $value)
{
throw new \Exception('Secret::offsetSet is not implemented');
}
public function offsetUnset($offset)
{
throw new \Exception('Secret::offsetUnset is not implemented');
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Nexmo\Account;
class SecretCollection implements \ArrayAccess {
protected $data;
public function __construct($secrets, $links) {
$this->data = [
'secrets' => $secrets,
'_links' => $links
];
}
public function getSecrets() {
return $this['secrets'];
}
public function getLinks() {
return $this['_links'];
}
public static function fromApi($data) {
$secrets = [];
foreach ($data['_embedded']['secrets'] as $s) {
$secrets[] = Secret::fromApi($s);
}
return new self($secrets, $data['_links']);
}
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
public function offsetGet($offset)
{
return $this->data[$offset];
}
public function offsetSet($offset, $value)
{
throw new \Exception('SecretCollection::offsetSet is not implemented');
}
public function offsetUnset($offset)
{
throw new \Exception('SecretCollection::offsetUnset is not implemented');
}
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Nexmo\Account;
class SmsPrice extends Price {
protected $priceMethod = 'getOutboundSmsPrice';
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Nexmo\Account;
class VoicePrice extends Price {
protected $priceMethod = 'getOutboundVoicePrice';
}