StripeObject.php 8.5 KB

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