1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sofa\Eloquence\Metable; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Sofa\Eloquence\Contracts\AttributeBag as AttributeBagContract; |
8
|
|
|
|
9
|
|
|
class AttributeBag extends Collection implements AttributeBagContract |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Create new AttributeBag. |
13
|
|
|
* |
14
|
|
|
* @param array $attributes |
15
|
|
|
*/ |
16
|
|
|
public function __construct($attributes = []) |
17
|
|
|
{ |
18
|
|
|
foreach ($attributes as $attribute) { |
19
|
|
|
$this->add($attribute); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Add or update attribute. |
25
|
|
|
* |
26
|
|
|
* @param \Sofa\Eloquence\Metable\Attribute|string $key |
27
|
|
|
* @param mixed $value |
28
|
|
|
* @return $this |
29
|
|
|
*/ |
30
|
|
|
public function set($key, $value = null) |
31
|
|
|
{ |
32
|
|
|
if ($key instanceof Attribute) { |
33
|
|
|
return $this->setInstance($key); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($this->has($key)) { |
37
|
|
|
$this->update($key, $value); |
38
|
|
|
} else { |
39
|
|
|
$this->items[$key] = $this->newAttribute($key, $value); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set attribute. |
47
|
|
|
* |
48
|
|
|
* @param \Sofa\Eloquence\Metable\Attribute $attribute |
49
|
|
|
*/ |
50
|
|
|
protected function setInstance(Attribute $attribute) |
51
|
|
|
{ |
52
|
|
|
if ($this->has($attribute->getMetaKey())) { |
53
|
|
|
$this->update($attribute); |
54
|
|
|
} else { |
55
|
|
|
$this->items[$attribute->getMetaKey()] = $attribute; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set attribute. |
63
|
|
|
* |
64
|
|
|
* @param \Sofa\Eloquence\Metable\Attribute $attribute |
65
|
|
|
*/ |
66
|
|
|
public function add($attribute) |
67
|
|
|
{ |
68
|
|
|
return $this->addInstance($attribute); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set attribute. |
73
|
|
|
* |
74
|
|
|
* @param \Sofa\Eloquence\Metable\Attribute $attribute |
75
|
|
|
*/ |
76
|
|
|
protected function addInstance(Attribute $attribute) |
77
|
|
|
{ |
78
|
|
|
return $this->set($attribute); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Update existing attribute. |
83
|
|
|
* |
84
|
|
|
* @param \Sofa\Eloquence\Metable\Attribute|string $key |
85
|
|
|
* @param mixed $value |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
protected function update($key, $value = null) |
89
|
|
|
{ |
90
|
|
|
if ($key instanceof Attribute) { |
91
|
|
|
$value = $key->getValue(); |
92
|
|
|
$key = $key->getMetaKey(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->get($key)->setValue($value); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* New attribute instance. |
102
|
|
|
* |
103
|
|
|
* @param string $key |
104
|
|
|
* @param mixed $value |
105
|
|
|
* @return \Sofa\Eloquence\Metable\Attribute |
106
|
|
|
*/ |
107
|
|
|
protected function newAttribute($key, $value) |
108
|
|
|
{ |
109
|
|
|
return new Attribute($key, $value); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get attribute value. |
114
|
|
|
* |
115
|
|
|
* @param string $key |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function getValue($key) |
119
|
|
|
{ |
120
|
|
|
if ($attribute = $this->get($key)) { |
121
|
|
|
return $attribute->getValue(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get collection as key-value array. |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
public function toArray() |
131
|
|
|
{ |
132
|
|
|
return array_filter(array_map(function ($attribute) { |
133
|
|
|
return $attribute->getValue(); |
134
|
|
|
}, $this->items)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Unset attribute. |
139
|
|
|
* |
140
|
|
|
* @param string $key |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function forget($key) |
144
|
|
|
{ |
145
|
|
|
if ($attribute = $this->get($key)) { |
146
|
|
|
$attribute->setValue(null); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set attribute. |
154
|
|
|
* |
155
|
|
|
* @param string $key |
156
|
|
|
* @param mixed $value |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
public function offsetSet($key, $value) |
160
|
|
|
{ |
161
|
|
|
$this->set($key, $value); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set attribute to null. |
166
|
|
|
* |
167
|
|
|
* @param string $key |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function offsetUnset($key) |
171
|
|
|
{ |
172
|
|
|
$this->forget($key); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Handle dynamic properties. |
177
|
|
|
* |
178
|
|
|
* @param string $key |
179
|
|
|
* @param mixed $value |
180
|
|
|
*/ |
181
|
|
|
public function __set($key, $value) |
182
|
|
|
{ |
183
|
|
|
$this->set($key, $value); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Handle dynamic properties. |
188
|
|
|
* |
189
|
|
|
* @param string $key |
190
|
|
|
* @return mixed |
191
|
|
|
*/ |
192
|
|
|
public function __get($key) |
193
|
|
|
{ |
194
|
|
|
return $this->getValue($key); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Handle isset calls. |
199
|
|
|
* |
200
|
|
|
* @param string $key |
201
|
|
|
* @return boolean |
202
|
|
|
*/ |
203
|
|
|
public function __isset($key) |
204
|
|
|
{ |
205
|
|
|
return (bool) $this->get($key); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Handle unset calls. |
210
|
|
|
* |
211
|
|
|
* @param string $key |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
|
public function __unset($key) |
215
|
|
|
{ |
216
|
|
|
$this->forget($key); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Create copy of the attribute bag. |
221
|
|
|
* |
222
|
|
|
* @return static |
223
|
|
|
*/ |
224
|
|
|
public function replicate($except = null) |
225
|
|
|
{ |
226
|
|
|
$except = $except ? array_combine($except, $except) : []; |
227
|
|
|
|
228
|
|
|
$attributes = []; |
229
|
|
|
|
230
|
|
|
foreach (array_diff_key($this->items, $except) as $attribute) { |
231
|
|
|
$attributes[] = $attribute->replicate(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return new static($attributes); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|