M2_Stripe_Object

The Membership 2 M2 Stripe Object class.

Defined (1)

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

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