AttributeBag   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 2
dl 0
loc 241
rs 10
c 0
b 0
f 0

18 Methods

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