Browse files

dumping twilio includes

  • Loading branch information...
1 parent 1484989 commit 3e0fd994a22a34b202629312bf42769ccbf039cb @claytantor claytantor committed Jan 26, 2013
Showing with 0 additions and 1,828 deletions.
  1. +0 −166 include/Services/Twilio.php
  2. +0 −78 include/Services/Twilio/ArrayDataProxy.php
  3. +0 −78 include/Services/Twilio/AutoPagingIterator.php
  4. +0 −163 include/Services/Twilio/CachingDataProxy.php
  5. +0 −346 include/Services/Twilio/Capability.php
  6. +0 −44 include/Services/Twilio/DataProxy.php
  7. +0 −55 include/Services/Twilio/InstanceResource.php
  8. +0 −148 include/Services/Twilio/ListResource.php
  9. +0 −74 include/Services/Twilio/Page.php
  10. +0 −41 include/Services/Twilio/PartialApplicationHelper.php
  11. +0 −36 include/Services/Twilio/RequestValidator.php
  12. +0 −85 include/Services/Twilio/Resource.php
  13. +0 −26 include/Services/Twilio/Rest/Account.php
  14. +0 −10 include/Services/Twilio/Rest/Accounts.php
  15. +0 −6 include/Services/Twilio/Rest/Application.php
  16. +0 −12 include/Services/Twilio/Rest/Applications.php
  17. +0 −7 include/Services/Twilio/Rest/AvailablePhoneNumber.php
  18. +0 −30 include/Services/Twilio/Rest/AvailablePhoneNumbers.php
  19. +0 −18 include/Services/Twilio/Rest/Call.php
  20. +0 −26 include/Services/Twilio/Rest/Calls.php
  21. +0 −12 include/Services/Twilio/Rest/Conference.php
  22. +0 −6 include/Services/Twilio/Rest/Conferences.php
  23. +0 −6 include/Services/Twilio/Rest/IncomingPhoneNumber.php
  24. +0 −10 include/Services/Twilio/Rest/IncomingPhoneNumbers.php
  25. +0 −6 include/Services/Twilio/Rest/Notification.php
  26. +0 −6 include/Services/Twilio/Rest/Notifications.php
  27. +0 −6 include/Services/Twilio/Rest/OutgoingCallerId.php
  28. +0 −12 include/Services/Twilio/Rest/OutgoingCallerIds.php
  29. +0 −10 include/Services/Twilio/Rest/Participant.php
  30. +0 −6 include/Services/Twilio/Rest/Participants.php
  31. +0 −6 include/Services/Twilio/Rest/Recording.php
  32. +0 −6 include/Services/Twilio/Rest/Recordings.php
  33. +0 −6 include/Services/Twilio/Rest/Sandbox.php
  34. +0 −6 include/Services/Twilio/Rest/ShortCode.php
  35. +0 −6 include/Services/Twilio/Rest/ShortCodes.php
  36. +0 −6 include/Services/Twilio/Rest/SmsMessage.php
  37. +0 −24 include/Services/Twilio/Rest/SmsMessages.php
  38. +0 −6 include/Services/Twilio/Rest/Transcription.php
  39. +0 −6 include/Services/Twilio/Rest/Transcriptions.php
  40. +0 −25 include/Services/Twilio/RestException.php
  41. +0 −99 include/Services/Twilio/TinyHttp.php
  42. +0 −103 include/Services/Twilio/Twiml.php
View
166 include/Services/Twilio.php
@@ -1,166 +0,0 @@
-<?php
-
-function Services_Twilio_autoload($className) {
- if (substr($className, 0, 15) != 'Services_Twilio') {
- return false;
- }
- $file = str_replace('_', '/', $className);
- $file = str_replace('Services/', '', $file);
- return include dirname(__FILE__) . "/$file.php";
-}
-
-spl_autoload_register('Services_Twilio_autoload');
-
-/**
- * Twilio API client interface.
- *
- * @category Services
- * @package Services_Twilio
- * @author Neuman Vong <neuman@twilio.com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- * @link http://pear.php.net/package/Services_Twilio
- */
-class Services_Twilio extends Services_Twilio_Resource
-{
- const USER_AGENT = 'twilio-php/3.2.2';
-
- protected $http;
- protected $version;
-
- /**
- * Constructor.
- *
- * @param string $sid Account SID
- * @param string $token Account auth token
- * @param string $version API version
- * @param Services_Twilio_Http $_http A HTTP client
- */
- public function __construct(
- $sid,
- $token,
- $version = '2010-04-01',
- Services_Twilio_TinyHttp $_http = null
- ) {
- $this->version = $version;
- if (null === $_http) {
- $_http = new Services_Twilio_TinyHttp(
- "https://api.twilio.com",
- array("curlopts" => array(CURLOPT_USERAGENT => self::USER_AGENT))
- );
- }
- $_http->authenticate($sid, $token);
- $this->http = $_http;
- $this->accounts = new Services_Twilio_Rest_Accounts($this);
- $this->account = $this->accounts->get($sid);
- }
-
- /**
- * GET the resource at the specified path.
- *
- * @param string $path Path to the resource
- * @param array $params Query string parameters
- *
- * @return object The object representation of the resource
- */
- public function retrieveData($path, array $params = array())
- {
- $path = "/$this->version/$path.json";
- return empty($params)
- ? $this->_processResponse($this->http->get($path))
- : $this->_processResponse(
- $this->http->get("$path?" . http_build_query($params, '', '&'))
- );
- }
-
- /**
- * DELETE the resource at the specified path.
- *
- * @param string $path Path to the resource
- * @param array $params Query string parameters
- *
- * @return object The object representation of the resource
- */
- public function deleteData($path, array $params = array())
- {
- $path = "/$this->version/$path.json";
- return empty($params)
- ? $this->_processResponse($this->http->delete($path))
- : $this->_processResponse(
- $this->http->delete("$path?" . http_build_query($params, '', '&'))
- );
- }
-
- /**
- * POST to the resource at the specified path.
- *
- * @param string $path Path to the resource
- * @param array $params Query string parameters
- *
- * @return object The object representation of the resource
- */
- public function createData($path, array $params = array())
- {
- $path = "/$this->version/$path.json";
- $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
- return empty($params)
- ? $this->_processResponse($this->http->post($path, $headers))
- : $this->_processResponse(
- $this->http->post(
- $path,
- $headers,
- http_build_query($params, '', '&')
- )
- );
- }
-
- /**
- * Convert the JSON encoded resource into a PHP object.
- *
- * @param array $response 3-tuple containing status, headers, and body
- *
- * @return object PHP object decoded from JSON
- */
- private function _processResponse($response)
- {
- list($status, $headers, $body) = $response;
- if ($status == 204) {
- return TRUE;
- }
- if (empty($headers['Content-Type'])) {
- throw new DomainException('Response header is missing Content-Type');
- }
- switch ($headers['Content-Type']) {
- case 'application/json':
- return $this->_processJsonResponse($status, $headers, $body);
- break;
- case 'text/xml':
- return $this->_processXmlResponse($status, $headers, $body);
- break;
- }
- throw new DomainException(
- 'Unexpected content type: ' . $headers['Content-Type']);
- }
-
- private function _processJsonResponse($status, $headers, $body) {
- $decoded = json_decode($body);
- if (200 <= $status && $status < 300) {
- return $decoded;
- }
- throw new Services_Twilio_RestException(
- (int)$decoded->status,
- $decoded->message,
- isset($decoded->code) ? $decoded->code : null,
- isset($decoded->more_info) ? $decoded->more_info : null
- );
- }
-
- private function _processXmlResponse($status, $headers, $body) {
- $decoded = simplexml_load_string($body);
- throw new Services_Twilio_RestException(
- (int)$decoded->Status,
- (string)$decoded->Message,
- (string)$decoded->Code,
- (string)$decoded->MoreInfo
- );
- }
-}
View
78 include/Services/Twilio/ArrayDataProxy.php
@@ -1,78 +0,0 @@
-<?php
-
-/**
- * DataProxy implementation using an array for storage backend.
- *
- * @category Services
- * @package Services_Twilio
- * @author Neuman Vong <neuman@twilio.com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- * @link http://pear.php.net/package/Services_Twilio
- */
-class Services_Twilio_ArrayDataProxy
- implements Services_Twilio_DataProxy
-{
- protected $array;
-
- /**
- * Constructor.
- *
- * @param array $array Array representation
- */
- public function __construct($array)
- {
- $this->array = $array;
- }
-
- /**
- * Implementation of retrieveData.
- *
- * @param string $key The index
- * @param array $params Optional parameters
- *
- * @return object Object representation
- */
- function retrieveData($key, array $params = array())
- {
- return (object) $this->array;
- }
-
- /**
- * Implementation of createData.
- *
- * @param string $key The index
- * @param array $params Optional parameters
- *
- * @return object Object representation
- */
- function createData($key, array $params = array())
- {
- return (object) $this->array;
- }
-
- /**
- * Implementation of updateData.
- *
- * @param array $params Update parameters
- *
- * @return object Object representation
- */
- function updateData(array $params)
- {
- return $this->array;
- }
-
- /**
- * Implementation of magic method __get.
- *
- * @param string $prop The name of the property to get
- *
- * @return mixed The value of the property
- */
- function __get($prop)
- {
- return is_array($this->array)
- ? $this->array['prop']
- : $this->array->$prop;
- }
-}
View
78 include/Services/Twilio/AutoPagingIterator.php
@@ -1,78 +0,0 @@
-<?php
-
-class Services_Twilio_AutoPagingIterator
- implements Iterator
-{
- protected $generator;
- protected $promoter;
- protected $args;
- protected $items;
-
- private $_args;
-
- public function __construct($generator, $promoter, array $args) {
- $this->generator = $generator;
- $this->promoter = $promoter;
- $this->args = $args;
- $this->items = array();
-
- // Save a backup for rewind()
- $this->_args = $args;
- }
-
- public function current()
- {
- return current($this->items);
- }
-
- public function key()
- {
- return key($this->items);
- }
-
- public function next()
- {
- try {
- $this->loadIfNecessary();
- return next($this->items);
- }
- catch (Services_Twilio_RestException $e) {
- // Swallow the out-of-range error
- }
- }
-
- public function rewind()
- {
- $this->args = $this->_args;
- $this->items = array();
- }
-
- public function count()
- {
- throw new BadMethodCallException('Not allowed');
- }
-
- public function valid()
- {
- try {
- $this->loadIfNecessary();
- return key($this->items) !== null;
- }
- catch (Services_Twilio_RestException $e) {
- // Swallow the out-of-range error
- }
- return false;
- }
-
- protected function loadIfNecessary()
- {
- if (// Empty because it's the first time or last page was empty
- empty($this->items)
- // null key when the items list is iterated over completely
- || key($this->items) === null
- ) {
- $this->items = call_user_func_array($this->generator, $this->args);
- $this->args = call_user_func_array($this->promoter, $this->args);
- }
- }
-}
View
163 include/Services/Twilio/CachingDataProxy.php
@@ -1,163 +0,0 @@
-<?php
-
-/**
- * DataProxy implmentation that caches the output of the proxy it wraps.
- *
- * @category Services
- * @package Services_Twilio
- * @author Neuman Vong <neuman@twilio.com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- * @link http://pear.php.net/package/Services_Twilio
- */
-class Services_Twilio_CachingDataProxy
- implements Services_Twilio_DataProxy
-{
- /**
- * The proxy being wrapped.
- *
- * @var DataProxy $proxy
- */
- protected $proxy;
-
- /**
- * The principal data used to retrieve an object from the proxy.
- *
- * @var array $principal
- */
- protected $principal;
-
- /**
- * The object cache.
- *
- * @var object $cache
- */
- protected $cache;
-
- /**
- * Constructor.
- *
- * @param array $principal Usually the SID
- * @param Services_Twilio_DataProxy $proxy The proxy
- * @param object|null $cache The cache
- */
- public function __construct($principal, Services_Twilio_DataProxy $proxy,
- $cache = null
- ) {
- if (is_scalar($principal)) {
- $principal = array('sid' => $principal, 'params' => array());
- }
- $this->principal = $principal;
- $this->proxy = $proxy;
- $this->cache = $cache;
- }
-
- /**
- * Set the object cache.
- *
- * @param object $object The new object
- *
- * @return null
- */
- public function setCache($object)
- {
- $this->cache = $object;
- }
-
- /**
- * Implementation of magic method __get.
- *
- * @param string $prop The name of the property to get
- *
- * @return mixed The value of the property
- */
- public function __get($prop)
- {
- if ($prop == 'sid') {
- return $this->principal['sid'];
- }
- if (empty($this->cache)) {
- $this->_load();
- }
- return isset($this->cache->$prop)
- ? $this->cache->$prop
- : null;
- }
-
- /**
- * Implementation of retrieveData.
- *
- * @param string $path The path
- * @param array $params Optional parameters
- *
- * @return object Object representation
- */
- public function retrieveData($path, array $params = array())
- {
- return $this->proxy->retrieveData(
- $this->principal['sid'] . "/$path",
- $params
- );
- }
-
- /**
- * Implementation of createData.
- *
- * @param string $path The path
- * @param array $params Optional parameters
- *
- * @return object Object representation
- */
- public function createData($path, array $params = array())
- {
- return $this->proxy->createData(
- $this->principal['sid'] . "/$path",
- $params
- );
- }
-
- /**
- * Implementation of updateData.
- *
- * @param array $params Update parameters
- *
- * @return object Object representation
- */
- public function updateData($params)
- {
- $this->cache = $this->proxy->createData(
- $this->principal['sid'],
- $params
- );
- return $this;
- }
-
- /**
- * Implementation of deleteData.
- *
- * @param string $path The path
- * @param array $params Optional parameters
- *
- * @return null
- */
- public function deleteData($path, array $params = array())
- {
- $this->proxy->delete(
- $this->principal['sid'] . "/$path",
- $params
- );
- }
-
- /**
- * Retrieves object from proxy into cache, then initializes subresources.
- *
- * @param object|null $object The object
- *
- * @return null
- */
- private function _load($object = null)
- {
- $this->cache = $object !== null
- ? $object
- : $this->proxy->retrieveData($this->principal['sid']);
- }
-}
View
346 include/Services/Twilio/Capability.php
@@ -1,346 +0,0 @@
-<?php
-
-/**
- * Twilio Capability Token generator
- *
- * @category Services
- * @package Services_Twilio
- * @author Jeff Lindsay <jeff.lindsay@twilio.com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- */
-class Services_Twilio_Capability
-{
- public $accountSid;
- public $authToken;
- public $scopes;
-
- /**
- * Create a new TwilioCapability with zero permissions. Next steps are to
- * grant access to resources by configuring this token through the
- * functions allowXXXX.
- *
- * @param $accountSid the account sid to which this token is granted access
- * @param $authToken the secret key used to sign the token. Note, this auth
- * token is not visible to the user of the token.
- */
- public function __construct($accountSid, $authToken)
- {
- $this->accountSid = $accountSid;
- $this->authToken = $authToken;
- $this->scopes = array();
- $this->clientName = false;
- }
-
- /**
- * If the user of this token should be allowed to accept incoming
- * connections then configure the TwilioCapability through this method and
- * specify the client name.
- *
- * @param $clientName
- */
- public function allowClientIncoming($clientName)
- {
-
- // clientName must be a non-zero length alphanumeric string
- if (preg_match('/\W/', $clientName)) {
- throw new InvalidArgumentException(
- 'Only alphanumeric characters allowed in client name.');
- }
-
- if (strlen($clientName) == 0) {
- throw new InvalidArgumentException(
- 'Client name must not be a zero length string.');
- }
-
- $this->clientName = $clientName;
- $this->allow('client', 'incoming',
- array('clientName' => $clientName));
- }
-
- /**
- * Allow the user of this token to make outgoing connections.
- *
- * @param $appSid the application to which this token grants access
- * @param $appParams signed parameters that the user of this token cannot
- * overwrite.
- */
- public function allowClientOutgoing($appSid, array $appParams=array())
- {
- $this->allow('client', 'outgoing', array(
- 'appSid' => $appSid,
- 'appParams' => http_build_query($appParams)));
- }
-
- /**
- * Allow the user of this token to access their event stream.
- *
- * @param $filters key/value filters to apply to the event stream
- */
- public function allowEventStream(array $filters=array())
- {
- $this->allow('stream', 'subscribe', array(
- 'path' => '/2010-04-01/Events',
- 'params' => http_build_query($filters),
- ));
- }
-
- /**
- * Generates a new token based on the credentials and permissions that
- * previously has been granted to this token.
- *
- * @param $ttl the expiration time of the token (in seconds). Default
- * value is 3600 (1hr)
- * @return the newly generated token that is valid for $ttl seconds
- */
- public function generateToken($ttl = 3600)
- {
- $payload = array(
- 'scope' => array(),
- 'iss' => $this->accountSid,
- 'exp' => time() + $ttl,
- );
- $scopeStrings = array();
-
- foreach ($this->scopes as $scope) {
- if ($scope->privilege == "outgoing" && $this->clientName)
- $scope->params["clientName"] = $this->clientName;
- $scopeStrings[] = $scope->toString();
- }
-
- $payload['scope'] = implode(' ', $scopeStrings);
- return JWT::encode($payload, $this->authToken, 'HS256');
- }
-
- protected function allow($service, $privilege, $params) {
- $this->scopes[] = new ScopeURI($service, $privilege, $params);
- }
-}
-
-/**
- * Scope URI implementation
- *
- * Simple way to represent configurable privileges in an OAuth
- * friendly way. For our case, they look like this:
- *
- * scope:<service>:<privilege>?<params>
- *
- * For example:
- * scope:client:incoming?name=jonas
- *
- * @author Jeff Lindsay <jeff.lindsay@twilio.com>
- */
-class ScopeURI
-{
- public $service;
- public $privilege;
- public $params;
-
- public function __construct($service, $privilege, $params = array())
- {
- $this->service = $service;
- $this->privilege = $privilege;
- $this->params = $params;
- }
-
- public function toString()
- {
- $uri = "scope:{$this->service}:{$this->privilege}";
- if (count($this->params)) {
- $uri .= "?".http_build_query($this->params);
- }
- return $uri;
- }
-
- /**
- * Parse a scope URI into a ScopeURI object
- *
- * @param string $uri The scope URI
- * @return ScopeURI The parsed scope uri
- */
- public static function parse($uri)
- {
- if (strpos($uri, 'scope:') !== 0) {
- throw new UnexpectedValueException(
- 'Not a scope URI according to scheme');
- }
-
- $parts = explode('?', $uri, 1);
- $params = null;
-
- if (count($parts) > 1) {
- parse_str($parts[1], $params);
- }
-
- $parts = explode(':', $parts[0], 2);
-
- if (count($parts) != 3) {
- throw new UnexpectedValueException(
- 'Not enough parts for scope URI');
- }
-
- list($scheme, $service, $privilege) = $parts;
- return new ScopeURI($service, $privilege, $params);
- }
-
-}
-
-/**
- * JSON Web Token implementation
- *
- * Minimum implementation used by Realtime auth, based on this spec:
- * http://self-issued.info/docs/draft-jones-json-web-token-01.html.
- *
- * @author Neuman Vong <neuman@twilio.com>
- */
-class JWT
-{
- /**
- * @param string $jwt The JWT
- * @param string|null $key The secret key
- * @param bool $verify Don't skip verification process
- *
- * @return object The JWT's payload as a PHP object
- */
- public static function decode($jwt, $key = null, $verify = true)
- {
- $tks = explode('.', $jwt);
- if (count($tks) != 3) {
- throw new UnexpectedValueException('Wrong number of segments');
- }
- list($headb64, $payloadb64, $cryptob64) = $tks;
- if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))
- ) {
- throw new UnexpectedValueException('Invalid segment encoding');
- }
- if (null === $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($payloadb64))
- ) {
- throw new UnexpectedValueException('Invalid segment encoding');
- }
- $sig = JWT::urlsafeB64Decode($cryptob64);
- if ($verify) {
- if (empty($header->alg)) {
- throw new DomainException('Empty algorithm');
- }
- if ($sig != JWT::sign("$headb64.$payloadb64", $key, $header->alg)) {
- throw new UnexpectedValueException('Signature verification failed');
- }
- }
- return $payload;
- }
-
- /**
- * @param object|array $payload PHP object or array
- * @param string $key The secret key
- * @param string $algo The signing algorithm
- *
- * @return string A JWT
- */
- public static function encode($payload, $key, $algo = 'HS256')
- {
- $header = array('typ' => 'JWT', 'alg' => $algo);
-
- $segments = array();
- $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
- $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));
- $signing_input = implode('.', $segments);
-
- $signature = JWT::sign($signing_input, $key, $algo);
- $segments[] = JWT::urlsafeB64Encode($signature);
-
- return implode('.', $segments);
- }
-
- /**
- * @param string $msg The message to sign
- * @param string $key The secret key
- * @param string $method The signing algorithm
- *
- * @return string An encrypted message
- */
- public static function sign($msg, $key, $method = 'HS256')
- {
- $methods = array(
- 'HS256' => 'sha256',
- 'HS384' => 'sha384',
- 'HS512' => 'sha512',
- );
- if (empty($methods[$method])) {
- throw new DomainException('Algorithm not supported');
- }
- return hash_hmac($methods[$method], $msg, $key, true);
- }
-
- /**
- * @param string $input JSON string
- *
- * @return object Object representation of JSON string
- */
- public static function jsonDecode($input)
- {
- $obj = json_decode($input);
- if (function_exists('json_last_error') && $errno = json_last_error()) {
- JWT::handleJsonError($errno);
- }
- else if ($obj === null && $input !== 'null') {
- throw new DomainException('Null result with non-null input');
- }
- return $obj;
- }
-
- /**
- * @param object|array $input A PHP object or array
- *
- * @return string JSON representation of the PHP object or array
- */
- public static function jsonEncode($input)
- {
- $json = json_encode($input);
- if (function_exists('json_last_error') && $errno = json_last_error()) {
- JWT::handleJsonError($errno);
- }
- else if ($json === 'null' && $input !== null) {
- throw new DomainException('Null result with non-null input');
- }
- return $json;
- }
-
- /**
- * @param string $input A base64 encoded string
- *
- * @return string A decoded string
- */
- public static function urlsafeB64Decode($input)
- {
- $padlen = 4 - strlen($input) % 4;
- $input .= str_repeat('=', $padlen);
- return base64_decode(strtr($input, '-_', '+/'));
- }
-
- /**
- * @param string $input Anything really
- *
- * @return string The base64 encode of what you passed in
- */
- public static function urlsafeB64Encode($input)
- {
- return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
- }
-
- /**
- * @param int $errno An error number from json_last_error()
- *
- * @return void
- */
- private static function handleJsonError($errno)
- {
- $messages = array(
- JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
- JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
- JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON'
- );
- throw new DomainException(isset($messages[$errno])
- ? $messages[$errno]
- : 'Unknown JSON error: ' . $errno
- );
- }
-}
View
44 include/Services/Twilio/DataProxy.php
@@ -1,44 +0,0 @@
-<?php
-
-/**
- * DataProxy interface.
- *
- * @category Services
- * @package Services_Twilio
- * @author Neuman Vong <neuman@twilio.com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- * @link http://pear.php.net/package/Services_Twilio
- */
-interface Services_Twilio_DataProxy
-{
- /**
- * Retrieve the object specified by key.
- *
- * @param string $key The index
- * @param array $params Optional parameters
- *
- * @return object The object
- */
- function retrieveData($key, array $params = array());
-
- /**
- * Create the object specified by key.
- *
- * @param string $key The index
- * @param array $params Optional parameters
- *
- * @return object The object
- */
- function createData($key, array $params = array());
-
- /**
- * Delete the object specified by key.
- *
- * @param string $key The index
- * @param array $params Optional parameters
- *
- * @return null
- */
- function deleteData($key, array $params = array());
-}
-
View
55 include/Services/Twilio/InstanceResource.php
@@ -1,55 +0,0 @@
-<?php
-
-/**
- * Abstraction of an instance resource from the Twilio API.
- *
- * @category Services
- * @package Services_Twilio
- * @author Neuman Vong <neuman@twilio.com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- * @link http://pear.php.net/package/Services_Twilio
- */
-abstract class Services_Twilio_InstanceResource
- extends Services_Twilio_Resource
-{
- /**
- * @param mixed $params An array of updates, or a property name
- * @param mixed $value A value with which to update the resource
- *
- * @return null
- */
- public function update($params, $value = null)
- {
- if (!is_array($params)) {
- $params = array($params => $value);
- }
- $this->proxy->updateData($params);
- }
-
- /**
- * Set this resource's proxy.
- *
- * @param Services_Twilio_DataProxy $proxy An instance of DataProxy
- *
- * @return null
- */
- public function setProxy($proxy)
- {
- $this->proxy = $proxy;
- }
-
- /**
- * Get the value of a property on this resource.
- *
- * @param string $key The property name
- *
- * @return mixed Could be anything.
- */
- public function __get($key)
- {
- if ($subresource = $this->getSubresources($key)) {
- return $subresource;
- }
- return $this->proxy->$key;
- }
-}
View
148 include/Services/Twilio/ListResource.php
@@ -1,148 +0,0 @@
-<?php
-
-/**
- * Abstraction of a list resource from the Twilio API.
- *
- * @category Services
- * @package Services_Twilio
- * @author Neuman Vong <neuman@twilio.com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- * @link http://pear.php.net/package/Services_Twilio
- */
-abstract class Services_Twilio_ListResource
- extends Services_Twilio_Resource
- implements IteratorAggregate
-{
- private $_page;
-
- /**
- * Gets a resource from this list.
- *
- * @param string $sid The resource SID
- * @return Services_Twilio_InstanceResource The resource
- */
- public function get($sid)
- {
- $schema = $this->getSchema();
- $type = $schema['instance'];
- return new $type(is_object($sid)
- ? new Services_Twilio_CachingDataProxy(
- isset($sid->sid) ? $sid->sid : NULL, $this, $sid
- ) : new Services_Twilio_CachingDataProxy($sid, $this));
- }
-
- /**
- * Deletes a resource from this list.
- *
- * @param string $sid The resource SID
- * @return null
- */
- public function delete($sid, array $params = array())
- {
- $schema = $this->getSchema();
- $basename = $schema['basename'];
- $this->proxy->deleteData("$basename/$sid", $params);
- }
-
- /**
- * Create a resource on the list and then return its representation as an
- * InstanceResource.
- *
- * @param array $params The parameters with which to create the resource
- *
- * @return Services_Twilio_InstanceResource The created resource
- */
- protected function _create(array $params)
- {
- $schema = $this->getSchema();
- $basename = $schema['basename'];
- return $this->get($this->proxy->createData($basename, $params));
- }
-
- /**
- * Create a resource on the list and then return its representation as an
- * InstanceResource.
- *
- * @param array $params The parameters with which to create the resource
- *
- * @return Services_Twilio_InstanceResource The created resource
- */
- public function retrieveData($sid, array $params = array())
- {
- $schema = $this->getSchema();
- $basename = $schema['basename'];
- return $this->proxy->retrieveData("$basename/$sid", $params);
- }
-
- /**
- * Create a resource on the list and then return its representation as an
- * InstanceResource.
- *
- * @param array $params The parameters with which to create the resource
- *
- * @return Services_Twilio_InstanceResource The created resource
- */
- public function createData($sid, array $params = array())
- {
- $schema = $this->getSchema();
- $basename = $schema['basename'];
- return $this->proxy->createData("$basename/$sid", $params);
- }
-
- /**
- * Returns a page of InstanceResources from this list.
- *
- * @param int $page The start page
- * @param int $size Number of items per page
- * @param array $size Optional filters
- *
- * @return Services_Twilio_Page A page
- */
- public function getPage($page = 0, $size = 50, array $filters = array())
- {
- $schema = $this->getSchema();
- $page = $this->proxy->retrieveData($schema['basename'], array(
- 'Page' => $page,
- 'PageSize' => $size,
- ) + $filters);
-
- $page->{$schema['list']} = array_map(
- array($this, 'get'),
- $page->{$schema['list']}
- );
-
- return new Services_Twilio_Page($page, $schema['list']);
- }
-
- /**
- * Returns meta data about this list resource type.
- *
- * @return array Meta data
- */
- public function getSchema()
- {
- $name = get_class($this);
- $parts = explode('_', $name);
- $basename = end($parts);
- return array(
- 'name' => $name,
- 'basename' => $basename,
- 'instance' => substr($name, 0, -1),
- 'list' => self::decamelize($basename),
- );
- }
-
- public function getIterator($page = 0, $size = 50, array $filters = array())
- {
- return new Services_Twilio_AutoPagingIterator(
- array($this, 'getPageGenerator'),
- create_function('$page, $size, $filters',
- 'return array($page + 1, $size, $filters);'),
- array($page, $size, $filters)
- );
- }
-
- public function getPageGenerator($page, $size, array $filters = array()) {
- return $this->getPage($page, $size, $filters)->getItems();
- }
-}
View
74 include/Services/Twilio/Page.php
@@ -1,74 +0,0 @@
-<?php
-
-/**
- * A representation of a page of resources.
- *
- * @category Services
- * @package Services_Twilio
- * @author Neuman Vong <neuman@twilio.com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- * @link http://pear.php.net/package/Services_Twilio
- */
-class Services_Twilio_Page
- implements IteratorAggregate
-{
-
- /**
- * The page object.
- *
- * @var object $page
- */
- protected $page;
-
- /**
- * The item list.
- *
- * @var array $items
- */
- protected $items;
-
- /**
- * Constructs a page.
- *
- * @param object $page The page object
- * @param string $name The key of the item list
- */
- public function __construct($page, $name)
- {
- $this->page = $page;
- $this->items = $page->{$name};
- }
-
- /**
- * The item list of the page.
- *
- * @return array A list of instance resources
- */
- public function getItems()
- {
- return $this->items;
- }
-
- /**
- * Magic method to allow retrieving the properties of the wrapped page.
- *
- * @param string $prop The property name
- *
- * @return mixed Could be anything
- */
- public function __get($prop)
- {
- return $this->page->$prop;
- }
-
- /**
- * Implementation of IteratorAggregate::getIterator().
- *
- * @return Traversable
- */
- public function getIterator()
- {
- return $this->getItems();
- }
-}
-
View
41 include/Services/Twilio/PartialApplicationHelper.php
@@ -1,41 +0,0 @@
-<?php
-
-/**
- * Helper class to wrap an object with a modified interface created by
- * a partial application of its existing methods.
- *
- * @category Services
- * @package Services_Twilio
- * @author Neuman Vong <neuman@twilio.com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- * @link http://pear.php.net/package/Services_Twilio
- */
-class Services_Twilio_PartialApplicationHelper
-{
- private $callbacks;
-
- public function __construct()
- {
- $this->callbacks = array();
- }
-
- public function set($method, $callback, array $args)
- {
- if (!is_callable($callback)) {
- return FALSE;
- }
- $this->callbacks[$method] = array($callback, $args);
- }
-
- public function __call($method, $args)
- {
- if (!isset($this->callbacks[$method])) {
- throw new Exception("Method not found: $method");
- }
- list($callback, $cb_args) = $this->callbacks[$method];
- return call_user_func_array(
- $callback,
- array_merge($cb_args, $args)
- );
- }
-}
View
36 include/Services/Twilio/RequestValidator.php
@@ -1,36 +0,0 @@
-<?php
-
-class Services_Twilio_RequestValidator
-{
-
- protected $AuthToken;
-
- function __construct($token)
- {
- $this->AuthToken = $token;
- }
-
- public function computeSignature($url, $data = array())
- {
- // sort the array by keys
- ksort($data);
-
- // append them to the data string in order
- // with no delimiters
- foreach($data as $key => $value)
- $url .= "$key$value";
-
- // This function calculates the HMAC hash of the data with the key
- // passed in
- // Note: hash_hmac requires PHP 5 >= 5.1.2 or PECL hash:1.1-1.5
- // Or http://pear.php.net/package/Crypt_HMAC/
- return base64_encode(hash_hmac("sha1", $url, $this->AuthToken, true));
- }
-
- public function validate($expectedSignature, $url, $data = array())
- {
- return $this->computeSignature($url, $data)
- == $expectedSignature;
- }
-
-}
View
85 include/Services/Twilio/Resource.php
@@ -1,85 +0,0 @@
-<?php
-
-/**
- * Abstraction of a Twilio resource.
- *
- * @category Services
- * @package Services_Twilio
- * @author Neuman Vong <neuman@twilio.com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- * @link http://pear.php.net/package/Services_Twilio
- */
-abstract class Services_Twilio_Resource
- implements Services_Twilio_DataProxy
-{
- protected $name;
- protected $proxy;
- protected $subresources;
-
- public function __construct(Services_Twilio_DataProxy $proxy)
- {
- $this->subresources = array();
- $this->proxy = $proxy;
- $this->name = get_class($this);
- $this->init();
- }
-
- protected function init()
- {
- // Left empty for derived classes to implement
- }
-
- public function retrieveData($path, array $params = array())
- {
- return $this->proxy->retrieveData($path, $params);
- }
-
- public function deleteData($path, array $params = array())
- {
- return $this->proxy->deleteData($path, $params);
- }
-
- public function createData($path, array $params = array())
- {
- return $this->proxy->createData($path, $params);
- }
-
- public function getSubresources($name = null)
- {
- if (isset($name)) {
- return isset($this->subresources[$name])
- ? $this->subresources[$name]
- : null;
- }
- return $this->subresources;
- }
-
- public function addSubresource($name, Services_Twilio_Resource $res)
- {
- $this->subresources[$name] = $res;
- }
-
- protected function setupSubresources()
- {
- foreach (func_get_args() as $name) {
- $constantized = ucfirst(Services_Twilio_Resource::camelize($name));
- $type = "Services_Twilio_Rest_" . $constantized;
- $this->addSubresource($name, new $type($this));
- }
- }
-
- public static function decamelize($word)
- {
- return preg_replace(
- '/(^|[a-z])([A-Z])/e',
- 'strtolower(strlen("\\1") ? "\\1_\\2" : "\\2")',
- $word
- );
- }
-
- public static function camelize($word)
- {
- return preg_replace('/(^|_)([a-z])/e', 'strtoupper("\\2")', $word);
- }
-}
-
View
26 include/Services/Twilio/Rest/Account.php
@@ -1,26 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Account
- extends Services_Twilio_InstanceResource
-{
- protected function init()
- {
- $this->setupSubresources(
- 'applications',
- 'available_phone_numbers',
- 'outgoing_caller_ids',
- 'calls',
- 'conferences',
- 'incoming_phone_numbers',
- 'notifications',
- 'outgoing_callerids',
- 'recordings',
- 'sms_messages',
- 'transcriptions'
- );
-
- $this->sandbox = new Services_Twilio_Rest_Sandbox(
- new Services_Twilio_CachingDataProxy('Sandbox', $this)
- );
- }
-}
View
10 include/Services/Twilio/Rest/Accounts.php
@@ -1,10 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Accounts
- extends Services_Twilio_ListResource
-{
- public function create(array $params = array())
- {
- return parent::_create($params);
- }
-}
View
6 include/Services/Twilio/Rest/Application.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Application
- extends Services_Twilio_InstanceResource
-{
-}
View
12 include/Services/Twilio/Rest/Applications.php
@@ -1,12 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Applications
- extends Services_Twilio_ListResource
-{
- public function create($name, array $params = array())
- {
- return parent::_create(array(
- 'FriendlyName' => $name
- ) + $params);
- }
-}
View
7 include/Services/Twilio/Rest/AvailablePhoneNumber.php
@@ -1,7 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_AvailablePhoneNumber
- extends Services_Twilio_InstanceResource
-{
-}
-
View
30 include/Services/Twilio/Rest/AvailablePhoneNumbers.php
@@ -1,30 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_AvailablePhoneNumbers
- extends Services_Twilio_ListResource
-{
- public function getLocal($country)
- {
- $curried = new Services_Twilio_PartialApplicationHelper();
- $curried->set(
- 'getList',
- array($this, 'getList'),
- array($country, 'Local')
- );
- return $curried;
- }
- public function getTollFree($country)
- {
- $curried = new Services_Twilio_PartialApplicationHelper();
- $curried->set(
- 'getList',
- array($this, 'getList'),
- array($country, 'TollFree')
- );
- return $curried;
- }
- public function getList($country, $type, array $params = array())
- {
- return $this->retrieveData("$country/$type", $params);
- }
-}
View
18 include/Services/Twilio/Rest/Call.php
@@ -1,18 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Call
- extends Services_Twilio_InstanceResource
-{
- public function hangup()
- {
- $this->update('Status', 'completed');
- }
-
- protected function init()
- {
- $this->setupSubresources(
- 'notifications',
- 'recordings'
- );
- }
-}
View
26 include/Services/Twilio/Rest/Calls.php
@@ -1,26 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Calls
- extends Services_Twilio_ListResource
-{
-
- public static function isApplicationSid($value)
- {
- return strlen($value) == 34
- && !(strpos($value, "AP") === false);
- }
-
- public function create($from, $to, $url, array $params = array())
- {
-
- $params["To"] = $to;
- $params["From"] = $from;
-
- if (self::isApplicationSid($url))
- $params["ApplicationSid"] = $url;
- else
- $params["Url"] = $url;
-
- return parent::_create($params);
- }
-}
View
12 include/Services/Twilio/Rest/Conference.php
@@ -1,12 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Conference
- extends Services_Twilio_InstanceResource
-{
- protected function init()
- {
- $this->setupSubresources(
- 'participants'
- );
- }
-}
View
6 include/Services/Twilio/Rest/Conferences.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Conferences
- extends Services_Twilio_ListResource
-{
-}
View
6 include/Services/Twilio/Rest/IncomingPhoneNumber.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_IncomingPhoneNumber
- extends Services_Twilio_InstanceResource
-{
-}
View
10 include/Services/Twilio/Rest/IncomingPhoneNumbers.php
@@ -1,10 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_IncomingPhoneNumbers
- extends Services_Twilio_ListResource
-{
- function create(array $params = array())
- {
- return parent::_create($params);
- }
-}
View
6 include/Services/Twilio/Rest/Notification.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Notification
- extends Services_Twilio_InstanceResource
-{
-}
View
6 include/Services/Twilio/Rest/Notifications.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Notifications
- extends Services_Twilio_ListResource
-{
-}
View
6 include/Services/Twilio/Rest/OutgoingCallerId.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_OutgoingCallerId
- extends Services_Twilio_InstanceResource
-{
-}
View
12 include/Services/Twilio/Rest/OutgoingCallerIds.php
@@ -1,12 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_OutgoingCallerIds
- extends Services_Twilio_ListResource
-{
- public function create($phoneNumber, array $params = array())
- {
- return parent::_create(array(
- 'PhoneNumber' => $phoneNumber,
- ) + $params);
- }
-}
View
10 include/Services/Twilio/Rest/Participant.php
@@ -1,10 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Participant
- extends Services_Twilio_InstanceResource
-{
- public function mute()
- {
- $this->update('Muted', 'true');
- }
-}
View
6 include/Services/Twilio/Rest/Participants.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Participants
- extends Services_Twilio_ListResource
-{
-}
View
6 include/Services/Twilio/Rest/Recording.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Recording
- extends Services_Twilio_InstanceResource
-{
-}
View
6 include/Services/Twilio/Rest/Recordings.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Recordings
- extends Services_Twilio_ListResource
-{
-}
View
6 include/Services/Twilio/Rest/Sandbox.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Sandbox
- extends Services_Twilio_InstanceResource
-{
-}
View
6 include/Services/Twilio/Rest/ShortCode.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_ShortCode
- extends Services_Twilio_InstanceResource
-{
-}
View
6 include/Services/Twilio/Rest/ShortCodes.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Shortcodes
- extends Services_Twilio_ListResource
-{
-}
View
6 include/Services/Twilio/Rest/SmsMessage.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_SmsMessage
- extends Services_Twilio_InstanceResource
-{
-}
View
24 include/Services/Twilio/Rest/SmsMessages.php
@@ -1,24 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_SmsMessages
- extends Services_Twilio_ListResource
-{
- public function getSchema()
- {
- return array(
- 'class' => 'Services_Twilio_Rest_SmsMessages',
- 'basename' => 'SMS/Messages',
- 'instance' => 'Services_Twilio_Rest_SmsMessage',
- 'list' => 'sms_messages',
- );
- }
-
- function create($from, $to, $body, array $params = array())
- {
- return parent::_create(array(
- 'From' => $from,
- 'To' => $to,
- 'Body' => $body
- ) + $params);
- }
-}
View
6 include/Services/Twilio/Rest/Transcription.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Transcription
- extends Services_Twilio_InstanceResource
-{
-}
View
6 include/Services/Twilio/Rest/Transcriptions.php
@@ -1,6 +0,0 @@
-<?php
-
-class Services_Twilio_Rest_Transcriptions
- extends Services_Twilio_ListResource
-{
-}
View
25 include/Services/Twilio/RestException.php
@@ -1,25 +0,0 @@
-<?php
-
-class Services_Twilio_RestException
- extends Exception
-{
- protected $status;
- protected $info;
-
- public function __construct($status, $message, $code = 0, $info = '')
- {
- $this->status = $status;
- $this->info = $info;
- parent::__construct($message, $code);
- }
-
- public function getStatus()
- {
- return $this->status;
- }
-
- public function getInfo()
- {
- return $this->info;
- }
-}
View
99 include/Services/Twilio/TinyHttp.php
@@ -1,99 +0,0 @@
-<?php
-/**
- * Based on TinyHttp from https://gist.github.com/618157.
- * Copyright 2011, Neuman Vong. BSD License.
- */
-
-class Services_Twilio_TinyHttpException extends ErrorException {}
-
-class Services_Twilio_TinyHttp {
- var $user, $pass, $scheme, $host, $port, $debug, $curlopts;
-
- public function __construct($uri = '', $kwargs = array()) {
- foreach (parse_url($uri) as $name => $value) $this->$name = $value;
- $this->debug = isset($kwargs['debug']) ? !!$kwargs['debug'] : NULL;
- $this->curlopts = isset($kwargs['curlopts']) ? $kwargs['curlopts'] : array();
- }
-
- public function __call($name, $args) {
- list($res, $req_headers, $req_body) = $args + array(0, array(), '');
-
- $opts = $this->curlopts + array(
- CURLOPT_URL => "$this->scheme://$this->host$res",
- CURLOPT_CAINFO => '/opt/bitnami/common/openssl/certs/curl-ca-bundle.crt',
- CURLOPT_HEADER => TRUE,
- CURLOPT_RETURNTRANSFER => TRUE,
- CURLOPT_INFILESIZE => -1,
- CURLOPT_POSTFIELDS => NULL,
- CURLOPT_TIMEOUT => 60,
- );
-
- foreach ($req_headers as $k => $v) $opts[CURLOPT_HTTPHEADER][] = "$k: $v";
- if ($this->port) $opts[CURLOPT_PORT] = $this->port;
- if ($this->debug) $opts[CURLINFO_HEADER_OUT] = TRUE;
- if ($this->user && $this->pass) $opts[CURLOPT_USERPWD] = "$this->user:$this->pass";
- switch ($name) {
- case 'get':
- $opts[CURLOPT_HTTPGET] = TRUE;
- break;
- case 'post':
- $opts[CURLOPT_POST] = TRUE;
- $opts[CURLOPT_POSTFIELDS] = $req_body;
- break;
- case 'put':
- $opts[CURLOPT_PUT] = TRUE;
- if (strlen($req_body)) {
- if ($buf = fopen('php://memory', 'w+')) {
- fwrite($buf, $req_body);
- fseek($buf, 0);
- $opts[CURLOPT_INFILE] = $buf;
- $opts[CURLOPT_INFILESIZE] = strlen($req_body);
- } else throw new Services_Twilio_TinyHttpException('unable to open temporary file');
- }
- break;
- case 'head':
- $opts[CURLOPT_NOBODY] = TRUE;
- break;
- default:
- $opts[CURLOPT_CUSTOMREQUEST] = strtoupper($name);
- break;
- }
- try {
- if ($curl = curl_init()) {
- if (curl_setopt_array($curl, $opts)) {
- if ($response = curl_exec($curl)) {
- $parts = explode("\r\n\r\n", $response, 3);
- list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue')
- ? array($parts[1], $parts[2])
- : array($parts[0], $parts[1]);
- $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if ($this->debug) {
- error_log(
- curl_getinfo($curl, CURLINFO_HEADER_OUT) .
- $req_body
- );
- }
- $header_lines = explode("\r\n", $head);
- array_shift($header_lines);
- foreach ($header_lines as $line) {
- list($key, $value) = explode(":", $line, 2);
- $headers[$key] = trim($value);
- }
- curl_close($curl);
- if (isset($buf) && is_resource($buf)) fclose($buf);
- return array($status, $headers, $body);
- } else throw new Services_Twilio_TinyHttpException(curl_error($curl));
- } else throw new Services_Twilio_TinyHttpException(curl_error($curl));
- } else throw new Services_Twilio_TinyHttpException('unable to initialize cURL');
- } catch (ErrorException $e) {
- if (is_resource($curl)) curl_close($curl);
- if (isset($buf) && is_resource($buf)) fclose($buf);
- throw $e;
- }
- }
-
- public function authenticate($user, $pass) {
- $this->user = $user;
- $this->pass = $pass;
- }
-}
View
103 include/Services/Twilio/Twiml.php
@@ -1,103 +0,0 @@
-<?php
-
-/**
- * Excepttion class for Services_Twilio_Twiml.
- */
-class Services_Twilio_TwimlException extends Exception {}
-
-/**
- * Twiml response generator.
- *
- * @category Services
- * @package Services_Twilio
- * @author Neuman Vong <neuman at ashmoremusic dot com>
- * @license http://creativecommons.org/licenses/MIT/ MIT
- * @link https://gist.github.com/855985
- */
-class Services_Twilio_Twiml
-{
-
- protected $element;
-
- /**
- * Constructs a Twiml response.
- *
- * @param SimpleXmlElement|array $arg:
- * - the element to wrap
- * - attributes to add to the element
- * - if null, initialize an empty element named 'Response'
- */
- public function __construct($arg = null)
- {
- switch (true) {
- case $arg instanceof SimpleXmlElement:
- $this->element = $arg;
- break;
- case $arg === null:
- $this->element = new SimpleXmlElement('<Response/>');
- break;
- case is_array($arg):
- $this->element = new SimpleXmlElement('<Response/>');
- foreach ($arg as $name => $value) {
- $this->element->addAttribute($name, $value);
- }
- break;
- default:
- throw new TwimlException('Invalid argument');
- }
- }
-
- /**
- * Converts method calls into Twiml verbs.
- *
- * A basic example:
- *
- * php> print $this->say('hello');
- * <Say>hello</Say>
- *
- * An example with attributes:
- *
- * php> print $this->say('hello', array('voice' => 'woman'));
- * <Say voice="woman">hello</Say>
- *
- * You could even just pass in an attributes array, omitting the noun:
- *
- * php> print $this->gather(array('timeout' => '20'));
- * <Gather timeout="20"/>
- *
- * @param string $verb The Twiml verb.
- * @param array $args:
- * - (noun string)
- * - (noun string, attributes array)
- * - (attributes array)
- *
- * @return SimpleXmlElement A SimpleXmlElement
- */
- public function __call($verb, array $args)
- {
- list($noun, $attrs) = $args + array('', array());
- if (is_array($noun)) {
- list($attrs, $noun) = array($noun, '');
- }
- $child = empty($noun)
- ? $this->element->addChild(ucfirst($verb))
- : $this->element->addChild(ucfirst($verb), $noun);
- foreach ($attrs as $name => $value) {
- $child->addAttribute($name, $value);
- }
- return new self($child);
- }
-
- /**
- * Returns the object as XML.
- *
- * @return string The response as an XML string
- */
- public function __toString()
- {
- $xml = $this->element->asXml();
- return str_replace(
- '<?xml version="1.0"?>',
- '<?xml version="1.0" encoding="UTF-8"?>', $xml);
- }
-}

0 comments on commit 3e0fd99

Please sign in to comment.