[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Abstraction of a Twilio resource. 5 * 6 * @category Services 7 * @package Services_Twilio 8 * @author Neuman Vong <[email protected]> 9 * @license http://creativecommons.org/licenses/MIT/ MIT 10 * @link http://pear.php.net/package/Services_Twilio 11 */ 12 abstract class Services_Twilio_Resource { 13 protected $subresources; 14 15 public function __construct($client, $uri, $params = array()) 16 { 17 $this->subresources = array(); 18 $this->client = $client; 19 20 foreach ($params as $name => $param) { 21 $this->$name = $param; 22 } 23 24 $this->uri = $uri; 25 $this->init($client, $uri); 26 } 27 28 protected function init($client, $uri) 29 { 30 // Left empty for derived classes to implement 31 } 32 33 public function getSubresources($name = null) { 34 if (isset($name)) { 35 return isset($this->subresources[$name]) 36 ? $this->subresources[$name] 37 : null; 38 } 39 return $this->subresources; 40 } 41 42 protected function setupSubresources() 43 { 44 foreach (func_get_args() as $name) { 45 $constantized = ucfirst(self::camelize($name)); 46 $type = "Services_Twilio_Rest_" . $constantized; 47 $this->subresources[$name] = new $type( 48 $this->client, $this->uri . "/$constantized" 49 ); 50 } 51 } 52 53 /* 54 * Get the resource name from the classname 55 * 56 * Ex: Services_Twilio_Rest_Accounts -> Accounts 57 * 58 * @param boolean $camelized Whether to return camel case or not 59 */ 60 public function getResourceName($camelized = false) 61 { 62 $name = get_class($this); 63 $parts = explode('_', $name); 64 $basename = end($parts); 65 if ($camelized) { 66 return $basename; 67 } else { 68 return self::decamelize($basename); 69 } 70 } 71 72 public static function decamelize($word) 73 { 74 $callback = create_function('$matches', 75 'return strtolower(strlen("$matches[1]") ? "$matches[1]_$matches[2]" : "$matches[2]");'); 76 77 return preg_replace_callback( 78 '/(^|[a-z])([A-Z])/', 79 $callback, 80 $word 81 ); 82 } 83 84 /** 85 * Return camelized version of a word 86 * Examples: sms_messages => SMSMessages, calls => Calls, 87 * incoming_phone_numbers => IncomingPhoneNumbers 88 * 89 * @param string $word The word to camelize 90 * @return string 91 */ 92 public static function camelize($word) { 93 $callback = create_function('$matches', 'return strtoupper("$matches[2]");'); 94 95 return preg_replace_callback('/(^|_)([a-z])/', 96 $callback, 97 $word); 98 } 99 100 /** 101 * Get the value of a property on this resource. 102 * 103 * @param string $key The property name 104 * @return mixed Could be anything. 105 */ 106 public function __get($key) { 107 if ($subresource = $this->getSubresources($key)) { 108 return $subresource; 109 } 110 return $this->$key; 111 } 112 113 /** 114 * Print a JSON representation of this object. Strips the HTTP client 115 * before returning. 116 * 117 * Note, this should mainly be used for debugging, and is not guaranteed 118 * to correspond 1:1 with the JSON API output. 119 * 120 * Note that echoing an object before an HTTP request has been made to 121 * "fill in" its properties may return an empty object 122 */ 123 public function __toString() { 124 $out = array(); 125 foreach ($this as $key => $value) { 126 if ($key !== 'client' && $key !== 'subresources') { 127 $out[$key] = $value; 128 } 129 } 130 return json_encode($out, true); 131 } 132 133 } 134
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |