HasAttributes.php 4.7 KB

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