Attribute.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /**
  11. * Attributes.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\Support;
  20. use EasyWeChat\Core\Exceptions\InvalidArgumentException;
  21. /**
  22. * Class Attributes.
  23. */
  24. abstract class Attribute extends Collection
  25. {
  26. /**
  27. * Attributes alias.
  28. *
  29. * @var array
  30. */
  31. protected $aliases = [];
  32. /**
  33. * Auto snake attribute name.
  34. *
  35. * @var bool
  36. */
  37. protected $snakeable = true;
  38. /**
  39. * Required attributes.
  40. *
  41. * @var array
  42. */
  43. protected $requirements = [];
  44. /**
  45. * Constructor.
  46. *
  47. * @param array $attributes
  48. */
  49. public function __construct(array $attributes = [])
  50. {
  51. parent::__construct($attributes);
  52. }
  53. /**
  54. * Set attribute.
  55. *
  56. * @param string $attribute
  57. * @param string $value
  58. *
  59. * @return Attribute
  60. */
  61. public function setAttribute($attribute, $value)
  62. {
  63. $this->set($attribute, $value);
  64. return $this;
  65. }
  66. /**
  67. * Get attribute.
  68. *
  69. * @param string $attribute
  70. * @param mixed $default
  71. *
  72. * @return mixed
  73. */
  74. public function getAttribute($attribute, $default)
  75. {
  76. return $this->get($attribute, $default);
  77. }
  78. /**
  79. * Set attribute.
  80. *
  81. * @param string $attribute
  82. * @param mixed $value
  83. *
  84. * @return Attribute
  85. *
  86. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  87. */
  88. public function with($attribute, $value)
  89. {
  90. $this->snakeable && $attribute = Str::snake($attribute);
  91. if (!$this->validate($attribute, $value)) {
  92. throw new InvalidArgumentException("Invalid attribute '{$attribute}'.");
  93. }
  94. $this->set($attribute, $value);
  95. return $this;
  96. }
  97. /**
  98. * Attribute validation.
  99. *
  100. * @param string $attribute
  101. * @param mixed $value
  102. *
  103. * @return bool
  104. */
  105. protected function validate($attribute, $value)
  106. {
  107. return true;
  108. }
  109. /**
  110. * Override parent set() method.
  111. *
  112. * @param string $attribute
  113. * @param mixed $value
  114. */
  115. public function set($attribute, $value = null)
  116. {
  117. parent::set($this->getRealKey($attribute), $value);
  118. }
  119. /**
  120. * Override parent get() method.
  121. *
  122. * @param string $attribute
  123. * @param mixed $default
  124. *
  125. * @return mixed
  126. */
  127. public function get($attribute, $default = null)
  128. {
  129. return parent::get($this->getRealKey($attribute), $default);
  130. }
  131. /**
  132. * Magic call.
  133. *
  134. * @param string $method
  135. * @param array $args
  136. *
  137. * @return Attribute
  138. */
  139. public function __call($method, $args)
  140. {
  141. if (0 === stripos($method, 'with')) {
  142. $method = substr($method, 4);
  143. }
  144. return $this->with($method, array_shift($args));
  145. }
  146. /**
  147. * Magic set.
  148. *
  149. * @param string $property
  150. * @param mixed $value
  151. *
  152. * @return Attribute
  153. */
  154. public function __set($property, $value)
  155. {
  156. return $this->with($property, $value);
  157. }
  158. /**
  159. * Whether or not an data exists by key.
  160. *
  161. * @param string $key
  162. *
  163. * @return bool
  164. */
  165. public function __isset($key)
  166. {
  167. return parent::__isset($this->getRealKey($key));
  168. }
  169. /**
  170. * Return the raw name of attribute.
  171. *
  172. * @param string $key
  173. *
  174. * @return string
  175. */
  176. protected function getRealKey($key)
  177. {
  178. if ($alias = array_search($key, $this->aliases, true)) {
  179. $key = $alias;
  180. }
  181. return $key;
  182. }
  183. /**
  184. * Check required attributes.
  185. *
  186. * @throws InvalidArgumentException
  187. */
  188. protected function checkRequiredAttributes()
  189. {
  190. foreach ($this->requirements as $attribute) {
  191. if (!isset($this->$attribute)) {
  192. throw new InvalidArgumentException(" '{$attribute}' cannot be empty.");
  193. }
  194. }
  195. }
  196. /**
  197. * Return all items.
  198. *
  199. * @return array
  200. *
  201. * @throws InvalidArgumentException
  202. */
  203. public function all()
  204. {
  205. $this->checkRequiredAttributes();
  206. return parent::all();
  207. }
  208. }