1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Traits\HasAttributes; |
4
|
|
|
|
5
|
|
|
use Nip\Utility\Str; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait HasAttributesRecordTrait |
9
|
|
|
* @package Nip\Records\Traits\HasAttributes |
10
|
|
|
*/ |
11
|
|
|
trait HasAttributesRecordTrait |
12
|
|
|
{ |
13
|
|
|
protected $attributes = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param $name |
17
|
|
|
* @return mixed |
18
|
|
|
*/ |
19
|
23 |
|
public function __get($name) |
20
|
|
|
{ |
21
|
23 |
|
return $this->getAttribute($name); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $name |
26
|
|
|
* @param $value |
27
|
|
|
*/ |
28
|
21 |
|
public function __set($name, $value) |
29
|
|
|
{ |
30
|
21 |
|
$this->setAttribute($name, $value); |
31
|
21 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $key |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
2 |
|
public function __isset($key) |
38
|
|
|
{ |
39
|
2 |
|
return $this->offsetExists($key); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $key |
44
|
|
|
*/ |
45
|
|
|
public function __unset($key) |
46
|
|
|
{ |
47
|
|
|
$this->offsetUnset($key); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Determine if the given attribute exists. |
52
|
|
|
* |
53
|
|
|
* @param mixed $offset |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
2 |
|
public function offsetExists($offset) |
57
|
|
|
{ |
58
|
2 |
|
return !is_null($this->getAttribute($offset)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the value for a given offset. |
63
|
|
|
* |
64
|
|
|
* @param mixed $offset |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function offsetGet($offset) |
68
|
|
|
{ |
69
|
|
|
return $this->getAttribute($offset); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set the value for a given offset. |
74
|
|
|
* |
75
|
|
|
* @param mixed $offset |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function offsetSet($offset, $value) |
80
|
|
|
{ |
81
|
|
|
$this->setAttribute($offset, $value); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Unset the value for a given offset. |
86
|
|
|
* |
87
|
|
|
* @param mixed $offset |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function offsetUnset($offset) |
91
|
|
|
{ |
92
|
|
|
unset($this->attributes[$offset]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get an attribute from the model. |
97
|
|
|
* |
98
|
|
|
* @param string $key |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
23 |
|
public function getAttribute($key) |
102
|
|
|
{ |
103
|
23 |
|
if (!$key) { |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
23 |
|
if (array_key_exists($key, $this->attributes) |
108
|
|
|
// || array_key_exists($key, $this->casts) |
109
|
23 |
|
|| $this->hasGetMutator($key) |
110
|
|
|
// || $this->isClassCastable($key) |
111
|
|
|
) { |
112
|
20 |
|
return $this->getAttributeValue($key); |
113
|
|
|
} |
114
|
|
|
// return $this->getRelationValue($key); |
115
|
10 |
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get a plain attribute (not a relationship). |
120
|
|
|
* |
121
|
|
|
* @param string $key |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
20 |
|
public function getAttributeValue($key) |
125
|
|
|
{ |
126
|
20 |
|
return $this->transformModelValue($key, $this->getAttributeFromArray($key)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Transform a raw model value using mutators, casts, etc. |
131
|
|
|
* |
132
|
|
|
* @param string $key |
133
|
|
|
* @param mixed $value |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
20 |
|
protected function transformModelValue($key, $value) |
137
|
|
|
{ |
138
|
|
|
// If the attribute has a get mutator, we will call that then return what |
139
|
|
|
// it returns as the value, which is useful for transforming values on |
140
|
|
|
// retrieval from the model to a form that is more useful for usage. |
141
|
20 |
|
if ($this->hasGetMutator($key)) { |
142
|
|
|
return $this->mutateAttribute($key, $value); |
143
|
|
|
} |
144
|
|
|
|
145
|
20 |
|
return $value; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get an attribute from the $attributes array. |
150
|
|
|
* |
151
|
|
|
* @param string $key |
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
20 |
|
protected function getAttributeFromArray($key) |
155
|
|
|
{ |
156
|
20 |
|
return $this->getAttributes()[$key] ?? null; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $key |
161
|
|
|
* @param $value |
162
|
|
|
*/ |
163
|
21 |
|
public function setAttribute($key, $value) |
164
|
|
|
{ |
165
|
21 |
|
if (property_exists($this, $key)) { |
166
|
|
|
$this->{$key} = $value; |
167
|
|
|
return; |
168
|
|
|
} |
169
|
|
|
|
170
|
21 |
|
$method = 'set' . Str::studly($key); |
171
|
21 |
|
if (method_exists($this, $method)) { |
172
|
|
|
$this->$method($value); |
173
|
|
|
return; |
174
|
|
|
} |
175
|
21 |
|
$method .= 'Attribute'; |
176
|
21 |
|
if (method_exists($this, $method)) { |
177
|
|
|
$this->$method($value); |
178
|
|
|
return; |
179
|
|
|
} |
180
|
21 |
|
$this->setDataValue($key, $value); |
181
|
21 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param bool|array $data |
185
|
|
|
*/ |
186
|
4 |
|
public function writeData($data = false) |
187
|
|
|
{ |
188
|
4 |
|
foreach ($data as $key => $value) { |
189
|
4 |
|
$this->__set($key, $value); |
190
|
|
|
} |
191
|
4 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return mixed |
195
|
|
|
*/ |
196
|
20 |
|
public function getAttributes() |
197
|
|
|
{ |
198
|
20 |
|
return $this->attributes; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param $key |
204
|
|
|
* @param $value |
205
|
|
|
*/ |
206
|
21 |
|
protected function setDataValue($key, $value) |
207
|
|
|
{ |
208
|
21 |
|
$this->attributes[$key] = $value; |
209
|
21 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Determine if a get mutator exists for an attribute. |
213
|
|
|
* |
214
|
|
|
* @param string $key |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
23 |
|
public function hasGetMutator($key) |
218
|
|
|
{ |
219
|
23 |
|
return method_exists($this, 'get' . Str::studly($key) . 'Attribute'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the value of an attribute using its mutator. |
224
|
|
|
* |
225
|
|
|
* @param string $key |
226
|
|
|
* @param mixed $value |
227
|
|
|
* @return mixed |
228
|
|
|
*/ |
229
|
|
|
protected function mutateAttribute($key, $value) |
230
|
|
|
{ |
231
|
|
|
return $this->{'get' . Str::studly($key) . 'Attribute'}($value); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|