diff --git a/lib/Source.php b/lib/Source.php index f01af43d51..c8df53dfac 100644 --- a/lib/Source.php +++ b/lib/Source.php @@ -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. diff --git a/lib/StripeObject.php b/lib/StripeObject.php index 07c5dc1fc3..8020931d68 100644 --- a/lib/StripeObject.php +++ b/lib/StripeObject.php @@ -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; } /** @@ -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,7 +231,7 @@ public function refreshFrom($values, $opts, $partial = false) } foreach ($removed as $k) { - if (self::$permanentAttributes->includes($k)) { + if (static::getPermanentAttributes()->includes($k)) { continue; } @@ -231,11 +239,11 @@ public function refreshFrom($values, $opts, $partial = false) } 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(); diff --git a/tests/StripeObjectTest.php b/tests/StripeObjectTest.php index 5f92222827..6fae14609d 100644 --- a/tests/StripeObjectTest.php +++ b/tests/StripeObjectTest.php @@ -97,7 +97,6 @@ public function testJsonEncode() public function testReplaceNewNestedUpdatable() { - StripeObject::init(); // Populate the $nestedUpdatableAttributes Set $s = new StripeObject(); $s->metadata = array('bar'); diff --git a/tests/TokenTest.php b/tests/TokenTest.php index 60ec76a154..52181b042f 100644 --- a/tests/TokenTest.php +++ b/tests/TokenTest.php @@ -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)); + } }