123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- abstract class Services_Twilio_Resource {
- protected $subresources;
- public function __construct($client, $uri, $params = array())
- {
- $this->subresources = array();
- $this->client = $client;
- foreach ($params as $name => $param) {
- $this->$name = $param;
- }
- $this->uri = $uri;
- $this->init($client, $uri);
- }
- protected function init($client, $uri)
- {
-
- }
- public function getSubresources($name = null) {
- if (isset($name)) {
- return isset($this->subresources[$name])
- ? $this->subresources[$name]
- : null;
- }
- return $this->subresources;
- }
- protected function setupSubresources()
- {
- foreach (func_get_args() as $name) {
- $constantized = ucfirst(self::camelize($name));
- $type = "Services_Twilio_Rest_" . $constantized;
- $this->subresources[$name] = new $type(
- $this->client, $this->uri . "/$constantized"
- );
- }
- }
-
- public function getResourceName($camelized = false)
- {
- $name = get_class($this);
- $parts = explode('_', $name);
- $basename = end($parts);
- if ($camelized) {
- return $basename;
- } else {
- return self::decamelize($basename);
- }
- }
- public static function decamelize($word)
- {
- $callback = create_function('$matches',
- 'return strtolower(strlen("$matches[1]") ? "$matches[1]_$matches[2]" : "$matches[2]");');
- return preg_replace_callback(
- '/(^|[a-z])([A-Z])/',
- $callback,
- $word
- );
- }
-
- public static function camelize($word) {
- $callback = create_function('$matches', 'return strtoupper("$matches[2]");');
- return preg_replace_callback('/(^|_)([a-z])/',
- $callback,
- $word);
- }
-
- public function __get($key) {
- if ($subresource = $this->getSubresources($key)) {
- return $subresource;
- }
- return $this->$key;
- }
-
- public function __toString() {
- $out = array();
- foreach ($this as $key => $value) {
- if ($key !== 'client' && $key !== 'subresources') {
- $out[$key] = $value;
- }
- }
- return json_encode($out, true);
- }
- }
|