Resource.php 3.6 KB

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