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