InstanceResource.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @category Services
  4. * @package Services_Twilio
  5. * @author Neuman Vong <neuman@twilio.com>
  6. * @license http://creativecommons.org/licenses/MIT/ MIT
  7. * @link http://pear.php.net/package/Services_Twilio
  8. */
  9. /**
  10. * Abstraction of an instance resource from the Twilio API.
  11. */
  12. abstract class Services_Twilio_InstanceResource extends Services_Twilio_Resource {
  13. /**
  14. * Make a request to the API to update an instance resource
  15. *
  16. * :param mixed $params: An array of updates, or a property name
  17. * :param mixed $value: A value with which to update the resource
  18. *
  19. * :rtype: null
  20. * :throws: a :php:class:`RestException <Services_Twilio_RestException>` if
  21. * the update fails.
  22. */
  23. public function update($params, $value = null)
  24. {
  25. if (!is_array($params)) {
  26. $params = array($params => $value);
  27. }
  28. $decamelizedParams = $this->client->createData($this->uri, $params);
  29. $this->updateAttributes($decamelizedParams);
  30. }
  31. /*
  32. * Add all properties from an associative array (the JSON response body) as
  33. * properties on this instance resource, except the URI
  34. *
  35. * :param stdClass $params: An object containing all of the parameters of
  36. * this instance
  37. * :return: Nothing, this is purely side effecting
  38. * :rtype: null
  39. */
  40. public function updateAttributes($params) {
  41. unset($params->uri);
  42. foreach ($params as $name => $value) {
  43. $this->$name = $value;
  44. }
  45. }
  46. /**
  47. * Get the value of a property on this resource.
  48. *
  49. * Instead of defining all of the properties of an object directly, we rely
  50. * on the API to tell us which properties an object has. This method will
  51. * query the API to retrieve a property for an object, if it is not already
  52. * set on the object.
  53. *
  54. * If the call is to a subresource, eg ``$client->account->messages``, no
  55. * request is made.
  56. *
  57. * To help with lazy HTTP requests, we don't actually retrieve an object
  58. * from the API unless you really need it. Hence, this function may make API
  59. * requests even if the property you're requesting isn't available on the
  60. * resource.
  61. *
  62. * :param string $key: The property name
  63. *
  64. * :return mixed: Could be anything.
  65. * :throws: a :php:class:`RestException <Services_Twilio_RestException>` if
  66. * the update fails.
  67. */
  68. public function __get($key)
  69. {
  70. if ($subresource = $this->getSubresources($key)) {
  71. return $subresource;
  72. }
  73. if (!isset($this->$key)) {
  74. $params = $this->client->retrieveData($this->uri);
  75. $this->updateAttributes($params);
  76. }
  77. return $this->$key;
  78. }
  79. }