PageRenderTime 59ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/salejunction/yith-essential-kit-for-woocommerce-1/modules/yith-woocommerce-stripe/includes/third-party/lib/Object.php

https://gitlab.com/hop23typhu/list-theme
PHP | 247 lines | 208 code | 24 blank | 15 comment | 29 complexity | f9ccea69eb9ea0b6f14cca7b690a183b MD5 | raw file
  1. <?php
  2. namespace Stripe;
  3. use ArrayAccess;
  4. use InvalidArgumentException;
  5. class Object implements ArrayAccess
  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('metadata'));
  21. }
  22. protected $_opts;
  23. protected $_values;
  24. protected $_unsavedValues;
  25. protected $_transientValues;
  26. protected $_retrieveOptions;
  27. public function __construct($id = null, $opts = null)
  28. {
  29. $this->_opts = $opts ? $opts : new Util\RequestOptions();
  30. $this->_values = array();
  31. $this->_unsavedValues = new Util\Set();
  32. $this->_transientValues = new Util\Set();
  33. $this->_retrieveOptions = array();
  34. if (is_array($id)) {
  35. foreach ($id as $key => $value) {
  36. if ($key != 'id') {
  37. $this->_retrieveOptions[$key] = $value;
  38. }
  39. }
  40. $id = $id['id'];
  41. }
  42. if ($id !== null) {
  43. $this->id = $id;
  44. }
  45. }
  46. // Standard accessor magic methods
  47. public function __set($k, $v)
  48. {
  49. if ($v === "") {
  50. throw new InvalidArgumentException(
  51. 'You cannot set \''.$k.'\'to an empty string. '
  52. .'We interpret empty strings as NULL in requests. '
  53. .'You may set obj->'.$k.' = NULL to delete the property'
  54. );
  55. }
  56. if (self::$nestedUpdatableAttributes->includes($k)
  57. && isset($this->$k) && is_array($v)) {
  58. $this->$k->replaceWith($v);
  59. } else {
  60. // TODO: may want to clear from $_transientValues (Won't be user-visible).
  61. $this->_values[$k] = $v;
  62. }
  63. if (!self::$permanentAttributes->includes($k)) {
  64. $this->_unsavedValues->add($k);
  65. }
  66. }
  67. public function __isset($k)
  68. {
  69. return isset($this->_values[$k]);
  70. }
  71. public function __unset($k)
  72. {
  73. unset($this->_values[$k]);
  74. $this->_transientValues->add($k);
  75. $this->_unsavedValues->discard($k);
  76. }
  77. public function __get($k)
  78. {
  79. if (array_key_exists($k, $this->_values)) {
  80. return $this->_values[$k];
  81. } else if ($this->_transientValues->includes($k)) {
  82. $class = get_class($this);
  83. $attrs = join(', ', array_keys($this->_values));
  84. $message = "Stripe Notice: Undefined property of $class instance: $k. "
  85. . "HINT: The $k attribute was set in the past, however. "
  86. . "It was then wiped when refreshing the object "
  87. . "with the result returned by Stripe's API, "
  88. . "probably as a result of a save(). The attributes currently "
  89. . "available on this object are: $attrs";
  90. error_log($message);
  91. return null;
  92. } else {
  93. $class = get_class($this);
  94. error_log("Stripe Notice: Undefined property of $class instance: $k");
  95. return null;
  96. }
  97. }
  98. // ArrayAccess methods
  99. public function offsetSet($k, $v)
  100. {
  101. $this->$k = $v;
  102. }
  103. public function offsetExists($k)
  104. {
  105. return array_key_exists($k, $this->_values);
  106. }
  107. public function offsetUnset($k)
  108. {
  109. unset($this->$k);
  110. }
  111. public function offsetGet($k)
  112. {
  113. return array_key_exists($k, $this->_values) ? $this->_values[$k] : null;
  114. }
  115. public function keys()
  116. {
  117. return array_keys($this->_values);
  118. }
  119. /**
  120. * This unfortunately needs to be public to be used in Util\Util
  121. *
  122. * @param array $values
  123. * @param array $opts
  124. *
  125. * @return Object The object constructed from the given values.
  126. */
  127. public static function constructFrom($values, $opts)
  128. {
  129. $obj = new static(isset($values['id']) ? $values['id'] : null);
  130. $obj->refreshFrom($values, $opts);
  131. return $obj;
  132. }
  133. /**
  134. * Refreshes this object using the provided values.
  135. *
  136. * @param array $values
  137. * @param array $opts
  138. * @param boolean $partial Defaults to false.
  139. */
  140. public function refreshFrom($values, $opts, $partial = false)
  141. {
  142. $this->_opts = $opts;
  143. // Wipe old state before setting new. This is useful for e.g. updating a
  144. // customer, where there is no persistent card parameter. Mark those values
  145. // which don't persist as transient
  146. if ($partial) {
  147. $removed = new Util\Set();
  148. } else {
  149. $removed = array_diff(array_keys($this->_values), array_keys($values));
  150. }
  151. foreach ($removed as $k) {
  152. if (self::$permanentAttributes->includes($k)) {
  153. continue;
  154. }
  155. unset($this->$k);
  156. }
  157. foreach ($values as $k => $v) {
  158. if (self::$permanentAttributes->includes($k) && isset($this[$k])) {
  159. continue;
  160. }
  161. if (self::$nestedUpdatableAttributes->includes($k) && is_array($v)) {
  162. $this->_values[$k] = AttachedObject::constructFrom($v, $opts);
  163. } else {
  164. $this->_values[$k] = Util\Util::convertToStripeObject($v, $opts);
  165. }
  166. $this->_transientValues->discard($k);
  167. $this->_unsavedValues->discard($k);
  168. }
  169. }
  170. /**
  171. * @return array A recursive mapping of attributes to values for this object,
  172. * including the proper value for deleted attributes.
  173. */
  174. public function serializeParameters()
  175. {
  176. $params = array();
  177. if ($this->_unsavedValues) {
  178. foreach ($this->_unsavedValues->toArray() as $k) {
  179. $v = $this->$k;
  180. if ($v === null) {
  181. $v = '';
  182. }
  183. $params[$k] = $v;
  184. }
  185. }
  186. // Get nested updates.
  187. foreach (self::$nestedUpdatableAttributes->toArray() as $property) {
  188. if (isset($this->$property) && $this->$property instanceof Object) {
  189. $params[$property] = $this->$property->serializeParameters();
  190. }
  191. }
  192. return $params;
  193. }
  194. public function __toJSON()
  195. {
  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. }
  202. public function __toString()
  203. {
  204. $class = get_class($this);
  205. return $class . ' JSON: ' . $this->__toJSON();
  206. }
  207. public function __toArray($recursive = false)
  208. {
  209. if ($recursive) {
  210. return Util\Util::convertStripeObjectToArray($this->_values);
  211. } else {
  212. return $this->_values;
  213. }
  214. }
  215. }
  216. Object::init();