ApiResource.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Pingpp;
  3. abstract class ApiResource extends PingppObject
  4. {
  5. private static $HEADERS_TO_PERSIST = array('Pingpp-Version' => true);
  6. public static function baseUrl()
  7. {
  8. return Pingpp::$apiBase;
  9. }
  10. /**
  11. * @return ApiResource The refreshed resource.
  12. */
  13. public function refresh()
  14. {
  15. $requestor = new ApiRequestor($this->_opts->apiKey, static::baseUrl());
  16. $url = $this->instanceUrl();
  17. list($response, $this->_opts->apiKey) = $requestor->request(
  18. 'get',
  19. $url,
  20. $this->_retrieveOptions,
  21. $this->_opts->headers
  22. );
  23. $this->refreshFrom($response, $this->_opts);
  24. return $this;
  25. }
  26. /**
  27. * @return string The name of the class, with namespacing and underscores
  28. * stripped.
  29. */
  30. public static function className()
  31. {
  32. $class = get_called_class();
  33. // Useful for namespaces: Foo\Charge
  34. if ($postfix = strrchr($class, '\\')) {
  35. $class = substr($postfix, 1);
  36. }
  37. // Useful for underscored 'namespaces': Foo_Charge
  38. if ($postfixFakeNamespaces = strrchr($class, '')) {
  39. $class = $postfixFakeNamespaces;
  40. }
  41. if (substr($class, 0, strlen('Pingpp')) == 'Pingpp') {
  42. $class = substr($class, strlen('Pingpp'));
  43. }
  44. $class = str_replace('_', '', $class);
  45. $name = urlencode($class);
  46. $name = strtolower($name);
  47. return $name;
  48. }
  49. /**
  50. * @return string The endpoint URL for the given class.
  51. */
  52. public static function classUrl()
  53. {
  54. $base = static::className();
  55. return "/v1/${base}s";
  56. }
  57. /**
  58. * @return string The full API URL for this API resource.
  59. */
  60. public function instanceUrl()
  61. {
  62. $id = $this['id'];
  63. $class = get_called_class();
  64. if ($id === null) {
  65. $message = "Could not determine which URL to request: "
  66. . "$class instance has invalid ID: $id";
  67. throw new Error\InvalidRequest($message, null);
  68. }
  69. $id = Util\Util::utf8($id);
  70. $base = static::classUrl();
  71. $extn = urlencode($id);
  72. return "$base/$extn";
  73. }
  74. private static function _validateParams($params = null)
  75. {
  76. if ($params && !is_array($params)) {
  77. $message = "You must pass an array as the first argument to Pingpp API "
  78. . "method calls.";
  79. throw new Error\Api($message);
  80. }
  81. }
  82. protected function _request($method, $url, $params = array(), $options = null)
  83. {
  84. $opts = $this->_opts->merge($options);
  85. return static::_staticRequest($method, $url, $params, $opts);
  86. }
  87. protected static function _staticRequest($method, $url, $params, $options)
  88. {
  89. $opts = Util\RequestOptions::parse($options);
  90. $requestor = new ApiRequestor($opts->apiKey, static::baseUrl());
  91. list($response, $opts->apiKey) = $requestor->request($method, $url, $params, $opts->headers);
  92. foreach ($opts->headers as $k => $v) {
  93. if (!array_key_exists($k, self::$HEADERS_TO_PERSIST)) {
  94. unset($opts->headers[$k]);
  95. }
  96. }
  97. return array($response, $opts);
  98. }
  99. protected static function _retrieve($id, $options = null)
  100. {
  101. $opts = Util\RequestOptions::parse($options);
  102. $instance = new static($id, $opts);
  103. $instance->refresh();
  104. return $instance;
  105. }
  106. protected static function _all($params = null, $options = null)
  107. {
  108. self::_validateParams($params);
  109. $url = static::classUrl();
  110. list($response, $opts) = static::_staticRequest('get', $url, $params, $options);
  111. return Util\Util::convertToPingppObject($response, $opts);
  112. }
  113. protected static function _create($params = null, $options = null)
  114. {
  115. self::_validateParams($params);
  116. $url = static::classUrl();
  117. list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
  118. return Util\Util::convertToPingppObject($response, $opts);
  119. }
  120. protected function _save($options = null)
  121. {
  122. $params = $this->serializeParameters();
  123. if (count($params) > 0) {
  124. $url = $this->instanceUrl();
  125. list($response, $opts) = $this->_request('put', $url, $params, $options);
  126. $this->refreshFrom($response, $opts);
  127. }
  128. return $this;
  129. }
  130. protected function _delete($params = null, $options = null)
  131. {
  132. self::_validateParams($params);
  133. $url = $this->instanceUrl();
  134. list($response, $opts) = $this->_request('delete', $url, $params, $options);
  135. $this->refreshFrom($response, $opts);
  136. return $this;
  137. }
  138. }