1
|
|
|
<?php namespace Arcanedev\Stripe; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Stripe\Contracts\StripeObject as StripObjectContract; |
4
|
|
|
use Arcanedev\Stripe\Contracts\Utilities\Arrayable; |
5
|
|
|
use Arcanedev\Stripe\Contracts\Utilities\Jsonable; |
6
|
|
|
use Arcanedev\Stripe\Exceptions\ApiException; |
7
|
|
|
use Arcanedev\Stripe\Exceptions\InvalidArgumentException; |
8
|
|
|
use Arcanedev\Stripe\Http\RequestOptions; |
9
|
|
|
use Arcanedev\Stripe\Http\Response; |
10
|
|
|
use Arcanedev\Stripe\Utilities\Util; |
11
|
|
|
use Arcanedev\Stripe\Utilities\UtilSet; |
12
|
|
|
use ArrayAccess; |
13
|
|
|
use JsonSerializable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class StripeObject |
17
|
|
|
* |
18
|
|
|
* @package Arcanedev\Stripe |
19
|
|
|
* @author ARCANEDEV <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @property string id |
22
|
|
|
* @property string object |
23
|
|
|
*/ |
24
|
|
|
class StripeObject implements StripObjectContract, ArrayAccess, JsonSerializable, Arrayable, Jsonable |
25
|
|
|
{ |
26
|
|
|
/* ----------------------------------------------------------------- |
27
|
|
|
| Properties |
28
|
|
|
| ----------------------------------------------------------------- |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Arcanedev\Stripe\Http\RequestOptions|string|array |
33
|
|
|
*/ |
34
|
|
|
protected $opts; |
35
|
|
|
|
36
|
|
|
/** @var array */ |
37
|
|
|
protected $values; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Unsaved Values. |
41
|
|
|
* |
42
|
|
|
* @var \Arcanedev\Stripe\Utilities\UtilSet |
43
|
|
|
*/ |
44
|
|
|
protected $unsavedValues; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Transient (Deleted) Values. |
48
|
|
|
* |
49
|
|
|
* @var \Arcanedev\Stripe\Utilities\UtilSet |
50
|
|
|
*/ |
51
|
|
|
protected $transientValues; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieve parameters used to query the object. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $retrieveParameters; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Attributes that should not be sent to the API because |
62
|
|
|
* they're not updatable (e.g. API key, ID). |
63
|
|
|
* |
64
|
|
|
* @var \Arcanedev\Stripe\Utilities\UtilSet |
65
|
|
|
*/ |
66
|
|
|
public static $permanentAttributes; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Attributes that are nested but still updatable from the |
70
|
|
|
* parent class's URL (e.g. metadata). |
71
|
|
|
* |
72
|
|
|
* @var \Arcanedev\Stripe\Utilities\UtilSet |
73
|
|
|
*/ |
74
|
|
|
public static $nestedUpdatableAttributes; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Allow to check attributes while setting. |
78
|
|
|
* |
79
|
|
|
* @var bool |
80
|
|
|
*/ |
81
|
|
|
protected $checkUnsavedAttributes = false; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The last response. |
85
|
|
|
* |
86
|
|
|
* @var \Arcanedev\Stripe\Http\Response |
87
|
|
|
*/ |
88
|
|
|
protected $lastResponse; |
89
|
|
|
|
90
|
|
|
/* ----------------------------------------------------------------- |
91
|
|
|
| Constructor |
92
|
|
|
| ----------------------------------------------------------------- |
93
|
|
|
*/ |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Make a Stripe object instance. |
97
|
|
|
* |
98
|
|
|
* @param string|null $id |
99
|
|
|
* @param string|array|null $options |
100
|
|
|
*/ |
101
|
498 |
|
public function __construct($id = null, $options = null) |
102
|
|
|
{ |
103
|
498 |
|
$this->init(); |
104
|
498 |
|
$this->opts = $options ? $options : new RequestOptions; |
105
|
498 |
|
$this->setId($id); |
106
|
496 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Init the stripe object. |
110
|
|
|
*/ |
111
|
498 |
|
private function init() |
112
|
|
|
{ |
113
|
498 |
|
$this->values = []; |
114
|
498 |
|
self::$permanentAttributes = new UtilSet(['opts', 'id']); |
115
|
498 |
|
self::$nestedUpdatableAttributes = new UtilSet([ |
116
|
498 |
|
// Numbers are in place for indexes in an `additional_owners` array. |
117
|
|
|
// There's a maximum allowed additional owners of 3, but leave the 4th so errors work properly. |
118
|
|
|
0, 1, 2, 3, 4, |
119
|
|
|
'additional_owners', |
120
|
|
|
'address', |
121
|
|
|
'address_kana', |
122
|
|
|
'address_kanji', |
123
|
|
|
'dob', |
124
|
498 |
|
'inventory', |
125
|
498 |
|
'legal_entity', |
126
|
498 |
|
'metadata', |
127
|
498 |
|
'owner', |
128
|
|
|
'payout_schedule', |
129
|
|
|
'personal_address', |
130
|
|
|
'personal_address_kana', |
131
|
|
|
'personal_address_kanji', |
132
|
|
|
'shipping', |
133
|
|
|
'tos_acceptance', |
134
|
|
|
'transfer_schedule', |
135
|
|
|
'verification', |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
|
$this->unsavedValues = new UtilSet; |
139
|
|
|
$this->transientValues = new UtilSet; |
140
|
|
|
$this->retrieveParameters = []; |
141
|
|
|
} |
142
|
|
|
|
143
|
498 |
|
/* ----------------------------------------------------------------- |
144
|
|
|
| Getters & Setters (+Magics) |
145
|
498 |
|
| ----------------------------------------------------------------- |
146
|
|
|
*/ |
147
|
496 |
|
|
148
|
|
|
/** |
149
|
496 |
|
* Set the Id. |
150
|
|
|
* |
151
|
|
|
* @param array|string|null $id |
152
|
|
|
* |
153
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
154
|
|
|
* |
155
|
|
|
* @return self |
156
|
|
|
*/ |
157
|
|
|
private function setId($id) |
158
|
|
|
{ |
159
|
498 |
|
$this->setIdIfArray($id); |
160
|
|
|
|
161
|
498 |
|
if ( ! is_null($id)) $this->id = $id; |
162
|
|
|
|
163
|
8 |
|
return $this; |
164
|
6 |
|
} |
165
|
|
|
|
166
|
6 |
|
/** |
167
|
6 |
|
* Set the Id from Array. |
168
|
|
|
* |
169
|
|
|
* @param array|string|null $id |
170
|
|
|
* |
171
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
172
|
|
|
*/ |
173
|
|
|
private function setIdIfArray(&$id) |
174
|
2 |
|
{ |
175
|
|
|
if ( ! is_array($id)) return; |
176
|
2 |
|
|
177
|
|
|
$this->checkIdIsInArray($id); |
178
|
|
|
$this->retrieveParameters = array_diff_key($id, array_flip(['id'])); |
179
|
|
|
|
180
|
|
|
$id = $id['id']; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get Retrieve Parameters. |
185
|
|
|
* |
186
|
384 |
|
* @return array |
187
|
|
|
*/ |
188
|
384 |
|
protected function getRetrieveParams() |
189
|
|
|
{ |
190
|
384 |
|
return $this->retrieveParameters; |
191
|
380 |
|
} |
192
|
|
|
|
193
|
6 |
|
/** |
194
|
|
|
* Standard get accessor. |
195
|
6 |
|
* |
196
|
|
|
* @param string|int $key |
197
|
|
|
* |
198
|
|
|
* @return mixed|null |
199
|
|
|
*/ |
200
|
|
|
public function &__get($key) |
201
|
|
|
{ |
202
|
|
|
$nullVal = null; |
203
|
|
|
|
204
|
430 |
|
if (in_array($key, $this->keys())) |
205
|
|
|
return $this->values[$key]; |
206
|
430 |
|
|
207
|
|
|
$this->showUndefinedPropertyMsg(get_class($this), $key); |
208
|
430 |
|
|
209
|
430 |
|
return $nullVal; |
210
|
430 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Standard set accessor. |
214
|
|
|
* |
215
|
|
|
* @param string $key |
216
|
|
|
* @param mixed $value |
217
|
|
|
*/ |
218
|
|
|
public function __set($key, $value) |
219
|
|
|
{ |
220
|
430 |
|
$supportedAttributes = $this->keys(); |
221
|
|
|
|
222
|
430 |
|
$this->setValue($key, $value); |
223
|
430 |
|
$this->checkUnsavedAttributes($supportedAttributes); |
224
|
|
|
} |
225
|
|
|
|
226
|
430 |
|
/** |
227
|
430 |
|
* Set value. |
228
|
430 |
|
* |
229
|
430 |
|
* @param string $key |
230
|
|
|
* @param mixed $value |
231
|
10 |
|
* |
232
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
233
|
|
|
*/ |
234
|
|
|
private function setValue($key, $value) |
235
|
430 |
|
{ |
236
|
|
|
$this->checkIfAttributeDeletion($key, $value); |
237
|
|
|
$this->checkMetadataAttribute($key, $value); |
238
|
430 |
|
|
239
|
430 |
|
if ( |
240
|
|
|
self::$nestedUpdatableAttributes->includes($key) && |
241
|
|
|
isset($this->$key) && |
242
|
|
|
$this->$key instanceof AttachedObject && |
243
|
|
|
is_array($value) |
244
|
|
|
) { |
245
|
|
|
$this->$key->replaceWith($value); |
246
|
6 |
|
} |
247
|
|
|
else { |
248
|
6 |
|
// TODO: may want to clear from $transientValues (Won't be user-visible). |
249
|
|
|
$this->values[$key] = $value; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$this->checkPermanentAttributes($key); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the last response from the Stripe API. |
257
|
|
|
* |
258
|
378 |
|
* @return \Arcanedev\Stripe\Http\Response |
259
|
|
|
*/ |
260
|
378 |
|
public function getLastResponse() |
261
|
|
|
{ |
262
|
378 |
|
return $this->lastResponse; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Set the last response from the Stripe API. |
267
|
|
|
* |
268
|
|
|
* @param \Arcanedev\Stripe\Http\Response $response |
269
|
|
|
* |
270
|
|
|
* @return static |
271
|
|
|
*/ |
272
|
104 |
|
public function setLastResponse(Response $response) |
273
|
|
|
{ |
274
|
104 |
|
$this->lastResponse = $response; |
275
|
|
|
|
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Check has a value by key. |
281
|
|
|
* |
282
|
38 |
|
* @param string $key |
283
|
|
|
* |
284
|
38 |
|
* @return bool |
285
|
|
|
*/ |
286
|
38 |
|
public function __isset($key) |
287
|
38 |
|
{ |
288
|
38 |
|
return isset($this->values[$key]); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Unset element from values. |
293
|
|
|
* |
294
|
|
|
* @param string $key |
295
|
2 |
|
*/ |
296
|
|
|
public function __unset($key) |
297
|
2 |
|
{ |
298
|
|
|
unset($this->values[$key]); |
299
|
|
|
|
300
|
|
|
$this->transientValues->add($key); |
301
|
|
|
$this->unsavedValues->discard($key); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
2 |
|
* Convert StripeObject to string. |
306
|
|
|
* |
307
|
2 |
|
* @return string |
308
|
|
|
*/ |
309
|
|
|
public function __toString() |
310
|
|
|
{ |
311
|
|
|
return get_class($this).' JSON: '.$this->toJson(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Json serialize. |
316
|
|
|
* |
317
|
10 |
|
* @return array |
318
|
|
|
*/ |
319
|
10 |
|
public function jsonSerialize() |
320
|
8 |
|
{ |
321
|
10 |
|
return $this->toArray(true); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Convert StripeObject to array. |
326
|
|
|
* |
327
|
|
|
* @param bool $recursive |
328
|
|
|
* |
329
|
|
|
* @return array |
330
|
|
|
*/ |
331
|
2 |
|
public function toArray($recursive = false) |
332
|
|
|
{ |
333
|
2 |
|
return $recursive |
334
|
2 |
|
? Util::convertStripeObjectToArray($this->values) |
335
|
|
|
: $this->values; |
336
|
2 |
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Convert StripeObject to JSON. |
340
|
|
|
* |
341
|
|
|
* @param int $options |
342
|
|
|
* |
343
|
|
|
* @return string |
344
|
452 |
|
*/ |
345
|
|
|
public function toJson($options = 0) |
346
|
452 |
|
{ |
347
|
|
|
if ($options === 0 && defined('JSON_PRETTY_PRINT')) |
348
|
|
|
$options = JSON_PRETTY_PRINT; |
349
|
|
|
|
350
|
|
|
return json_encode($this->toArray(true), $options); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
34 |
|
* Get only value keys. |
355
|
|
|
* |
356
|
34 |
|
* @return array |
357
|
34 |
|
*/ |
358
|
|
|
public function keys() |
359
|
372 |
|
{ |
360
|
|
|
return empty($this->values) ? [] : array_keys($this->values); |
361
|
372 |
|
} |
362
|
|
|
|
363
|
|
|
/* ----------------------------------------------------------------- |
364
|
2 |
|
| ArrayAccess methods |
365
|
|
|
| ----------------------------------------------------------------- |
366
|
2 |
|
*/ |
367
|
2 |
|
|
368
|
|
|
public function offsetSet($key, $value) |
369
|
266 |
|
{ |
370
|
|
|
$this->$key = $value; |
371
|
266 |
|
} |
372
|
254 |
|
|
373
|
266 |
|
public function offsetExists($key) |
374
|
|
|
{ |
375
|
|
|
return array_key_exists($key, $this->values); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
public function offsetUnset($key) |
379
|
|
|
{ |
380
|
|
|
unset($this->$key); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function offsetGet($key) |
384
|
|
|
{ |
385
|
|
|
return array_key_exists($key, $this->values) |
386
|
|
|
? $this->values[$key] |
387
|
|
|
: null; |
388
|
|
|
} |
389
|
|
|
|
390
|
382 |
|
/* ----------------------------------------------------------------- |
391
|
|
|
| Main Methods |
392
|
382 |
|
| ----------------------------------------------------------------- |
393
|
382 |
|
*/ |
394
|
|
|
|
395
|
382 |
|
/** |
396
|
|
|
* This unfortunately needs to be public to be used in Util.php |
397
|
|
|
* Return The object constructed from the given values. |
398
|
|
|
* |
399
|
|
|
* @param array $values |
400
|
|
|
* @param \Arcanedev\Stripe\Http\RequestOptions|array|string|null $options |
401
|
|
|
* |
402
|
|
|
* @return static |
403
|
|
|
*/ |
404
|
|
|
public static function scopedConstructFrom($values, $options) |
405
|
390 |
|
{ |
406
|
|
|
$obj = new static(isset($values['id']) ? $values['id'] : null); |
407
|
390 |
|
$obj->refreshFrom($values, $options); |
408
|
|
|
|
409
|
390 |
|
return $obj; |
410
|
|
|
} |
411
|
390 |
|
|
412
|
390 |
|
/** |
413
|
368 |
|
* Refreshes this object using the provided values. |
414
|
|
|
* |
415
|
390 |
|
* @param array $values |
416
|
|
|
* @param \Arcanedev\Stripe\Http\RequestOptions|array|string|null $options |
417
|
390 |
|
* @param bool $partial |
418
|
390 |
|
*/ |
419
|
|
|
public function refreshFrom($values, $options, $partial = false) |
420
|
390 |
|
{ |
421
|
|
|
$this->opts = is_array($options) ? RequestOptions::parse($options) : $options; |
422
|
|
|
|
423
|
|
|
$this->cleanObject($values, $partial); |
424
|
|
|
|
425
|
|
|
foreach ($values as $key => $value) { |
426
|
|
|
if (self::$permanentAttributes->includes($key) && isset($this[$key])) |
427
|
|
|
continue; |
428
|
390 |
|
|
429
|
|
|
$this->values[$key] = $this->constructValue($key, $value, $this->opts); |
430
|
|
|
|
431
|
|
|
$this->transientValues->discard($key); |
432
|
|
|
$this->unsavedValues->discard($key); |
433
|
390 |
|
} |
434
|
390 |
|
} |
435
|
390 |
|
|
436
|
|
|
/** |
437
|
390 |
|
* Clean refreshed StripeObject. |
438
|
30 |
|
* |
439
|
2 |
|
* @param array $values |
440
|
|
|
* @param bool|false $partial |
441
|
30 |
|
*/ |
442
|
|
|
private function cleanObject($values, $partial) |
443
|
390 |
|
{ |
444
|
|
|
// Wipe old state before setting new. |
445
|
|
|
// This is useful for e.g. updating a customer, where there is no persistent card parameter. |
446
|
|
|
// Mark those values which don't persist as transient |
447
|
|
|
$removed = ! $partial |
448
|
|
|
? array_diff($this->keys(), array_keys($values)) |
449
|
|
|
: new UtilSet; |
450
|
|
|
|
451
|
|
|
foreach ($removed as $key) { |
452
|
|
|
if (self::$permanentAttributes->includes($key)) |
453
|
|
|
continue; |
454
|
390 |
|
|
455
|
|
|
unset($this->$key); |
456
|
390 |
|
} |
457
|
284 |
|
} |
458
|
390 |
|
|
459
|
|
|
/** |
460
|
|
|
* Construct Value. |
461
|
|
|
* |
462
|
|
|
* @param string $key |
463
|
|
|
* @param mixed $value |
464
|
|
|
* @param \Arcanedev\Stripe\Http\RequestOptions|array|string|null $options |
465
|
|
|
* |
466
|
|
|
* @return static|\Arcanedev\Stripe\StripeResource|\Arcanedev\Stripe\Collection|array |
467
|
|
|
*/ |
468
|
|
|
private function constructValue($key, $value, $options) |
469
|
|
|
{ |
470
|
|
|
return (self::$nestedUpdatableAttributes->includes($key) && is_array($value)) |
471
|
|
|
? AttachedObject::scopedConstructFrom($value, $options) |
472
|
|
|
: Util::convertToStripeObject($value, $options); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* Pretend to have late static bindings. |
477
|
|
|
* |
478
|
|
|
* @param string $method |
479
|
|
|
* |
480
|
|
|
* @return mixed |
481
|
|
|
*/ |
482
|
|
|
protected function lsb($method) |
483
|
|
|
{ |
484
|
|
|
return call_user_func_array( |
485
|
|
|
[get_class($this), $method], |
486
|
|
|
array_slice(func_get_args(), 1) |
487
|
|
|
); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* Scoped Late Static Bindings. |
492
|
|
|
* |
493
|
|
|
* @param string $class |
494
|
|
|
* @param string $method |
495
|
|
|
* |
496
|
|
|
* @return mixed |
497
|
|
|
*/ |
498
|
|
|
protected static function scopedLsb($class, $method) |
499
|
|
|
{ |
500
|
|
|
return call_user_func_array( |
501
|
|
|
[$class, $method], |
502
|
|
|
array_slice(func_get_args(), 2) |
503
|
|
|
); |
504
|
8 |
|
} |
505
|
|
|
|
506
|
8 |
|
/* ----------------------------------------------------------------- |
507
|
2 |
|
| Check Methods |
508
|
6 |
|
| ----------------------------------------------------------------- |
509
|
|
|
*/ |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Check if array has id. |
513
|
|
|
* |
514
|
|
|
* @param array $array |
515
|
2 |
|
* |
516
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
517
|
2 |
|
*/ |
518
|
|
|
private function checkIdIsInArray($array) |
519
|
|
|
{ |
520
|
|
|
if ( ! array_key_exists('id', $array)) |
521
|
|
|
throw new ApiException('The attribute id must be included.'); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Check if object has retrieve parameters. |
526
|
|
|
* |
527
|
|
|
* @return bool |
528
|
430 |
|
*/ |
529
|
|
|
public function hasRetrieveParams() |
530
|
|
|
{ |
531
|
430 |
|
return (bool) count($this->getRetrieveParams()); |
532
|
2 |
|
} |
533
|
2 |
|
|
534
|
2 |
|
/** |
535
|
2 |
|
* Check if attribute deletion. |
536
|
|
|
* |
537
|
430 |
|
* @param string $key |
538
|
|
|
* @param mixed|null $value |
539
|
|
|
* |
540
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
541
|
|
|
*/ |
542
|
|
|
private function checkIfAttributeDeletion($key, $value) |
543
|
|
|
{ |
544
|
|
|
// Don't use empty($value) instead of ($value === '') |
545
|
|
|
if ( ! is_null($value) && $value === '') |
546
|
|
|
throw new InvalidArgumentException( |
547
|
430 |
|
"You cannot set '{$key}' to an empty string. " |
548
|
|
|
. 'We interpret empty strings as \'null\' in requests. ' |
549
|
|
|
. "You may set obj->{$key} = null to delete the property" |
550
|
430 |
|
); |
551
|
430 |
|
} |
552
|
|
|
|
553
|
2 |
|
/** |
554
|
2 |
|
* Check metadata attribute. |
555
|
|
|
* |
556
|
430 |
|
* @param string $key |
557
|
|
|
* @param mixed|null $value |
558
|
|
|
* |
559
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
560
|
|
|
*/ |
561
|
|
|
private function checkMetadataAttribute($key, $value) |
562
|
|
|
{ |
563
|
430 |
|
if ( |
564
|
|
|
$key === 'metadata' && |
565
|
430 |
|
( ! is_array($value) && ! is_null($value)) |
566
|
98 |
|
) |
567
|
430 |
|
throw new InvalidArgumentException( |
568
|
|
|
'The metadata value must be an array or null, '.gettype($value).' is given' |
569
|
|
|
); |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* Check permanent attributes. |
574
|
|
|
* |
575
|
|
|
* @param string $key |
576
|
430 |
|
*/ |
577
|
|
|
private function checkPermanentAttributes($key) |
578
|
430 |
|
{ |
579
|
430 |
|
if ( ! self::$permanentAttributes->includes($key)) |
580
|
|
|
$this->unsavedValues->add($key); |
581
|
16 |
|
} |
582
|
16 |
|
|
583
|
|
|
/** |
584
|
12 |
|
* Check unsaved attributes. |
585
|
|
|
* |
586
|
|
|
* @param array $supported |
587
|
|
|
* |
588
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
589
|
|
|
*/ |
590
|
|
|
private function checkUnsavedAttributes($supported) |
591
|
|
|
{ |
592
|
|
|
if ($this->checkUnsavedAttributes === false || count($supported) === 0) |
593
|
16 |
|
return; |
594
|
|
|
|
595
|
16 |
|
$this->checkNotFoundAttributesException( |
596
|
4 |
|
$this->unsavedValues->diffKeys($supported) |
597
|
4 |
|
); |
598
|
|
|
} |
599
|
12 |
|
|
600
|
|
|
/** |
601
|
|
|
* Check not found attributes exception. |
602
|
|
|
* |
603
|
|
|
* @param array $notFound |
604
|
|
|
* |
605
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
606
|
|
|
*/ |
607
|
|
|
private function checkNotFoundAttributesException($notFound) |
608
|
|
|
{ |
609
|
|
|
if (count($notFound)) |
610
|
|
|
throw new InvalidArgumentException( |
611
|
|
|
'The attributes ['.implode(', ', $notFound).'] are not supported.' |
612
|
88 |
|
); |
613
|
|
|
} |
614
|
88 |
|
|
615
|
|
|
/* ----------------------------------------------------------------- |
616
|
88 |
|
| Other Methods |
617
|
88 |
|
| ----------------------------------------------------------------- |
618
|
|
|
*/ |
619
|
88 |
|
|
620
|
|
|
/** |
621
|
|
|
* A recursive mapping of attributes to values for this object, |
622
|
|
|
* including the proper value for deleted attributes. |
623
|
|
|
* |
624
|
|
|
* @return array |
625
|
|
|
*/ |
626
|
|
|
protected function serializeParameters() |
627
|
88 |
|
{ |
628
|
|
|
$params = []; |
629
|
88 |
|
|
630
|
74 |
|
$this->serializeUnsavedValues($params); |
631
|
|
|
$this->serializeNestedUpdatableAttributes($params); |
632
|
88 |
|
|
633
|
|
|
return $params; |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* Serialize unsaved values. |
638
|
|
|
* |
639
|
88 |
|
* @param array $params |
640
|
|
|
*/ |
641
|
88 |
|
private function serializeUnsavedValues(&$params) |
642
|
|
|
{ |
643
|
88 |
|
foreach ($this->unsavedValues->toArray() as $key) { |
644
|
88 |
|
$params[$key] = ! is_null($value = $this->$key) ? $value : ''; |
645
|
88 |
|
} |
646
|
|
|
} |
647
|
88 |
|
|
648
|
|
|
/** |
649
|
|
|
* Serialize nested updatable attributes. |
650
|
88 |
|
* |
651
|
|
|
* @param array $params |
652
|
|
|
*/ |
653
|
|
|
private function serializeNestedUpdatableAttributes(&$params) |
654
|
|
|
{ |
655
|
|
|
foreach (self::$nestedUpdatableAttributes->toArray() as $property) { |
656
|
|
|
if ( |
657
|
|
|
isset($this->$property) && |
658
|
6 |
|
$this->$property instanceof self && |
659
|
|
|
$serialized = $this->$property->serializeParameters() |
660
|
6 |
|
) { |
661
|
|
|
$params[$property] = $serialized; |
662
|
6 |
|
} |
663
|
4 |
|
} |
664
|
4 |
|
} |
665
|
4 |
|
|
666
|
|
|
/** |
667
|
|
|
* Show undefined property warning message. |
668
|
6 |
|
* |
669
|
|
|
* @param string $class |
670
|
6 |
|
* @param string $key |
671
|
|
|
*/ |
672
|
|
|
private function showUndefinedPropertyMsg($class, $key) |
673
|
|
|
{ |
674
|
|
|
$message = "Stripe Notice: Undefined property of {$class} instance: {$key}."; |
675
|
|
|
|
676
|
|
|
if ( ! $this->transientValues->isEmpty() && $this->transientValues->includes($key)) { |
677
|
4 |
|
$message .= " HINT: The [{$key}] attribute was set in the past, however. " . |
678
|
|
|
'It was then wiped when refreshing the object with the result returned by Stripe\'s API, ' . |
679
|
4 |
|
'probably as a result of a save().'.$this->showUndefinedPropertyMsgAttributes(); |
680
|
|
|
} |
681
|
4 |
|
|
682
|
|
|
if ( ! is_testing()) |
683
|
|
|
Stripe::getLogger()->error($message); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* Show available attributes for undefined property warning message. |
688
|
|
|
* |
689
|
|
|
* @return string |
690
|
|
|
*/ |
691
|
|
|
private function showUndefinedPropertyMsgAttributes() |
692
|
|
|
{ |
693
|
|
|
return count($attributes = $this->keys()) |
694
|
|
|
? ' The attributes currently available on this object are: '.implode(', ', $attributes) |
695
|
|
|
: ''; |
696
|
|
|
} |
697
|
|
|
} |
698
|
|
|
|