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