package updates
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?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\Entity;
|
||||
|
||||
trait ArrayAccessTrait
|
||||
{
|
||||
public abstract function getResponseData();
|
||||
|
||||
public abstract function getRequestData();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?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\Entity;
|
||||
|
||||
interface CollectionAwareInterface
|
||||
{
|
||||
/**
|
||||
* @param CollectionInterface $collection
|
||||
* @return mixed
|
||||
*/
|
||||
public function setCollection(CollectionInterface $collection);
|
||||
|
||||
/**
|
||||
* @return CollectionInterface
|
||||
*/
|
||||
public function getCollection();
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?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\Entity;
|
||||
|
||||
trait CollectionAwareTrait
|
||||
{
|
||||
/**
|
||||
* @var CollectionInterface
|
||||
*/
|
||||
protected $collection;
|
||||
|
||||
public function setCollection(CollectionInterface $collection)
|
||||
{
|
||||
$this->collection = $collection;
|
||||
}
|
||||
|
||||
public function getCollection()
|
||||
{
|
||||
if(!isset($this->collection)){
|
||||
throw new \RuntimeException('missing collection');
|
||||
}
|
||||
|
||||
return $this->collection;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?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\Entity;
|
||||
|
||||
|
||||
interface CollectionInterface extends \Countable, \Iterator
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getCollectionName();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getCollectionPath();
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param $idOrEntity
|
||||
* @return mixed
|
||||
*/
|
||||
public function hydrateEntity($data, $idOrEntity);
|
||||
}
|
||||
+250
@@ -0,0 +1,250 @@
|
||||
<?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\Entity;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Zend\Diactoros\Request;
|
||||
use Nexmo\Application\Application;
|
||||
|
||||
/**
|
||||
* Common code for iterating over a collection, and using the collection class to discover the API path.
|
||||
*/
|
||||
trait CollectionTrait
|
||||
{
|
||||
/**
|
||||
* Index of the current resource of the current page
|
||||
* @var int
|
||||
*/
|
||||
protected $current;
|
||||
|
||||
/**
|
||||
* Current page data.
|
||||
* @var array
|
||||
*/
|
||||
protected $page;
|
||||
|
||||
/**
|
||||
* Last API Response
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
protected $response;
|
||||
|
||||
/**
|
||||
* User set page index.
|
||||
* @var int
|
||||
*/
|
||||
protected $index;
|
||||
|
||||
/**
|
||||
* User set pgge sixe.
|
||||
* @var int
|
||||
*/
|
||||
protected $size;
|
||||
|
||||
/**
|
||||
* @var FilterInterface
|
||||
*/
|
||||
protected $filter;
|
||||
|
||||
abstract public function getCollectionName();
|
||||
abstract public function getCollectionPath();
|
||||
abstract public function hydrateEntity($data, $id);
|
||||
|
||||
/**
|
||||
* Return the current item, expects concrete collection to handle creating the object.
|
||||
* @return mixed
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->hydrateEntity($this->page['_embedded'][$this->getCollectionName()][$this->current], $this->key());
|
||||
}
|
||||
|
||||
/**
|
||||
* No checks here, just advance the index.
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
$this->current++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the ID of the resource, in some cases this is `id`, in others `uuid`.
|
||||
* @return string
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
if(isset($this->page['_embedded'][$this->getCollectionName()][$this->current]['id'])){
|
||||
return $this->page['_embedded'][$this->getCollectionName()][$this->current]['id'];
|
||||
} elseif(isset($this->page['_embedded'][$this->getCollectionName()][$this->current]['uuid'])) {
|
||||
return $this->page['_embedded'][$this->getCollectionName()][$this->current]['uuid'];
|
||||
}
|
||||
|
||||
return $this->current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle pagination automatically (unless configured not to).
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
//can't be valid if there's not a page (rewind sets this)
|
||||
if(!isset($this->page)){
|
||||
return false;
|
||||
}
|
||||
|
||||
//all hal collections have an `_embedded` object, we expect there to be a property matching the collection name
|
||||
if(!isset($this->page['_embedded']) OR !isset($this->page['_embedded'][$this->getCollectionName()])){
|
||||
return false;
|
||||
}
|
||||
|
||||
//if we have a page with no items, we've gone beyond the end of the collection
|
||||
if(!count($this->page['_embedded'][$this->getCollectionName()])){
|
||||
return false;
|
||||
}
|
||||
|
||||
//index the start of a page at 0
|
||||
if(is_null($this->current)){
|
||||
$this->current = 0;
|
||||
}
|
||||
|
||||
//if our current index is past the current page, fetch the next page if possible and reset the index
|
||||
if(!isset($this->page['_embedded'][$this->getCollectionName()][$this->current])){
|
||||
if(isset($this->page['_links']) AND isset($this->page['_links']['next'])){
|
||||
$this->fetchPage($this->page['_links']['next']['href']);
|
||||
$this->current = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the initial page
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->fetchPage($this->getCollectionPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Count of total items
|
||||
* @return integer
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
if(isset($this->page)){
|
||||
return (int) $this->page['count'];
|
||||
}
|
||||
}
|
||||
|
||||
public function setPage($index)
|
||||
{
|
||||
$this->index = (int) $index;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPage()
|
||||
{
|
||||
if(isset($this->page)){
|
||||
return $this->page['page_index'];
|
||||
}
|
||||
|
||||
if(isset($this->index)){
|
||||
return $this->index;
|
||||
}
|
||||
|
||||
throw new \RuntimeException('page not set');
|
||||
}
|
||||
|
||||
public function getSize()
|
||||
{
|
||||
if(isset($this->page)){
|
||||
return $this->page['page_size'];
|
||||
}
|
||||
|
||||
if(isset($this->size)){
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
throw new \RuntimeException('size not set');
|
||||
}
|
||||
|
||||
public function setSize($size)
|
||||
{
|
||||
$this->size = (int) $size;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters reduce to query params and include paging settings.
|
||||
*
|
||||
* @param FilterInterface $filter
|
||||
* @return $this
|
||||
*/
|
||||
public function setFilter(FilterInterface $filter)
|
||||
{
|
||||
$this->filter = $filter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFilter()
|
||||
{
|
||||
if(!isset($this->filter)){
|
||||
$this->setFilter(new EmptyFilter());
|
||||
}
|
||||
|
||||
return $this->filter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a page using the current filter if no query is provided.
|
||||
*
|
||||
* @param $absoluteUri
|
||||
*/
|
||||
protected function fetchPage($absoluteUri)
|
||||
{
|
||||
//use filter if no query provided
|
||||
if(false === strpos($absoluteUri, '?')){
|
||||
$query = [];
|
||||
|
||||
if(isset($this->size)){
|
||||
$query['page_size'] = $this->size;
|
||||
}
|
||||
|
||||
if(isset($this->index)){
|
||||
$query['page_index'] = $this->index;
|
||||
}
|
||||
|
||||
if(isset($this->filter)){
|
||||
$query = array_merge($this->filter->getQuery(), $query);
|
||||
}
|
||||
|
||||
$absoluteUri .= '?' . http_build_query($query);
|
||||
}
|
||||
|
||||
//
|
||||
$request = new Request(
|
||||
$this->getClient()->getApiUrl() . $absoluteUri,
|
||||
'GET'
|
||||
);
|
||||
|
||||
$response = $this->client->send($request);
|
||||
|
||||
if($response->getStatusCode() != '200'){
|
||||
throw $this->getException($response);
|
||||
}
|
||||
|
||||
$this->response = $response;
|
||||
$this->page = json_decode($this->response->getBody()->getContents(), true);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?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\Entity;
|
||||
|
||||
|
||||
class EmptyFilter implements FilterInterface
|
||||
{
|
||||
public function getQuery()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?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\Entity;
|
||||
|
||||
|
||||
interface EntityInterface
|
||||
{
|
||||
public function getRequest();
|
||||
|
||||
public function getRequestData($sent = true);
|
||||
|
||||
public function getResponse();
|
||||
|
||||
public function getResponseData();
|
||||
|
||||
public function setResponse(\Psr\Http\Message\ResponseInterface $response);
|
||||
|
||||
public function setRequest(\Psr\Http\Message\RequestInterface $request);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?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\Entity;
|
||||
|
||||
|
||||
interface FilterInterface
|
||||
{
|
||||
public function getQuery();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?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\Entity;
|
||||
|
||||
trait HasEntityTrait
|
||||
{
|
||||
protected $entity;
|
||||
|
||||
public function setEntity($entity)
|
||||
{
|
||||
$this->entity = $entity;
|
||||
}
|
||||
|
||||
public function getEntity()
|
||||
{
|
||||
return $this->entity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?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\Entity;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
trait JsonResponseTrait
|
||||
{
|
||||
protected $responseJson;
|
||||
|
||||
public function getResponseData()
|
||||
{
|
||||
if(!($this instanceof EntityInterface)){
|
||||
throw new \Exception(sprintf(
|
||||
'%s can only be used if the class implements %s',
|
||||
__TRAIT__,
|
||||
EntityInterface::class
|
||||
));
|
||||
}
|
||||
|
||||
if(($response = $this->getResponse()) && ($response instanceof ResponseInterface)){
|
||||
if($response->getBody()->isSeekable()){
|
||||
$response->getBody()->rewind();
|
||||
}
|
||||
|
||||
$body = $response->getBody()->getContents();
|
||||
$this->responseJson = json_decode($body, true);
|
||||
return $this->responseJson;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?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\Entity;
|
||||
|
||||
/**
|
||||
* Identifies the Entity as using JsonSerializable to prepare request data.
|
||||
*/
|
||||
interface JsonSerializableInterface extends \JsonSerializable {}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?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\Entity;
|
||||
|
||||
/**
|
||||
* Implements getRequestData from EntityInterface based on the entity's jsonSerialize().
|
||||
*
|
||||
* @see EntityInterface::getRequestData()
|
||||
*/
|
||||
trait JsonSerializableTrait
|
||||
{
|
||||
/**
|
||||
* Get an array of params to use in an API request.
|
||||
*/
|
||||
public function getRequestData($sent = true)
|
||||
{
|
||||
if(!($this instanceof EntityInterface)){
|
||||
throw new \Exception(sprintf(
|
||||
'%s can only be used if the class implements %s',
|
||||
__TRAIT__,
|
||||
EntityInterface::class
|
||||
));
|
||||
}
|
||||
|
||||
if(!($this instanceof \JsonSerializable)){
|
||||
throw new \Exception(sprintf(
|
||||
'%s can only be used if the class implements %s',
|
||||
__TRAIT__,
|
||||
\JsonSerializable::class
|
||||
));
|
||||
}
|
||||
|
||||
if($sent && ($request = $this->getRequest())){
|
||||
//TODO, figure out what the request data actually was
|
||||
}
|
||||
|
||||
return $this->jsonSerialize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?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\Entity;
|
||||
|
||||
/**
|
||||
* Identifies the Entity as using JsonSerializable to prepare request data.
|
||||
*/
|
||||
interface JsonUnserializableInterface
|
||||
{
|
||||
/**
|
||||
* Update the object state with the json data (as an array)
|
||||
* @param $json
|
||||
* @return null
|
||||
*/
|
||||
public function jsonUnserialize(array $json);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?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\Entity;
|
||||
|
||||
/**
|
||||
* Class Psr7Trait
|
||||
*
|
||||
* Allow an entity to contain last request / response objects.
|
||||
*/
|
||||
trait NoRequestResponseTrait
|
||||
{
|
||||
public function setResponse(\Psr\Http\Message\ResponseInterface $response)
|
||||
{
|
||||
throw new \RuntimeException(__CLASS__ . ' does not support request / response');
|
||||
}
|
||||
|
||||
public function setRequest(\Psr\Http\Message\RequestInterface $request)
|
||||
{
|
||||
throw new \RuntimeException(__CLASS__ . ' does not support request / response');
|
||||
}
|
||||
|
||||
public function getRequest()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getResponse()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
<?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\Entity;
|
||||
|
||||
/**
|
||||
* Class Psr7Trait
|
||||
*
|
||||
* Allow an entity to contain last request / response objects.
|
||||
*/
|
||||
trait Psr7Trait
|
||||
{
|
||||
/**
|
||||
* @var \Psr\Http\Message\RequestInterface
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* @var \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
protected $response;
|
||||
|
||||
public function setResponse(\Psr\Http\Message\ResponseInterface $response)
|
||||
{
|
||||
$this->response = $response;
|
||||
|
||||
$status = $response->getStatusCode();
|
||||
|
||||
if($this instanceof JsonUnserializableInterface AND ((200 == $status) OR (201 == $status))){
|
||||
$this->jsonUnserialize($this->getResponseData());
|
||||
}
|
||||
}
|
||||
|
||||
public function setRequest(\Psr\Http\Message\RequestInterface $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function getRequest()
|
||||
{
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
public function getResponse()
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?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\Entity;
|
||||
|
||||
/**
|
||||
* Implements getRequestData from EntityInterface with a simple array. Request data stored in an array, and locked once
|
||||
* a request object has been set.
|
||||
*
|
||||
* @see EntityInterface::getRequestData()
|
||||
*/
|
||||
trait RequestArrayTrait
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $requestData = [];
|
||||
|
||||
/**
|
||||
* Get an array of params to use in an API request.
|
||||
*/
|
||||
public function getRequestData($sent = true)
|
||||
{
|
||||
if(!($this instanceof EntityInterface)){
|
||||
throw new \Exception(sprintf(
|
||||
'%s can only be used if the class implements %s',
|
||||
__TRAIT__,
|
||||
EntityInterface::class
|
||||
));
|
||||
}
|
||||
|
||||
if($sent && ($request = $this->getRequest())){
|
||||
$query = [];
|
||||
parse_str($request->getUri()->getQuery(), $query);
|
||||
return $query;
|
||||
}
|
||||
|
||||
// Trigger a pre-getRequestData() hook for any last minute
|
||||
// decision making that needs to be done, but only if
|
||||
// it hasn't been sent already
|
||||
if (method_exists($this, 'preGetRequestDataHook')) {
|
||||
$this->preGetRequestDataHook();
|
||||
}
|
||||
|
||||
return $this->requestData;
|
||||
}
|
||||
|
||||
protected function setRequestData($name, $value)
|
||||
{
|
||||
if(!($this instanceof EntityInterface)){
|
||||
throw new \Exception(sprintf(
|
||||
'%s can only be used if the class implements %s',
|
||||
__TRAIT__,
|
||||
EntityInterface::class
|
||||
));
|
||||
}
|
||||
|
||||
if($this->getResponse()){
|
||||
throw new \RuntimeException(sprintf(
|
||||
'can not set request parameter `%s` for `%s` after API request has be made',
|
||||
$name,
|
||||
get_class($this)
|
||||
));
|
||||
}
|
||||
|
||||
$this->requestData[$name] = $value;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user