package updates
This commit is contained in:
+301
@@ -0,0 +1,301 @@
|
||||
<?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\Call;
|
||||
|
||||
use Nexmo\Client\ClientAwareInterface;
|
||||
use Nexmo\Client\ClientAwareTrait;
|
||||
use Nexmo\Conversations\Conversation;
|
||||
use Nexmo\Entity\EntityInterface;
|
||||
use Nexmo\Entity\JsonResponseTrait;
|
||||
use Nexmo\Entity\JsonSerializableTrait;
|
||||
use Nexmo\Entity\JsonUnserializableInterface;
|
||||
use Nexmo\Entity\NoRequestResponseTrait;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Nexmo\Client\Exception;
|
||||
use Zend\Diactoros\Request;
|
||||
|
||||
/**
|
||||
* Class Call
|
||||
*
|
||||
* @property \Nexmo\Call\Stream $stream
|
||||
* @property \Nexmo\Call\Talk $talk
|
||||
* @property \Nexmo\Call\Dtmf $dtmf
|
||||
*
|
||||
* @method \Nexmo\Call\Stream stream()
|
||||
* @method \Nexmo\Call\Talk talk()
|
||||
* @method \Nexmo\Call\Dtmf dtmf()
|
||||
*/
|
||||
class Call implements EntityInterface, \JsonSerializable, JsonUnserializableInterface, ClientAwareInterface
|
||||
{
|
||||
use NoRequestResponseTrait;
|
||||
use JsonSerializableTrait;
|
||||
use JsonResponseTrait;
|
||||
use ClientAwareTrait;
|
||||
|
||||
const WEBHOOK_ANSWER = 'answer';
|
||||
const WEBHOOK_EVENT = 'event';
|
||||
|
||||
const TIMER_LENGTH = 'length';
|
||||
const TIMER_RINGING = 'ringing';
|
||||
|
||||
const TIMEOUT_MACHINE = 'machine';
|
||||
|
||||
protected $id;
|
||||
|
||||
protected $to;
|
||||
|
||||
protected $from;
|
||||
|
||||
/**
|
||||
* @var Webhook[]
|
||||
*/
|
||||
protected $webhooks = [];
|
||||
|
||||
protected $data = [];
|
||||
|
||||
protected $subresources = [];
|
||||
|
||||
public function __construct($id = null)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
$request = new Request(
|
||||
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId()
|
||||
,'GET'
|
||||
);
|
||||
|
||||
$response = $this->getClient()->send($request);
|
||||
|
||||
if($response->getStatusCode() != '200'){
|
||||
throw $this->getException($response);
|
||||
}
|
||||
|
||||
$data = json_decode($response->getBody()->getContents(), true);
|
||||
$this->jsonUnserialize($data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
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_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;
|
||||
}
|
||||
|
||||
public function put($payload)
|
||||
{
|
||||
$request = new Request(
|
||||
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId()
|
||||
,'PUT',
|
||||
'php://temp',
|
||||
['content-type' => 'application/json']
|
||||
);
|
||||
|
||||
$request->getBody()->write(json_encode($payload));
|
||||
$response = $this->client->send($request);
|
||||
|
||||
$responseCode = $response->getStatusCode();
|
||||
if($responseCode != '200' && $responseCode != '204'){
|
||||
throw $this->getException($response);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setTo($endpoint)
|
||||
{
|
||||
if(!($endpoint instanceof Endpoint)){
|
||||
$endpoint = new Endpoint($endpoint);
|
||||
}
|
||||
|
||||
$this->to = $endpoint;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Endpoint
|
||||
*/
|
||||
public function getTo()
|
||||
{
|
||||
if($this->lazyLoad()){
|
||||
return new Endpoint($this->data['to']['number'], $this->data['to']['type']);
|
||||
}
|
||||
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
public function setFrom($endpoint)
|
||||
{
|
||||
if(!($endpoint instanceof Endpoint)){
|
||||
$endpoint = new Endpoint($endpoint);
|
||||
}
|
||||
|
||||
$this->from = $endpoint;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Endpoint
|
||||
*/
|
||||
public function getFrom()
|
||||
{
|
||||
if($this->lazyLoad()){
|
||||
return new Endpoint($this->data['from']['number'], $this->data['from']['type']);
|
||||
}
|
||||
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
public function setWebhook($type, $url = null, $method = null)
|
||||
{
|
||||
if($type instanceof Webhook){
|
||||
$this->webhooks[$type->getType()] = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
if(is_null($url)){
|
||||
throw new \InvalidArgumentException('must provide `Nexmo\Call\Webhook` object, or a type and url: missing url' );
|
||||
}
|
||||
|
||||
$this->webhooks[$type] = new Webhook($type, $url, $method);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setTimer($type, $length)
|
||||
{
|
||||
$this->data[$type . '_timer'] = $length;
|
||||
}
|
||||
|
||||
public function setTimeout($type, $length)
|
||||
{
|
||||
$this->data[$type . '_timeout'] = $length;
|
||||
}
|
||||
|
||||
public function getStatus()
|
||||
{
|
||||
if($this->lazyLoad()){
|
||||
return $this->data['status'];
|
||||
}
|
||||
}
|
||||
|
||||
public function getDirection()
|
||||
{
|
||||
if($this->lazyLoad()){
|
||||
return $this->data['direction'];
|
||||
}
|
||||
}
|
||||
|
||||
public function getConversation()
|
||||
{
|
||||
if($this->lazyLoad()){
|
||||
return new Conversation($this->data['conversation_uuid']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the resource data is loaded.
|
||||
*
|
||||
* Will attempt to load the data if it's not already.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function lazyLoad()
|
||||
{
|
||||
if(!empty($this->data)){
|
||||
return true;
|
||||
}
|
||||
|
||||
if(isset($this->id)){
|
||||
$this->get($this);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
switch($name){
|
||||
case 'stream':
|
||||
case 'talk':
|
||||
case 'dtmf':
|
||||
return $this->lazySubresource(ucfirst($name));
|
||||
default:
|
||||
throw new \RuntimeException('property does not exist: ' . $name);
|
||||
}
|
||||
}
|
||||
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
switch($name){
|
||||
case 'stream':
|
||||
case 'talk':
|
||||
case 'dtmf':
|
||||
$entity = $this->lazySubresource(ucfirst($name));
|
||||
return call_user_func_array($entity, $arguments);
|
||||
default:
|
||||
throw new \RuntimeException('method does not exist: ' . $name);
|
||||
}
|
||||
}
|
||||
|
||||
protected function lazySubresource($type)
|
||||
{
|
||||
if(!isset($this->subresources[$type])){
|
||||
$class = 'Nexmo\Call\\' . $type;
|
||||
$instance = new $class($this->getId());
|
||||
$instance->setClient($this->getClient());
|
||||
$this->subresources[$type] = $instance;
|
||||
}
|
||||
|
||||
return $this->subresources[$type];
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
$data = $this->data;
|
||||
|
||||
if(isset($this->to)){
|
||||
$data['to'] = [$this->to->jsonSerialize()];
|
||||
}
|
||||
|
||||
if(isset($this->from)){
|
||||
$data['from'] = $this->from->jsonSerialize();
|
||||
}
|
||||
|
||||
foreach($this->webhooks as $webhook){
|
||||
$data = array_merge($data, $webhook->jsonSerialize());
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function jsonUnserialize(array $json)
|
||||
{
|
||||
$this->data = $json;
|
||||
$this->id = $json['uuid'];
|
||||
}
|
||||
}
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
<?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\Call;
|
||||
|
||||
use Nexmo\Client\ClientAwareInterface;
|
||||
use Nexmo\Client\ClientAwareTrait;
|
||||
use Nexmo\Conversations\Conversation;
|
||||
use Nexmo\Entity\CollectionInterface;
|
||||
use Nexmo\Entity\CollectionTrait;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Zend\Diactoros\Request;
|
||||
use Nexmo\Client\Exception;
|
||||
|
||||
class Collection implements ClientAwareInterface, CollectionInterface, \ArrayAccess
|
||||
{
|
||||
use ClientAwareTrait;
|
||||
use CollectionTrait;
|
||||
|
||||
public static function getCollectionName()
|
||||
{
|
||||
return 'calls';
|
||||
}
|
||||
|
||||
public static function getCollectionPath()
|
||||
{
|
||||
return '/v1/' . self::getCollectionName();
|
||||
}
|
||||
|
||||
public function hydrateEntity($data, $idOrCall)
|
||||
{
|
||||
if(!($idOrCall instanceof Call)){
|
||||
$idOrCall = new Call($idOrCall);
|
||||
}
|
||||
|
||||
$idOrCall->setClient($this->getClient());
|
||||
$idOrCall->jsonUnserialize($data);
|
||||
|
||||
return $idOrCall;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $callOrFilter
|
||||
* @return $this|Call
|
||||
*/
|
||||
public function __invoke(Filter $filter = null)
|
||||
{
|
||||
if(!is_null($filter)){
|
||||
$this->setFilter($filter);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function create($call)
|
||||
{
|
||||
return $this->post($call);
|
||||
}
|
||||
|
||||
public function put($payload, $idOrCall)
|
||||
{
|
||||
if(!($idOrCall instanceof Call)){
|
||||
$idOrCall = new Call($idOrCall);
|
||||
}
|
||||
|
||||
$idOrCall->setClient($this->getClient());
|
||||
$idOrCall->put($payload);
|
||||
return $idOrCall;
|
||||
}
|
||||
|
||||
public function delete($call = null, $type)
|
||||
{
|
||||
if(is_object($call) AND is_callable([$call, 'getId'])){
|
||||
$call = $call->getId();
|
||||
}
|
||||
|
||||
if(!($call instanceof Call)){
|
||||
$call = new Call($call);
|
||||
}
|
||||
|
||||
$request = new Request(
|
||||
$this->getClient()->getApiUrl() . $this->getCollectionPath() . '/' . $call->getId() . '/' . $type
|
||||
,'DELETE'
|
||||
);
|
||||
|
||||
$response = $this->client->send($request);
|
||||
|
||||
if($response->getStatusCode() != '204'){
|
||||
throw $this->getException($response);
|
||||
}
|
||||
|
||||
return $call;
|
||||
}
|
||||
|
||||
public function post($call)
|
||||
{
|
||||
if($call instanceof Call){
|
||||
$body = $call->getRequestData();
|
||||
} else {
|
||||
$body = $call;
|
||||
}
|
||||
|
||||
$request = new Request(
|
||||
$this->getClient()->getApiUrl() . $this->getCollectionPath()
|
||||
,'POST',
|
||||
'php://temp',
|
||||
['content-type' => 'application/json']
|
||||
);
|
||||
|
||||
$request->getBody()->write(json_encode($body));
|
||||
$response = $this->client->send($request);
|
||||
|
||||
if($response->getStatusCode() != '201'){
|
||||
throw $this->getException($response);
|
||||
}
|
||||
|
||||
$body = json_decode($response->getBody()->getContents(), true);
|
||||
$call = new Call($body['uuid']);
|
||||
$call->jsonUnserialize($body);
|
||||
$call->setClient($this->getClient());
|
||||
|
||||
return $call;
|
||||
}
|
||||
|
||||
public function get($call)
|
||||
{
|
||||
if(!($call instanceof Call)){
|
||||
$call = new Call($call);
|
||||
}
|
||||
|
||||
$call->setClient($this->getClient());
|
||||
$call->get();
|
||||
|
||||
return $call;
|
||||
}
|
||||
|
||||
protected function getException(ResponseInterface $response)
|
||||
{
|
||||
$body = json_decode($response->getBody()->getContents(), true);
|
||||
$status = $response->getStatusCode();
|
||||
|
||||
// Error responses aren't consistent. Some are generated within the
|
||||
// proxy and some are generated within voice itself. This handles
|
||||
// both cases
|
||||
|
||||
// This message isn't very useful, but we shouldn't ever see it
|
||||
$errorTitle = 'Unexpected error';
|
||||
|
||||
if (isset($body['title'])) {
|
||||
$errorTitle = $body['title'];
|
||||
}
|
||||
|
||||
if (isset($body['error_title'])) {
|
||||
$errorTitle = $body['error_title'];
|
||||
}
|
||||
|
||||
if($status >= 400 AND $status < 500) {
|
||||
$e = new Exception\Request($errorTitle, $status);
|
||||
} elseif($status >= 500 AND $status < 600) {
|
||||
$e = new Exception\Server($errorTitle, $status);
|
||||
} else {
|
||||
$e = new Exception\Exception('Unexpected HTTP Status Code');
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
//todo: validate form of id
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $call
|
||||
* @return Call
|
||||
*/
|
||||
public function offsetGet($call)
|
||||
{
|
||||
if(!($call instanceof Call)){
|
||||
$call = new Call($call);
|
||||
}
|
||||
|
||||
$call->setClient($this->getClient());
|
||||
return $call;
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
throw new \RuntimeException('can not set collection properties');
|
||||
}
|
||||
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
throw new \RuntimeException('can not unset collection properties');
|
||||
}
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Call;
|
||||
use Nexmo\Call\Collection;
|
||||
use Nexmo\Client\ClientAwareInterface;
|
||||
use Nexmo\Client\ClientAwareTrait;
|
||||
use Nexmo\Entity\JsonSerializableInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Zend\Diactoros\Request;
|
||||
use Nexmo\Client\Exception;
|
||||
|
||||
/**
|
||||
* Lightweight resource, only has put / delete.
|
||||
*/
|
||||
class Dtmf implements JsonSerializableInterface, ClientAwareInterface, \ArrayAccess
|
||||
{
|
||||
use ClientAwareTrait;
|
||||
|
||||
protected $id;
|
||||
|
||||
protected $data = [];
|
||||
|
||||
protected $params= [
|
||||
'digits'
|
||||
];
|
||||
|
||||
public function __construct($id = null)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function __invoke(self $entity = null)
|
||||
{
|
||||
if(is_null($entity)){
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $this->put($entity);
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setDigits($digits)
|
||||
{
|
||||
$this->data['digits'] = (string) $digits;
|
||||
}
|
||||
|
||||
public function put($dtmf = null)
|
||||
{
|
||||
if(!$dtmf){
|
||||
$dtmf = $this;
|
||||
}
|
||||
|
||||
$request = new Request(
|
||||
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() . '/dtmf',
|
||||
'PUT',
|
||||
'php://temp',
|
||||
['content-type' => 'application/json']
|
||||
);
|
||||
|
||||
$request->getBody()->write(json_encode($dtmf));
|
||||
$response = $this->client->send($request);
|
||||
return $this->parseEventResponse($response);
|
||||
}
|
||||
|
||||
protected function parseEventResponse(ResponseInterface $response)
|
||||
{
|
||||
if($response->getStatusCode() != '200'){
|
||||
throw $this->getException($response);
|
||||
}
|
||||
|
||||
$json = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
if(!$json){
|
||||
throw new Exception\Exception('Unexpected Response Body Format');
|
||||
}
|
||||
|
||||
return new Event($json);
|
||||
}
|
||||
|
||||
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_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');
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if(!in_array($offset, $this->params)){
|
||||
throw new \RuntimeException('invalid parameter: ' . $offset);
|
||||
}
|
||||
|
||||
$this->data[$offset] = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
if(!in_array($offset, $this->params)){
|
||||
throw new \RuntimeException('invalid parameter: ' . $offset);
|
||||
}
|
||||
|
||||
unset($this->data[$offset]);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Call;
|
||||
|
||||
|
||||
class Earmuff implements \JsonSerializable
|
||||
{
|
||||
function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'action' => 'earmuff'
|
||||
];
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
<?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\Call;
|
||||
|
||||
/**
|
||||
* Class Endpoint
|
||||
* Represents a call destination / origin.
|
||||
*
|
||||
* TODO: Try to unify this and other (message, etc) endpoint identifiers.
|
||||
*/
|
||||
class Endpoint implements \JsonSerializable
|
||||
{
|
||||
const PHONE = 'phone';
|
||||
|
||||
protected $id;
|
||||
|
||||
protected $type;
|
||||
|
||||
protected $additional;
|
||||
|
||||
public function __construct($id, $type = self::PHONE, $additional = [])
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->type = $type;
|
||||
$this->additional = $additional;
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function set($property, $value)
|
||||
{
|
||||
$this->additional[$property] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get($property)
|
||||
{
|
||||
if(isset($this->additional[$property])){
|
||||
return $this->additional[$property];
|
||||
}
|
||||
}
|
||||
|
||||
public function getNumber()
|
||||
{
|
||||
if(!self::PHONE == $this->type){
|
||||
throw new \RuntimeException('number not defined for this type');
|
||||
}
|
||||
|
||||
return $this->getId();
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return (string) $this->getId();
|
||||
}
|
||||
|
||||
function jsonSerialize()
|
||||
{
|
||||
switch($this->type){
|
||||
case 'phone':
|
||||
return array_merge(
|
||||
$this->additional,
|
||||
[
|
||||
'type' => $this->type,
|
||||
'number' => $this->id
|
||||
]
|
||||
|
||||
);
|
||||
default:
|
||||
throw new \RuntimeException('unknown type: ' . $this->type);
|
||||
}
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Call;
|
||||
|
||||
class Event implements \ArrayAccess
|
||||
{
|
||||
protected $data;
|
||||
|
||||
public function __construct($data)
|
||||
{
|
||||
if(!isset($data['uuid']) || !isset($data['message'])){
|
||||
throw new \InvalidArgumentException('missing message or uuid');
|
||||
}
|
||||
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->data['uuid'];
|
||||
}
|
||||
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->data['message'];
|
||||
}
|
||||
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->data[$offset]);
|
||||
}
|
||||
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return $this->data[$offset];
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
throw new \RuntimeException('can not set properties directly');
|
||||
}
|
||||
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
throw new \RuntimeException('can not set properties directly');
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
<?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\Call;
|
||||
|
||||
use Nexmo\Conversations\Conversation;
|
||||
use Nexmo\Entity\FilterInterface;
|
||||
|
||||
class Filter implements FilterInterface
|
||||
{
|
||||
protected $query = [];
|
||||
|
||||
public function getQuery()
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
public function sortAscending()
|
||||
{
|
||||
return $this->setOrder('asc');
|
||||
}
|
||||
|
||||
public function sortDescending()
|
||||
{
|
||||
return $this->setOrder('desc');
|
||||
}
|
||||
|
||||
public function setStatus($status)
|
||||
{
|
||||
$this->query['status'] = (string) $status;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setStart(\DateTime $start)
|
||||
{
|
||||
$start->setTimezone(new \DateTimeZone("UTC"));
|
||||
$this->query['date_start'] = $start->format('Y-m-d\TH:i:s\Z');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setEnd(\DateTime $end)
|
||||
{
|
||||
$end->setTimezone(new \DateTimeZone("UTC"));
|
||||
$this->query['date_end'] = $end->format('Y-m-d\TH:i:s\Z');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setSize($size)
|
||||
{
|
||||
$this->query['page_size'] = (int) $size;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setIndex($index)
|
||||
{
|
||||
$this->query['record_index'] = (int) $index;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setOrder($order)
|
||||
{
|
||||
$this->query['order'] = (string) $order;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setConversation($conversation)
|
||||
{
|
||||
if($conversation instanceof Conversation){
|
||||
$conversation = $conversation->getId();
|
||||
}
|
||||
|
||||
$this->query['conversation_uuid'] = $conversation;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Call;
|
||||
|
||||
|
||||
class Hangup implements \JsonSerializable
|
||||
{
|
||||
function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'action' => 'hangup'
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Call;
|
||||
|
||||
|
||||
class Mute implements \JsonSerializable
|
||||
{
|
||||
function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'action' => 'mute'
|
||||
];
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Call;
|
||||
use Nexmo\Call\Collection;
|
||||
use Nexmo\Client\ClientAwareInterface;
|
||||
use Nexmo\Client\ClientAwareTrait;
|
||||
use Nexmo\Entity\JsonSerializableInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Zend\Diactoros\Request;
|
||||
use Nexmo\Client\Exception;
|
||||
|
||||
/**
|
||||
* Lightweight resource, only has put / delete.
|
||||
*/
|
||||
class Stream implements JsonSerializableInterface, ClientAwareInterface
|
||||
{
|
||||
use ClientAwareTrait;
|
||||
|
||||
protected $id;
|
||||
|
||||
protected $data = [];
|
||||
|
||||
public function __construct($id = null)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function __invoke(Stream $stream = null)
|
||||
{
|
||||
if(is_null($stream)){
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $this->put($stream);
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setUrl($url)
|
||||
{
|
||||
if(!is_array($url)){
|
||||
$url = array($url);
|
||||
}
|
||||
|
||||
$this->data['stream_url'] = $url;
|
||||
}
|
||||
|
||||
public function setLoop($times)
|
||||
{
|
||||
$this->data['loop'] = (int) $times;
|
||||
}
|
||||
|
||||
public function put($stream = null)
|
||||
{
|
||||
if(!$stream){
|
||||
$stream = $this;
|
||||
}
|
||||
|
||||
$request = new Request(
|
||||
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() . '/stream',
|
||||
'PUT',
|
||||
'php://temp',
|
||||
['content-type' => 'application/json']
|
||||
);
|
||||
|
||||
$request->getBody()->write(json_encode($stream));
|
||||
$response = $this->client->send($request);
|
||||
return $this->parseEventResponse($response);
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$request = new Request(
|
||||
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() . '/stream',
|
||||
'DELETE'
|
||||
);
|
||||
|
||||
$response = $this->client->send($request);
|
||||
return $this->parseEventResponse($response);
|
||||
}
|
||||
|
||||
protected function parseEventResponse(ResponseInterface $response)
|
||||
{
|
||||
if($response->getStatusCode() != '200'){
|
||||
throw $this->getException($response);
|
||||
}
|
||||
|
||||
$json = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
if(!$json){
|
||||
throw new Exception\Exception('Unexpected Response Body Format');
|
||||
}
|
||||
|
||||
return new Event($json);
|
||||
}
|
||||
|
||||
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_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');
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
function jsonSerialize()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
}
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Call;
|
||||
use Nexmo\Call\Collection;
|
||||
use Nexmo\Client\ClientAwareInterface;
|
||||
use Nexmo\Client\ClientAwareTrait;
|
||||
use Nexmo\Entity\JsonSerializableInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Zend\Diactoros\Request;
|
||||
use Nexmo\Client\Exception;
|
||||
|
||||
/**
|
||||
* Lightweight resource, only has put / delete.
|
||||
*/
|
||||
class Talk implements JsonSerializableInterface, ClientAwareInterface, \ArrayAccess
|
||||
{
|
||||
use ClientAwareTrait;
|
||||
|
||||
protected $id;
|
||||
|
||||
protected $data = [];
|
||||
|
||||
protected $params= [
|
||||
'text',
|
||||
'voice_name',
|
||||
'loop'
|
||||
];
|
||||
|
||||
public function __construct($id = null)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function __invoke(self $entity = null)
|
||||
{
|
||||
if(is_null($entity)){
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $this->put($entity);
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setText($text)
|
||||
{
|
||||
$this->data['text'] = (string) $text;
|
||||
}
|
||||
|
||||
public function setVoiceName($name)
|
||||
{
|
||||
$this->data['voice_name'] = (string) $name;
|
||||
}
|
||||
|
||||
public function setLoop($times)
|
||||
{
|
||||
$this->data['loop'] = (int) $times;
|
||||
}
|
||||
|
||||
public function put($talk = null)
|
||||
{
|
||||
if(!$talk){
|
||||
$talk = $this;
|
||||
}
|
||||
|
||||
$request = new Request(
|
||||
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() . '/talk',
|
||||
'PUT',
|
||||
'php://temp',
|
||||
['content-type' => 'application/json']
|
||||
);
|
||||
|
||||
$request->getBody()->write(json_encode($talk));
|
||||
$response = $this->client->send($request);
|
||||
return $this->parseEventResponse($response);
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$request = new Request(
|
||||
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() . '/talk',
|
||||
'DELETE'
|
||||
);
|
||||
|
||||
$response = $this->client->send($request);
|
||||
return $this->parseEventResponse($response);
|
||||
}
|
||||
|
||||
protected function parseEventResponse(ResponseInterface $response)
|
||||
{
|
||||
if($response->getStatusCode() != '200'){
|
||||
throw $this->getException($response);
|
||||
}
|
||||
|
||||
$json = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
if(!$json){
|
||||
throw new Exception\Exception('Unexpected Response Body Format');
|
||||
}
|
||||
|
||||
return new Event($json);
|
||||
}
|
||||
|
||||
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_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');
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if(!in_array($offset, $this->params)){
|
||||
throw new \RuntimeException('invalid parameter: ' . $offset);
|
||||
}
|
||||
|
||||
$this->data[$offset] = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
if(!in_array($offset, $this->params)){
|
||||
throw new \RuntimeException('invalid parameter: ' . $offset);
|
||||
}
|
||||
|
||||
unset($this->data[$offset]);
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Call;
|
||||
|
||||
|
||||
class Transfer implements \JsonSerializable
|
||||
{
|
||||
protected $urls;
|
||||
|
||||
public function __construct($urls)
|
||||
{
|
||||
if(!is_array($urls)){
|
||||
$urls = array($urls);
|
||||
}
|
||||
|
||||
$this->urls = $urls;
|
||||
}
|
||||
|
||||
function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'action' => 'transfer',
|
||||
'destination' => [
|
||||
'type' => 'ncco',
|
||||
'url' => $this->urls
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Call;
|
||||
|
||||
|
||||
class Unearmuff implements \JsonSerializable
|
||||
{
|
||||
function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'action' => 'unearmuff'
|
||||
];
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Nexmo Client Library for PHP
|
||||
*
|
||||
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
|
||||
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
|
||||
*/
|
||||
|
||||
namespace Nexmo\Call;
|
||||
|
||||
|
||||
class Unmute implements \JsonSerializable
|
||||
{
|
||||
function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'action' => 'unmute'
|
||||
];
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
<?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\Call;
|
||||
|
||||
|
||||
class Webhook implements \JsonSerializable
|
||||
{
|
||||
protected $urls;
|
||||
|
||||
protected $method;
|
||||
|
||||
protected $type;
|
||||
|
||||
public function __construct($type, $urls, $method = null)
|
||||
{
|
||||
if(!is_array($urls)){
|
||||
$urls = [$urls];
|
||||
}
|
||||
|
||||
$this->urls = $urls;
|
||||
$this->type = $type;
|
||||
$this->method = $method;
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function add($url)
|
||||
{
|
||||
$this->urls[] = $url;
|
||||
}
|
||||
|
||||
function jsonSerialize()
|
||||
{
|
||||
$data = [
|
||||
$this->type . '_url' => $this->urls
|
||||
];
|
||||
|
||||
if(isset($this->method)){
|
||||
$data[$this->type . '_method'] = $this->method;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user