-
Notifications
You must be signed in to change notification settings - Fork 850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Ensure that card
is only treated as a nested updatable attribute on Source objects
#396
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,20 @@ | |
*/ | ||
class Source extends ApiResource | ||
{ | ||
/** | ||
* @return Util\Set Attributes that are nested but still updatable from | ||
* the parent class's URL (e.g. metadata). | ||
*/ | ||
public static function getNestedUpdatableAttributes() | ||
{ | ||
static $nestedUpdatableAttributes = null; | ||
if ($nestedUpdatableAttributes === null) { | ||
$nestedUpdatableAttributes = StripeObject::getNestedUpdatableAttributes(); | ||
$nestedUpdatableAttributes->add('card'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I worry that this will make it even harder to not miss edge-case. We already "break" the API often by forgetting to add those new API params to the whitelist. Now we need to add those but to potentially multiple objects now instead of just one canonical (though hidden) place in the library There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What this PR does is give the option to override the global whitelist in each class. If we're afraid that splitting up the whitelist will make it harder to maintain, we can simply keep the PR in its current form, i.e. keep the global whitelist on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand but it feels wrong/confusing. |
||
} | ||
return $nestedUpdatableAttributes; | ||
} | ||
|
||
/** | ||
* @param array|string $id The ID of the source to retrieve, or an options | ||
* array containing an `id` key. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,45 +13,53 @@ | |
class StripeObject implements ArrayAccess, JsonSerializable | ||
{ | ||
/** | ||
* @var Util\Set Attributes that should not be sent to the API because | ||
* @return Util\Set Attributes that should not be sent to the API because | ||
* they're not updatable (e.g. API key, ID). | ||
*/ | ||
public static $permanentAttributes; | ||
public static function getPermanentAttributes() | ||
{ | ||
static $permanentAttributes = null; | ||
if ($permanentAttributes === null) { | ||
$permanentAttributes = new Util\Set(array('_opts', 'id')); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh man. I was going to say that it would be nice to just move this up to a statically initialized expression so we don't have to check the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. I amended the commit so you can't see it, but my first submission used static initializations 😭 |
||
return $permanentAttributes; | ||
} | ||
|
||
/** | ||
* @var Util\Set Attributes that are nested but still updatable from | ||
* @return Util\Set Attributes that are nested but still updatable from | ||
* the parent class's URL (e.g. metadata). | ||
*/ | ||
public static $nestedUpdatableAttributes; | ||
|
||
public static function init() | ||
public static function getNestedUpdatableAttributes() | ||
{ | ||
self::$permanentAttributes = new Util\Set(array('_opts', 'id')); | ||
self::$nestedUpdatableAttributes = new Util\Set(array( | ||
// Numbers are in place for indexes in an `additional_owners` array. | ||
// | ||
// There's a maximum allowed additional owners of 3, but leave the | ||
// 4th so errors work properly. | ||
0, 1, 2, 3, 4, | ||
|
||
'additional_owners', | ||
'address', | ||
'address_kana', | ||
'address_kanji', | ||
'card', | ||
'dob', | ||
'inventory', | ||
'legal_entity', | ||
'metadata', | ||
'owner', | ||
'payout_schedule', | ||
'personal_address', | ||
'personal_address_kana', | ||
'personal_address_kanji', | ||
'shipping', | ||
'tos_acceptance', | ||
'transfer_schedule', | ||
'verification', | ||
)); | ||
static $nestedUpdatableAttributes = null; | ||
if ($nestedUpdatableAttributes === null) { | ||
$nestedUpdatableAttributes = new Util\Set(array( | ||
// Numbers are in place for indexes in an `additional_owners` array. | ||
// | ||
// There's a maximum allowed additional owners of 3, but leave the | ||
// 4th so errors work properly. | ||
0, 1, 2, 3, 4, | ||
|
||
'additional_owners', | ||
'address', | ||
'address_kana', | ||
'address_kanji', | ||
'dob', | ||
'inventory', | ||
'legal_entity', | ||
'metadata', | ||
'owner', | ||
'payout_schedule', | ||
'personal_address', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This lives under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The attributes defined here don't have to be top-level attributes in a given object, they can themselves be nested under other attributes (else the current code would not work either). So my intent was to move all of the identity verification attributes under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be bullish in moving these under something like |
||
'personal_address_kana', | ||
'personal_address_kanji', | ||
'shipping', | ||
'tos_acceptance', | ||
'transfer_schedule', | ||
'verification', | ||
)); | ||
} | ||
return $nestedUpdatableAttributes; | ||
} | ||
|
||
/** | ||
|
@@ -112,14 +120,14 @@ public function __set($k, $v) | |
); | ||
} | ||
|
||
if (self::$nestedUpdatableAttributes->includes($k) | ||
if (static::getNestedUpdatableAttributes()->includes($k) | ||
&& isset($this->$k) && $this->$k instanceof AttachedObject && is_array($v)) { | ||
$this->$k->replaceWith($v); | ||
} else { | ||
// TODO: may want to clear from $_transientValues (Won't be user-visible). | ||
$this->_values[$k] = $v; | ||
} | ||
if (!self::$permanentAttributes->includes($k)) { | ||
if (!static::getPermanentAttributes()->includes($k)) { | ||
$this->_unsavedValues->add($k); | ||
} | ||
} | ||
|
@@ -223,19 +231,19 @@ public function refreshFrom($values, $opts, $partial = false) | |
} | ||
|
||
foreach ($removed as $k) { | ||
if (self::$permanentAttributes->includes($k)) { | ||
if (static::getPermanentAttributes()->includes($k)) { | ||
continue; | ||
} | ||
|
||
unset($this->$k); | ||
} | ||
|
||
foreach ($values as $k => $v) { | ||
if (self::$permanentAttributes->includes($k) && isset($this[$k])) { | ||
if (static::getPermanentAttributes()->includes($k) && isset($this[$k])) { | ||
continue; | ||
} | ||
|
||
if (self::$nestedUpdatableAttributes->includes($k) && is_array($v)) { | ||
if (static::getNestedUpdatableAttributes()->includes($k) && is_array($v)) { | ||
$this->_values[$k] = AttachedObject::constructFrom($v, $opts); | ||
} else { | ||
$this->_values[$k] = Util\Util::convertToStripeObject($v, $opts); | ||
|
@@ -265,7 +273,7 @@ public function serializeParameters() | |
} | ||
|
||
// Get nested updates. | ||
foreach (self::$nestedUpdatableAttributes->toArray() as $property) { | ||
foreach (static::getNestedUpdatableAttributes()->toArray() as $property) { | ||
if (isset($this->$property)) { | ||
if ($this->$property instanceof StripeObject) { | ||
$serialized = $this->$property->serializeParameters(); | ||
|
@@ -308,5 +316,3 @@ public function __toArray($recursive = false) | |
} | ||
} | ||
} | ||
|
||
StripeObject::init(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add a comment somewhere in here that gives a breakdown similar to the one in this PR's description? I feel like this is a pretty difficult-to-understand complexity of the PHP library and I worry that the reasons we have
card
in here will be forgotten pretty quickly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍