Path: blob/master/externals/stripe-php/lib/Stripe/Object.php
12256 views
<?php12class Stripe_Object implements ArrayAccess3{4/**5* @var Stripe_Util_Set Attributes that should not be sent to the API because6* they're not updatable (e.g. API key, ID).7*/8public static $permanentAttributes;9/**10* @var Stripe_Util_Set Attributes that are nested but still updatable from11* the parent class's URL (e.g. metadata).12*/13public static $nestedUpdatableAttributes;1415public static function init()16{17self::$permanentAttributes = new Stripe_Util_Set(array('_apiKey', 'id'));18self::$nestedUpdatableAttributes = new Stripe_Util_Set(array('metadata'));19}2021protected $_apiKey;22protected $_values;23protected $_unsavedValues;24protected $_transientValues;25protected $_retrieveOptions;2627public function __construct($id=null, $apiKey=null)28{29$this->_apiKey = $apiKey;30$this->_values = array();31$this->_unsavedValues = new Stripe_Util_Set();32$this->_transientValues = new Stripe_Util_Set();3334$this->_retrieveOptions = array();35if (is_array($id)) {36foreach ($id as $key => $value) {37if ($key != 'id') {38$this->_retrieveOptions[$key] = $value;39}40}41$id = $id['id'];42}4344if ($id !== null) {45$this->id = $id;46}47}4849// Standard accessor magic methods50public function __set($k, $v)51{52if ($v === "") {53throw new InvalidArgumentException(54'You cannot set \''.$k.'\'to an empty string. '55.'We interpret empty strings as NULL in requests. '56.'You may set obj->'.$k.' = NULL to delete the property'57);58}5960if (self::$nestedUpdatableAttributes->includes($k)61&& isset($this->$k) && is_array($v)) {62$this->$k->replaceWith($v);63} else {64// TODO: may want to clear from $_transientValues (Won't be user-visible).65$this->_values[$k] = $v;66}67if (!self::$permanentAttributes->includes($k))68$this->_unsavedValues->add($k);69}70public function __isset($k)71{72return isset($this->_values[$k]);73}74public function __unset($k)75{76unset($this->_values[$k]);77$this->_transientValues->add($k);78$this->_unsavedValues->discard($k);79}80public function __get($k)81{82if (array_key_exists($k, $this->_values)) {83return $this->_values[$k];84} else if ($this->_transientValues->includes($k)) {85$class = get_class($this);86$attrs = join(', ', array_keys($this->_values));87$message = "Stripe Notice: Undefined property of $class instance: $k. "88. "HINT: The $k attribute was set in the past, however. "89. "It was then wiped when refreshing the object "90. "with the result returned by Stripe's API, "91. "probably as a result of a save(). The attributes currently "92. "available on this object are: $attrs";93error_log($message);94return null;95} else {96$class = get_class($this);97error_log("Stripe Notice: Undefined property of $class instance: $k");98return null;99}100}101102// ArrayAccess methods103public function offsetSet($k, $v)104{105$this->$k = $v;106}107108public function offsetExists($k)109{110return array_key_exists($k, $this->_values);111}112113public function offsetUnset($k)114{115unset($this->$k);116}117public function offsetGet($k)118{119return array_key_exists($k, $this->_values) ? $this->_values[$k] : null;120}121122public function keys()123{124return array_keys($this->_values);125}126127/**128* This unfortunately needs to be public to be used in Util.php129*130* @param string $class131* @param array $values132* @param string|null $apiKey133*134* @return Stripe_Object The object constructed from the given values.135*/136public static function scopedConstructFrom($class, $values, $apiKey=null)137{138$obj = new $class(isset($values['id']) ? $values['id'] : null, $apiKey);139$obj->refreshFrom($values, $apiKey);140return $obj;141}142143/**144* @param array $values145* @param string|null $apiKey146*147* @return Stripe_Object The object of the same class as $this constructed148* from the given values.149*/150public static function constructFrom($values, $apiKey=null)151{152return self::scopedConstructFrom(__CLASS__, $values, $apiKey);153}154155/**156* Refreshes this object using the provided values.157*158* @param array $values159* @param string $apiKey160* @param boolean $partial Defaults to false.161*/162public function refreshFrom($values, $apiKey, $partial=false)163{164$this->_apiKey = $apiKey;165166// Wipe old state before setting new. This is useful for e.g. updating a167// customer, where there is no persistent card parameter. Mark those values168// which don't persist as transient169if ($partial) {170$removed = new Stripe_Util_Set();171} else {172$removed = array_diff(array_keys($this->_values), array_keys($values));173}174175foreach ($removed as $k) {176if (self::$permanentAttributes->includes($k))177continue;178unset($this->$k);179}180181foreach ($values as $k => $v) {182if (self::$permanentAttributes->includes($k) && isset($this[$k]))183continue;184185if (self::$nestedUpdatableAttributes->includes($k) && is_array($v)) {186$this->_values[$k] = Stripe_Object::scopedConstructFrom(187'Stripe_AttachedObject', $v, $apiKey188);189} else {190$this->_values[$k] = Stripe_Util::convertToStripeObject($v, $apiKey);191}192193$this->_transientValues->discard($k);194$this->_unsavedValues->discard($k);195}196}197198/**199* @return array A recursive mapping of attributes to values for this object,200* including the proper value for deleted attributes.201*/202public function serializeParameters()203{204$params = array();205if ($this->_unsavedValues) {206foreach ($this->_unsavedValues->toArray() as $k) {207$v = $this->$k;208if ($v === NULL) {209$v = '';210}211$params[$k] = $v;212}213}214215// Get nested updates.216foreach (self::$nestedUpdatableAttributes->toArray() as $property) {217if (isset($this->$property)218&& $this->$property instanceOf Stripe_Object) {219$params[$property] = $this->$property->serializeParameters();220}221}222return $params;223}224225// Pretend to have late static bindings, even in PHP 5.2226protected function _lsb($method)227{228$class = get_class($this);229$args = array_slice(func_get_args(), 1);230return call_user_func_array(array($class, $method), $args);231}232protected static function _scopedLsb($class, $method)233{234$args = array_slice(func_get_args(), 2);235return call_user_func_array(array($class, $method), $args);236}237238public function __toJSON()239{240if (defined('JSON_PRETTY_PRINT')) {241return json_encode($this->__toArray(true), JSON_PRETTY_PRINT);242} else {243return json_encode($this->__toArray(true));244}245}246247public function __toString()248{249return $this->__toJSON();250}251252public function __toArray($recursive=false)253{254if ($recursive) {255return Stripe_Util::convertStripeObjectToArray($this->_values);256} else {257return $this->_values;258}259}260}261262263Stripe_Object::init();264265266