ApiResource.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Stripe;
  3. /**
  4. * Class ApiResource
  5. *
  6. * @package Stripe
  7. */
  8. abstract class ApiResource extends StripeObject
  9. {
  10. use ApiOperations\Request;
  11. /**
  12. * @return \Stripe\Util\Set A list of fields that can be their own type of
  13. * API resource (say a nested card under an account for example), and if
  14. * that resource is set, it should be transmitted to the API on a create or
  15. * update. Doing so is not the default behavior because API resources
  16. * should normally be persisted on their own RESTful endpoints.
  17. */
  18. public static function getSavedNestedResources()
  19. {
  20. static $savedNestedResources = null;
  21. if ($savedNestedResources === null) {
  22. $savedNestedResources = new Util\Set();
  23. }
  24. return $savedNestedResources;
  25. }
  26. /**
  27. * @var boolean A flag that can be set a behavior that will cause this
  28. * resource to be encoded and sent up along with an update of its parent
  29. * resource. This is usually not desirable because resources are updated
  30. * individually on their own endpoints, but there are certain cases,
  31. * replacing a customer's source for example, where this is allowed.
  32. */
  33. public $saveWithParent = false;
  34. public function __set($k, $v)
  35. {
  36. parent::__set($k, $v);
  37. $v = $this->$k;
  38. if ((static::getSavedNestedResources()->includes($k)) &&
  39. ($v instanceof ApiResource)) {
  40. $v->saveWithParent = true;
  41. }
  42. return $v;
  43. }
  44. /**
  45. * @return ApiResource The refreshed resource.
  46. */
  47. public function refresh()
  48. {
  49. $requestor = new ApiRequestor($this->_opts->apiKey, static::baseUrl());
  50. $url = $this->instanceUrl();
  51. list($response, $this->_opts->apiKey) = $requestor->request(
  52. 'get',
  53. $url,
  54. $this->_retrieveOptions,
  55. $this->_opts->headers
  56. );
  57. $this->setLastResponse($response);
  58. $this->refreshFrom($response->json, $this->_opts);
  59. return $this;
  60. }
  61. /**
  62. * @return string The base URL for the given class.
  63. */
  64. public static function baseUrl()
  65. {
  66. return Stripe::$apiBase;
  67. }
  68. /**
  69. * @return string The endpoint URL for the given class.
  70. */
  71. public static function classUrl()
  72. {
  73. // Replace dots with slashes for namespaced resources, e.g. if the object's name is
  74. // "foo.bar", then its URL will be "/v1/foo/bars".
  75. $base = str_replace('.', '/', static::OBJECT_NAME);
  76. return "/v1/${base}s";
  77. }
  78. /**
  79. * @return string The instance endpoint URL for the given class.
  80. */
  81. public static function resourceUrl($id)
  82. {
  83. if ($id === null) {
  84. $class = get_called_class();
  85. $message = "Could not determine which URL to request: "
  86. . "$class instance has invalid ID: $id";
  87. throw new Error\InvalidRequest($message, null);
  88. }
  89. $id = Util\Util::utf8($id);
  90. $base = static::classUrl();
  91. $extn = urlencode($id);
  92. return "$base/$extn";
  93. }
  94. /**
  95. * @return string The full API URL for this API resource.
  96. */
  97. public function instanceUrl()
  98. {
  99. return static::resourceUrl($this['id']);
  100. }
  101. }