PPPStripeStripeObject

Class StripeObject.

Defined (1)

The class is defined in the following location(s).

/includes/api/stripe-php/lib/StripeObject.php  
  1. class StripeObject implements ArrayAccess, JsonSerializable 
  2. /** 
  3. * @var Util\Set Attributes that should not be sent to the API because 
  4. * they're not updatable (e.g. API key, ID). 
  5. */ 
  6. public static $permanentAttributes; 
  7. /** 
  8. * @var Util\Set Attributes that are nested but still updatable from 
  9. * the parent class's URL (e.g. metadata). 
  10. */ 
  11. public static $nestedUpdatableAttributes; 
  12.  
  13. public static function init() 
  14. self::$permanentAttributes = new Util\Set(array('_opts', 'id')); 
  15. self::$nestedUpdatableAttributes = new Util\Set(array( 
  16. 'metadata', 'legal_entity', 'address', 'dob', 'transfer_schedule', 'verification',  
  17. 'tos_acceptance', 'personal_address',  
  18. // will make the array into an AttachedObject: weird, but works for now 
  19. 'additional_owners', 0, 1, 2, 3, 4, // Max 3, but leave the 4th so errors work properly 
  20. 'inventory' 
  21. )); 
  22.  
  23. /** 
  24. * @return object The last response from the Stripe API 
  25. */ 
  26. public function getLastResponse() 
  27. return $this->_lastResponse; 
  28.  
  29. /** 
  30. * @param ApiResponse 
  31. * @return void Set the last response from the Stripe API 
  32. */ 
  33. public function setLastResponse($resp) 
  34. $this->_lastResponse = $resp; 
  35.  
  36. protected $_opts; 
  37. protected $_values; 
  38. protected $_unsavedValues; 
  39. protected $_transientValues; 
  40. protected $_retrieveOptions; 
  41. protected $_lastResponse; 
  42.  
  43. public function __construct($id = null, $opts = null) 
  44. $this->_opts = $opts ? $opts : new Util\RequestOptions(); 
  45. $this->_values = array(); 
  46. $this->_unsavedValues = new Util\Set(); 
  47. $this->_transientValues = new Util\Set(); 
  48.  
  49. $this->_retrieveOptions = array(); 
  50. if (is_array($id)) { 
  51. foreach ($id as $key => $value) { 
  52. if ($key != 'id') { 
  53. $this->_retrieveOptions[$key] = $value; 
  54. $id = $id['id']; 
  55.  
  56. if ($id !== null) { 
  57. $this->id = $id; 
  58.  
  59. // Standard accessor magic methods 
  60. public function __set($k, $v) 
  61. if ($v === "") { 
  62. throw new InvalidArgumentException( 
  63. 'You cannot set \''.$k.'\'to an empty string. ' 
  64. .'We interpret empty strings as NULL in requests. ' 
  65. .'You may set obj->'.$k.' = NULL to delete the property' 
  66. ); 
  67.  
  68. if (self::$nestedUpdatableAttributes->includes($k) 
  69. && isset($this->$k) && $this->$k instanceof AttachedObject && is_array($v)) { 
  70. $this->$k->replaceWith($v); 
  71. } else { 
  72. // TODO: may want to clear from $_transientValues (Won't be user-visible). 
  73. $this->_values[$k] = $v; 
  74. if (!self::$permanentAttributes->includes($k)) { 
  75. $this->_unsavedValues->add($k); 
  76.  
  77. public function __isset($k) 
  78. return isset($this->_values[$k]); 
  79. public function __unset($k) 
  80. unset($this->_values[$k]); 
  81. $this->_transientValues->add($k); 
  82. $this->_unsavedValues->discard($k); 
  83. public function &__get($k) 
  84. // function should return a reference, using $nullval to return a reference to null 
  85. $nullval = null; 
  86. if (!empty($this->_values) && array_key_exists($k, $this->_values)) { 
  87. return $this->_values[$k]; 
  88. } else if (!empty($this->_transientValues) && $this->_transientValues->includes($k)) { 
  89. $class = get_class($this); 
  90. $attrs = join(', ', array_keys($this->_values)); 
  91. $message = "Stripe Notice: Undefined property of $class instance: $k. " 
  92. . "HINT: The $k attribute was set in the past, however. " 
  93. . "It was then wiped when refreshing the object " 
  94. . "with the result returned by Stripe's API, " 
  95. . "probably as a result of a save(). The attributes currently " 
  96. . "available on this object are: $attrs"; 
  97. error_log($message); 
  98. return $nullval; 
  99. } else { 
  100. $class = get_class($this); 
  101. error_log("Stripe Notice: Undefined property of $class instance: $k"); 
  102. return $nullval; 
  103.  
  104. // ArrayAccess methods 
  105. public function offsetSet($k, $v) 
  106. $this->$k = $v; 
  107.  
  108. public function offsetExists($k) 
  109. return array_key_exists($k, $this->_values); 
  110.  
  111. public function offsetUnset($k) 
  112. unset($this->$k); 
  113. public function offsetGet($k) 
  114. return array_key_exists($k, $this->_values) ? $this->_values[$k] : null; 
  115.  
  116. public function keys() 
  117. return array_keys($this->_values); 
  118.  
  119. /** 
  120. * This unfortunately needs to be public to be used in Util\Util 
  121. * @param array $values 
  122. * @param array $opts 
  123. * @return StripeObject The object constructed from the given values. 
  124. */ 
  125. public static function constructFrom($values, $opts) 
  126. $obj = new static(isset($values['id']) ? $values['id'] : null); 
  127. $obj->refreshFrom($values, $opts); 
  128. return $obj; 
  129.  
  130. /** 
  131. * Refreshes this object using the provided values. 
  132. * @param array $values 
  133. * @param array|Util\RequestOptions $opts 
  134. * @param boolean $partial Defaults to false. 
  135. */ 
  136. public function refreshFrom($values, $opts, $partial = false) 
  137. if (is_array($opts)) { 
  138. $opts = Util\RequestOptions::parse($opts); 
  139.  
  140. $this->_opts = $opts; 
  141.  
  142. // Wipe old state before setting new. This is useful for e.g. updating a 
  143. // customer, where there is no persistent card parameter. Mark those values 
  144. // which don't persist as transient 
  145. if ($partial) { 
  146. $removed = new Util\Set(); 
  147. } else { 
  148. $removed = array_diff(array_keys($this->_values), array_keys($values)); 
  149.  
  150. foreach ($removed as $k) { 
  151. if (self::$permanentAttributes->includes($k)) { 
  152. continue; 
  153.  
  154. unset($this->$k); 
  155.  
  156. foreach ($values as $k => $v) { 
  157. if (self::$permanentAttributes->includes($k) && isset($this[$k])) { 
  158. continue; 
  159.  
  160. if (self::$nestedUpdatableAttributes->includes($k) && is_array($v)) { 
  161. $this->_values[$k] = AttachedObject::constructFrom($v, $opts); 
  162. } else { 
  163. $this->_values[$k] = Util\Util::convertToStripeObject($v, $opts); 
  164.  
  165. $this->_transientValues->discard($k); 
  166. $this->_unsavedValues->discard($k); 
  167.  
  168. /** 
  169. * @return array A recursive mapping of attributes to values for this object,  
  170. * including the proper value for deleted attributes. 
  171. */ 
  172. public function serializeParameters() 
  173. $params = array(); 
  174. if ($this->_unsavedValues) { 
  175. foreach ($this->_unsavedValues->toArray() as $k) { 
  176. $v = $this->$k; 
  177. if ($v === null) { 
  178. $v = ''; 
  179.  
  180. $params[$k] = $v; 
  181.  
  182. // Get nested updates. 
  183. foreach (self::$nestedUpdatableAttributes->toArray() as $property) { 
  184. if (isset($this->$property)) { 
  185. if ($this->$property instanceof StripeObject) { 
  186. $serialized = $this->$property->serializeParameters(); 
  187. if ($serialized) { 
  188. $params[$property] = $serialized; 
  189.  
  190. return $params; 
  191.  
  192. public function jsonSerialize() 
  193. return $this->__toArray(true); 
  194.  
  195. public function __toJSON() 
  196. if (defined('JSON_PRETTY_PRINT')) { 
  197. return json_encode($this->__toArray(true), JSON_PRETTY_PRINT); 
  198. } else { 
  199. return json_encode($this->__toArray(true)); 
  200.  
  201. public function __toString() 
  202. $class = get_class($this); 
  203. return $class . ' JSON: ' . $this->__toJSON(); 
  204.  
  205. public function __toArray($recursive = false) 
  206. if ($recursive) { 
  207. return Util\Util::convertStripeObjectToArray($this->_values); 
  208. } else { 
  209. return $this->_values;