Skip to content

Commit

Permalink
Ensure that card is only treated as a nested updatable attribute on…
Browse files Browse the repository at this point in the history
… Source objects
  • Loading branch information
ob-stripe committed Nov 30, 2017
1 parent 82228e4 commit 9c981a0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 42 deletions.
14 changes: 14 additions & 0 deletions lib/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
return $nestedUpdatableAttributes;
}

/**
* @param array|string $id The ID of the source to retrieve, or an options
* array containing an `id` key.
Expand Down
88 changes: 47 additions & 41 deletions lib/StripeObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
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',
'personal_address_kana',
'personal_address_kanji',
'shipping',
'tos_acceptance',
'transfer_schedule',
'verification',
));
}
return $nestedUpdatableAttributes;
}

/**
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -308,5 +316,3 @@ public function __toArray($recursive = false)
}
}
}

StripeObject::init();
1 change: 0 additions & 1 deletion tests/StripeObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public function testJsonEncode()

public function testReplaceNewNestedUpdatable()
{
StripeObject::init(); // Populate the $nestedUpdatableAttributes Set
$s = new StripeObject();

$s->metadata = array('bar');
Expand Down
17 changes: 17 additions & 0 deletions tests/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,21 @@ public function testUrls()
$token = new Token('abcd/efgh');
$this->assertSame($token->instanceUrl(), '/v1/tokens/abcd%2Fefgh');
}

public function testNestedCardObject()
{
$token = Token::constructFrom(
array(
'id' => 'tok_foo',
'object' => 'token',
'card' => array(
'id' => 'card_foo',
'object' => 'card',
),
),
new Util\RequestOptions()
);
$this->assertSame("Stripe\\Token", get_class($token));
$this->assertSame("Stripe\\Card", get_class($token->card));
}
}

0 comments on commit 9c981a0

Please sign in to comment.