1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaylyu\Alipay\Kernel\Traits; |
4
|
|
|
|
5
|
|
|
use Kaylyu\Alipay\Kernel\Exceptions\InvalidArgumentException; |
6
|
|
|
use Kaylyu\Alipay\Kernel\Support\Arr; |
7
|
|
|
use Kaylyu\Alipay\Kernel\Support\Str; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait Attributes. |
12
|
|
|
*/ |
13
|
|
|
trait HasAttributes |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $attributes = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $snakeable = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set Attributes. |
27
|
|
|
* |
28
|
|
|
* @param array $attributes |
29
|
|
|
* |
30
|
|
|
* @return $this |
31
|
|
|
*/ |
32
|
|
|
public function setAttributes(array $attributes = []) |
33
|
|
|
{ |
34
|
|
|
$this->attributes = $attributes; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set attribute. |
41
|
|
|
* |
42
|
|
|
* @param string $attribute |
43
|
|
|
* @param string $value |
44
|
|
|
* |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function setAttribute($attribute, $value) |
48
|
|
|
{ |
49
|
|
|
Arr::set($this->attributes, $attribute, $value); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get attribute. |
56
|
|
|
* |
57
|
|
|
* @param string $attribute |
58
|
|
|
* @param mixed $default |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
public function getAttribute($attribute, $default = null) |
63
|
|
|
{ |
64
|
|
|
return Arr::get($this->attributes, $attribute, $default); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $attribute |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function isRequired($attribute) |
73
|
|
|
{ |
74
|
|
|
return in_array($attribute, $this->getRequired(), true); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array|mixed |
79
|
|
|
*/ |
80
|
|
|
public function getRequired() |
81
|
|
|
{ |
82
|
|
|
return property_exists($this, 'required') ? $this->required : []; |
|
|
|
|
83
|
|
|
} |
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
|
|
|
|
97
|
|
|
$this->setAttribute($attribute, $value); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Override parent set() method. |
104
|
|
|
* |
105
|
|
|
* @param string $attribute |
106
|
|
|
* @param mixed $value |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function set($attribute, $value) |
111
|
|
|
{ |
112
|
|
|
$this->setAttribute($attribute, $value); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Override parent get() method. |
119
|
|
|
* |
120
|
|
|
* @param string $attribute |
121
|
|
|
* @param mixed $default |
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
public function get($attribute, $default = null) |
126
|
|
|
{ |
127
|
|
|
return $this->getAttribute($attribute, $default); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $key |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public function has(string $key) |
136
|
|
|
{ |
137
|
|
|
return Arr::has($this->attributes, $key); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $attributes |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function merge(array $attributes) |
146
|
|
|
{ |
147
|
|
|
$this->attributes = array_merge($this->attributes, $attributes); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param array|string $keys |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
public function only($keys) |
158
|
|
|
{ |
159
|
|
|
return Arr::only($this->attributes, $keys); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return all items. |
164
|
|
|
* |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
|
|
public function all() |
168
|
|
|
{ |
169
|
|
|
$this->checkRequiredAttributes(); |
170
|
|
|
|
171
|
|
|
return $this->attributes; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Magic call. |
176
|
|
|
* |
177
|
|
|
* @param string $method |
178
|
|
|
* @param array $args |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function __call($method, $args) |
183
|
|
|
{ |
184
|
|
|
if (0 === stripos($method, 'with')) { |
185
|
|
|
return $this->with(substr($method, 4), array_shift($args)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
throw new \BadMethodCallException(sprintf('Method "%s" does not exists.', $method)); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Magic get. |
193
|
|
|
* |
194
|
|
|
* @param string $property |
195
|
|
|
* |
196
|
|
|
* @return mixed |
197
|
|
|
*/ |
198
|
|
|
public function __get($property) |
199
|
|
|
{ |
200
|
|
|
return $this->get($property); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Magic set. |
205
|
|
|
* |
206
|
|
|
* @param string $property |
207
|
|
|
* @param mixed $value |
208
|
|
|
* |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
|
|
public function __set($property, $value) |
212
|
|
|
{ |
213
|
|
|
return $this->with($property, $value); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Whether or not an data exists by key. |
218
|
|
|
* |
219
|
|
|
* @param string $key |
220
|
|
|
* |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function __isset($key) |
224
|
|
|
{ |
225
|
|
|
return isset($this->attributes[$key]); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Check required attributes. |
230
|
|
|
* |
231
|
|
|
* @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidArgumentException |
232
|
|
|
*/ |
233
|
|
|
protected function checkRequiredAttributes() |
234
|
|
|
{ |
235
|
|
|
foreach ($this->getRequired() as $attribute) { |
236
|
|
|
if (is_null($this->get($attribute))) { |
237
|
|
|
throw new InvalidArgumentException(sprintf('"%s" cannot be empty.', $attribute)); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.