1 | <?php namespace Arcanedev\Stripe; |
||
15 | abstract class StripeResource extends StripeObject implements StripeResourceContract |
||
16 | { |
||
17 | /* ----------------------------------------------------------------- |
||
18 | | Properties |
||
19 | | ----------------------------------------------------------------- |
||
20 | */ |
||
21 | |||
22 | /** @var array */ |
||
23 | private static $persistedHeaders = [ |
||
24 | 'Stripe-Account' => true, |
||
25 | 'Stripe-Version' => true, |
||
26 | ]; |
||
27 | |||
28 | /* ----------------------------------------------------------------- |
||
29 | | Main Methods |
||
30 | | ----------------------------------------------------------------- |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * Get the base url. |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | 543 | public static function baseUrl() |
|
39 | 6 | { |
|
40 | 543 | return Stripe::getApiBaseUrl(); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * Get the refreshed resource. |
||
45 | * |
||
46 | * @return self |
||
47 | */ |
||
48 | 267 | public function refresh() |
|
49 | 2 | { |
|
50 | 267 | list($response, $this->opts->apiKey) = Requestor::make($this->opts->apiKey, self::baseUrl()) |
|
51 | 267 | ->get($this->instanceUrl(), $this->retrieveParameters, $this->opts->headers); |
|
52 | |||
53 | /** @var \Arcanedev\Stripe\Http\Response $response */ |
||
54 | 228 | $this->setLastResponse($response); |
|
55 | 228 | $this->refreshFrom($response->getJson(), $this->opts); |
|
56 | |||
57 | 233 | return $this; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get The name of the class, with namespacing and underscores stripped. |
||
62 | * |
||
63 | * @param string $class |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | 516 | public static function className($class = '') |
|
68 | { |
||
69 | 516 | $name = self::getShortNameClass($class); |
|
70 | 516 | $name = str_split_camelcase($name, '_'); |
|
71 | |||
72 | 516 | return strtolower(urlencode($name)); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get Class short name. |
||
77 | * |
||
78 | * @param string $class |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | 516 | protected static function getShortNameClass($class = '') |
|
83 | { |
||
84 | 516 | if (empty($class)) |
|
85 | 508 | $class = get_called_class(); |
|
86 | |||
87 | 516 | return (new ReflectionClass($class))->getShortName(); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get the endpoint URL for the given class. |
||
92 | * |
||
93 | * @param string $class |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | 507 | public static function classUrl($class = '') |
|
98 | { |
||
99 | 507 | $base = self::className($class); |
|
100 | |||
101 | 507 | return "/v1/${base}s"; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get Instance URL. |
||
106 | * |
||
107 | * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function instanceUrl() |
||
112 | { |
||
113 | 345 | return static::resourceUrl($this['id']); |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Get the instance endpoint URL for the given class. |
||
118 | * |
||
119 | * @param string $id |
||
120 | * |
||
121 | * @return string |
||
122 | * |
||
123 | * @throws Exceptions\InvalidRequestException |
||
124 | */ |
||
125 | public static function resourceUrl($id) |
||
126 | { |
||
127 | 363 | if ($id === null) { |
|
128 | 9 | $class = get_called_class(); |
|
129 | 9 | throw new Exceptions\InvalidRequestException( |
|
130 | 9 | "Could not determine which URL to request: $class instance has invalid ID: $id", null |
|
131 | 3 | ); |
|
132 | } |
||
133 | |||
134 | 354 | return static::classUrl().'/'.urlencode(str_utf8($id)); |
|
135 | } |
||
136 | |||
137 | /* ------------------------------------------------------------------------------------------------ |
||
138 | | Request Functions |
||
139 | | ------------------------------------------------------------------------------------------------ |
||
140 | */ |
||
141 | /** |
||
142 | * Make a request. |
||
143 | * |
||
144 | * @param string $method |
||
145 | * @param string $url |
||
146 | * @param array|null $params |
||
147 | * @param array|string|null $options |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | protected function request($method, $url, $params = [], $options = null) |
||
152 | { |
||
153 | 222 | $opts = $this->opts->merge($options); |
|
154 | |||
155 | /** @var \Arcanedev\Stripe\Http\Response $response */ |
||
156 | 222 | list($response, $options) = static::staticRequest($method, $url, $params, $opts); |
|
157 | 222 | $this->setLastResponse($response); |
|
158 | |||
159 | 222 | return [$response->getJson(), $options]; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * Make a request (static). |
||
164 | * |
||
165 | * @param string $method |
||
166 | * @param string $url |
||
167 | * @param array|null $params |
||
168 | * @param array|string|null $options |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | protected static function staticRequest($method, $url, $params, $options) |
||
173 | { |
||
174 | 513 | $opts = RequestOptions::parse($options); |
|
175 | 513 | $requestor = Requestor::make($opts->apiKey, static::baseUrl()); |
|
176 | |||
177 | 501 | list($response, $opts->apiKey) = |
|
178 | 513 | $requestor->request($method, $url, $params, $opts->headers); |
|
179 | |||
180 | 501 | foreach ($opts->headers as $k => $v) { |
|
181 | 39 | if ( ! array_key_exists($k, self::$persistedHeaders)) |
|
182 | 27 | unset($opts->headers[$k]); |
|
183 | 167 | } |
|
184 | |||
185 | 501 | return [$response, $opts]; |
|
186 | } |
||
187 | |||
188 | /* ----------------------------------------------------------------- |
||
189 | | CRUD Scope Methods |
||
190 | | ----------------------------------------------------------------- |
||
191 | */ |
||
192 | |||
193 | /** |
||
194 | * Retrieve scope. |
||
195 | * |
||
196 | * @param string $id |
||
197 | * @param array|string|null $options |
||
198 | * |
||
199 | * @return self |
||
200 | */ |
||
201 | protected static function scopedRetrieve($id, $options = null) |
||
211 | |||
212 | /** |
||
213 | * List scope. |
||
214 | * |
||
215 | * @param array|null $params |
||
216 | * @param array|string|null $options |
||
217 | * |
||
218 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
219 | * |
||
220 | * @return \Arcanedev\Stripe\Collection|array |
||
221 | */ |
||
222 | protected static function scopedAll($params = [], $options = null) |
||
240 | |||
241 | /** |
||
242 | * Create scope. |
||
243 | * |
||
244 | * @param array|null $params |
||
245 | * @param array|string|null $options |
||
246 | * |
||
247 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
248 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
249 | * |
||
250 | * @return self |
||
251 | */ |
||
252 | protected static function scopedCreate($params = [], $options = null) |
||
264 | |||
265 | /** |
||
266 | * Update scope. |
||
267 | * |
||
268 | * @param string $id |
||
269 | * @param array|null $params |
||
270 | * @param array|string|null $options |
||
271 | * |
||
272 | * @return self |
||
273 | * |
||
274 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
275 | */ |
||
276 | protected static function scopedUpdate($id, $params = null, $options = null) |
||
288 | |||
289 | /** |
||
290 | * Save scope. |
||
291 | * |
||
292 | * @param array|string|null $options |
||
293 | * |
||
294 | * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException |
||
295 | * |
||
296 | * @return self |
||
297 | */ |
||
298 | protected function scopedSave($options = null) |
||
308 | |||
309 | /** |
||
310 | * Delete Scope. |
||
311 | * |
||
312 | * @param array|null $params |
||
313 | * @param array|string|null $options |
||
314 | * |
||
315 | * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException |
||
316 | * |
||
317 | * @return self |
||
318 | */ |
||
319 | protected function scopedDelete($params = [], $options = null) |
||
328 | |||
329 | /* ----------------------------------------------------------------- |
||
330 | | Custom Scope Methods |
||
331 | | ----------------------------------------------------------------- |
||
332 | */ |
||
333 | |||
334 | /** |
||
335 | * Custom Post Call. |
||
336 | * |
||
337 | * @param string $url |
||
338 | * @param array|null $params |
||
339 | * @param array|string|null $options |
||
340 | * |
||
341 | * @return self |
||
342 | */ |
||
343 | protected function scopedPostCall($url, $params = [], $options = null) |
||
354 | |||
355 | /* ----------------------------------------------------------------- |
||
356 | | Check Methods |
||
357 | | ----------------------------------------------------------------- |
||
358 | */ |
||
359 | |||
360 | /** |
||
361 | * Check Arguments. |
||
362 | * |
||
363 | * @param array|null $params |
||
364 | * @param array|string|null $options |
||
365 | */ |
||
366 | protected static function checkArguments($params = [], $options = null) |
||
371 | |||
372 | /** |
||
373 | * Check parameters. |
||
374 | * |
||
375 | * @param array|null $params |
||
376 | * |
||
377 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
378 | */ |
||
379 | private static function checkParameters($params) |
||
389 | |||
390 | /** |
||
391 | * Check Options. |
||
392 | * |
||
393 | * @param array|string|null $options |
||
394 | * |
||
395 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
396 | */ |
||
397 | private static function checkOptions($options) |
||
413 | |||
414 | /** |
||
415 | * Check the object is a Collection class. |
||
416 | * |
||
417 | * @param mixed $object |
||
418 | * |
||
419 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
420 | */ |
||
421 | private static function checkIsCollectionObject($object) |
||
429 | } |
||
430 |