PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/Eat Smpl/form(NotInUse)/sites/step3/lib/lib/ApiResource.php

https://gitlab.com/eatsmpl/site
PHP | 161 lines | 147 code | 6 blank | 8 comment | 1 complexity | 528def081b0db8628f8a82d41ec614c0 MD5 | raw file
  1. <?php
  2. namespace Stripe;
  3. abstract class ApiResource extends StripeObject
  4. {
  5. private static $HEADERS_TO_PERSIST = array('Stripe-Account' => true, 'Stripe-Version' => true);
  6. public static function baseUrl()
  7. {
  8. return Stripe::$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 ($postfixNamespaces = strrchr($class, '\\')) {
  35. $class = substr($postfixNamespaces, 1);
  36. }
  37. // Useful for underscored 'namespaces': Foo_Charge
  38. if ($postfixFakeNamespaces = strrchr($class, '')) {
  39. $class = $postfixFakeNamespaces;
  40. }
  41. if (substr($class, 0, strlen('Stripe')) == 'Stripe') {
  42. $class = substr($class, strlen('Stripe'));
  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. if ($id === null) {
  64. $class = get_called_class();
  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 Stripe API "
  78. . "method calls. (HINT: an example call to create a charge "
  79. . "would be: \"Stripe\\Charge::create(array('amount' => 100, "
  80. . "'currency' => 'usd', 'card' => array('number' => "
  81. . "4242424242424242, 'exp_month' => 5, 'exp_year' => 2015)))\")";
  82. throw new Error\Api($message);
  83. }
  84. }
  85. protected function _request($method, $url, $params = array(), $options = null)
  86. {
  87. $opts = $this->_opts->merge($options);
  88. return static::_staticRequest($method, $url, $params, $opts);
  89. }
  90. protected static function _staticRequest($method, $url, $params, $options)
  91. {
  92. $opts = Util\RequestOptions::parse($options);
  93. $requestor = new ApiRequestor($opts->apiKey, static::baseUrl());
  94. list($response, $opts->apiKey) = $requestor->request($method, $url, $params, $opts->headers);
  95. foreach ($opts->headers as $k => $v) {
  96. if (!array_key_exists($k, self::$HEADERS_TO_PERSIST)) {
  97. unset($opts->headers[$k]);
  98. }
  99. }
  100. return array($response, $opts);
  101. }
  102. protected static function _retrieve($id, $options = null)
  103. {
  104. $opts = Util\RequestOptions::parse($options);
  105. $instance = new static($id, $opts);
  106. $instance->refresh();
  107. return $instance;
  108. }
  109. protected static function _all($params = null, $options = null)
  110. {
  111. self::_validateParams($params);
  112. $url = static::classUrl();
  113. list($response, $opts) = static::_staticRequest('get', $url, $params, $options);
  114. return Util\Util::convertToStripeObject($response, $opts);
  115. }
  116. protected static function _create($params = null, $options = null)
  117. {
  118. self::_validateParams($params);
  119. $base = static::baseUrl();
  120. $url = static::classUrl();
  121. list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
  122. return Util\Util::convertToStripeObject($response, $opts);
  123. }
  124. protected function _save($options = null)
  125. {
  126. $params = $this->serializeParameters();
  127. if (count($params) > 0) {
  128. $url = $this->instanceUrl();
  129. list($response, $opts) = $this->_request('post', $url, $params, $options);
  130. $this->refreshFrom($response, $opts);
  131. }
  132. return $this;
  133. }
  134. protected function _delete($params = null, $options = null)
  135. {
  136. self::_validateParams($params);
  137. $url = $this->instanceUrl();
  138. list($response, $opts) = $this->_request('delete', $url, $params, $options);
  139. $this->refreshFrom($response, $opts);
  140. return $this;
  141. }
  142. }