PageRenderTime 601ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/stripe-woocommerce-addon/lib/lib/StripeObject.php

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