HasAttributes.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\Kernel\Traits;
  16. use ByteDance\Kernel\Exceptions\InvalidArgumentException;
  17. use ByteDance\Kernel\Support\Arr;
  18. use ByteDance\Kernel\Support\Str;
  19. /**
  20. * Trait Attributes.
  21. */
  22. trait HasAttributes
  23. {
  24. /**
  25. * @var array
  26. */
  27. protected $attributes = [];
  28. /**
  29. * @var bool
  30. */
  31. protected $snakeable = true;
  32. /**
  33. * Set Attributes.
  34. *
  35. * @param array $attributes
  36. *
  37. * @return $this
  38. */
  39. public function setAttributes(array $attributes = [])
  40. {
  41. $this->attributes = $attributes;
  42. return $this;
  43. }
  44. /**
  45. * Set attribute.
  46. *
  47. * @param string $attribute
  48. * @param string $value
  49. *
  50. * @return $this
  51. */
  52. public function setAttribute($attribute, $value)
  53. {
  54. Arr::set($this->attributes, $attribute, $value);
  55. return $this;
  56. }
  57. /**
  58. * Get attribute.
  59. *
  60. * @param string $attribute
  61. * @param mixed $default
  62. *
  63. * @return mixed
  64. */
  65. public function getAttribute($attribute, $default = null)
  66. {
  67. return Arr::get($this->attributes, $attribute, $default);
  68. }
  69. /**
  70. * @param string $attribute
  71. *
  72. * @return bool
  73. */
  74. public function isRequired($attribute)
  75. {
  76. return in_array($attribute, $this->getRequired(), true);
  77. }
  78. /**
  79. * @return array|mixed
  80. */
  81. public function getRequired()
  82. {
  83. return property_exists($this, 'required') ? $this->required : [];
  84. }
  85. /**
  86. * Set attribute.
  87. *
  88. * @param string $attribute
  89. * @param mixed $value
  90. *
  91. * @return $this
  92. */
  93. public function with($attribute, $value)
  94. {
  95. $this->snakeable && $attribute = Str::snake($attribute);
  96. $this->setAttribute($attribute, $value);
  97. return $this;
  98. }
  99. /**
  100. * Override parent set() method.
  101. *
  102. * @param string $attribute
  103. * @param mixed $value
  104. *
  105. * @return $this
  106. */
  107. public function set($attribute, $value)
  108. {
  109. $this->setAttribute($attribute, $value);
  110. return $this;
  111. }
  112. /**
  113. * Override parent get() method.
  114. *
  115. * @param string $attribute
  116. * @param mixed $default
  117. *
  118. * @return mixed
  119. */
  120. public function get($attribute, $default = null)
  121. {
  122. return $this->getAttribute($attribute, $default);
  123. }
  124. /**
  125. * @param string $key
  126. *
  127. * @return bool
  128. */
  129. public function has(string $key)
  130. {
  131. return Arr::has($this->attributes, $key);
  132. }
  133. /**
  134. * @param array $attributes
  135. *
  136. * @return $this
  137. */
  138. public function merge(array $attributes)
  139. {
  140. $this->attributes = array_merge($this->attributes, $attributes);
  141. return $this;
  142. }
  143. /**
  144. * @param array|string $keys
  145. *
  146. * @return array
  147. */
  148. public function only($keys)
  149. {
  150. return Arr::only($this->attributes, $keys);
  151. }
  152. /**
  153. * Return all items.
  154. *
  155. * @return array
  156. */
  157. public function all()
  158. {
  159. $this->checkRequiredAttributes();
  160. return $this->attributes;
  161. }
  162. /**
  163. * Magic call.
  164. *
  165. * @param string $method
  166. * @param array $args
  167. *
  168. * @return $this
  169. */
  170. public function __call($method, $args)
  171. {
  172. if (0 === stripos($method, 'with')) {
  173. return $this->with(substr($method, 4), array_shift($args));
  174. }
  175. throw new \BadMethodCallException(sprintf('Method "%s" does not exists.', $method));
  176. }
  177. /**
  178. * Magic get.
  179. *
  180. * @param string $property
  181. *
  182. * @return mixed
  183. */
  184. public function __get($property)
  185. {
  186. return $this->get($property);
  187. }
  188. /**
  189. * Magic set.
  190. *
  191. * @param string $property
  192. * @param mixed $value
  193. *
  194. * @return $this
  195. */
  196. public function __set($property, $value)
  197. {
  198. return $this->with($property, $value);
  199. }
  200. /**
  201. * Whether or not an data exists by key.
  202. *
  203. * @param string $key
  204. *
  205. * @return bool
  206. */
  207. public function __isset($key)
  208. {
  209. return isset($this->attributes[$key]);
  210. }
  211. /**
  212. * Check required attributes.
  213. *
  214. * @throws \ByteDance\Kernel\Exceptions\InvalidArgumentException
  215. */
  216. protected function checkRequiredAttributes()
  217. {
  218. foreach ($this->getRequired() as $attribute) {
  219. if (is_null($this->get($attribute))) {
  220. throw new InvalidArgumentException(sprintf('"%s" cannot be empty.', $attribute));
  221. }
  222. }
  223. }
  224. }