composer package updates
[openemr.git] / vendor / stripe / stripe-php / lib / ApiResource.php
blobf73b22c9663111572d1f7d575fc907d3f36e8077
1 <?php
3 namespace Stripe;
5 /**
6 * Class ApiResource
8 * @package Stripe
9 */
10 abstract class ApiResource extends StripeObject
12 use ApiOperations\Request;
14 /**
15 * @return \Stripe\Util\Set A list of fields that can be their own type of
16 * API resource (say a nested card under an account for example), and if
17 * that resource is set, it should be transmitted to the API on a create or
18 * update. Doing so is not the default behavior because API resources
19 * should normally be persisted on their own RESTful endpoints.
21 public static function getSavedNestedResources()
23 static $savedNestedResources = null;
24 if ($savedNestedResources === null) {
25 $savedNestedResources = new Util\Set();
27 return $savedNestedResources;
30 /**
31 * @var boolean A flag that can be set a behavior that will cause this
32 * resource to be encoded and sent up along with an update of its parent
33 * resource. This is usually not desirable because resources are updated
34 * individually on their own endpoints, but there are certain cases,
35 * replacing a customer's source for example, where this is allowed.
37 public $saveWithParent = false;
39 public function __set($k, $v)
41 parent::__set($k, $v);
42 $v = $this->$k;
43 if ((static::getSavedNestedResources()->includes($k)) &&
44 ($v instanceof ApiResource)) {
45 $v->saveWithParent = true;
47 return $v;
50 /**
51 * @return ApiResource The refreshed resource.
53 public function refresh()
55 $requestor = new ApiRequestor($this->_opts->apiKey, static::baseUrl());
56 $url = $this->instanceUrl();
58 list($response, $this->_opts->apiKey) = $requestor->request(
59 'get',
60 $url,
61 $this->_retrieveOptions,
62 $this->_opts->headers
64 $this->setLastResponse($response);
65 $this->refreshFrom($response->json, $this->_opts);
66 return $this;
69 /**
70 * @return string The name of the class, with namespacing and underscores
71 * stripped.
73 public static function className()
75 $class = get_called_class();
76 // Useful for namespaces: Foo\Charge
77 if ($postfixNamespaces = strrchr($class, '\\')) {
78 $class = substr($postfixNamespaces, 1);
80 // Useful for underscored 'namespaces': Foo_Charge
81 if ($postfixFakeNamespaces = strrchr($class, '')) {
82 $class = $postfixFakeNamespaces;
84 if (substr($class, 0, strlen('Stripe')) == 'Stripe') {
85 $class = substr($class, strlen('Stripe'));
87 $class = str_replace('_', '', $class);
88 $name = urlencode($class);
89 $name = strtolower($name);
90 return $name;
93 /**
94 * @return string The base URL for the given class.
96 public static function baseUrl()
98 return Stripe::$apiBase;
102 * @return string The endpoint URL for the given class.
104 public static function classUrl()
106 $base = static::className();
107 return "/v1/${base}s";
111 * @return string The instance endpoint URL for the given class.
113 public static function resourceUrl($id)
115 if ($id === null) {
116 $class = get_called_class();
117 $message = "Could not determine which URL to request: "
118 . "$class instance has invalid ID: $id";
119 throw new Error\InvalidRequest($message, null);
121 $id = Util\Util::utf8($id);
122 $base = static::classUrl();
123 $extn = urlencode($id);
124 return "$base/$extn";
128 * @return string The full API URL for this API resource.
130 public function instanceUrl()
132 return static::resourceUrl($this['id']);