Config::__unset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Signifly\Configurable;
4
5
use ArrayAccess;
6
use Countable;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
11
class Config implements ArrayAccess, Countable
12
{
13
    /**
14
     * The config db key.
15
     *
16
     * @var string|null
17
     */
18
    protected $configKey;
19
20
    /**
21
     * The config data.
22
     *
23
     * @var array
24
     */
25
    protected $data;
26
27
    /**
28
     * The Model instance.
29
     *
30
     * @var \Illuminate\Database\Eloquent\Model
31
     */
32
    protected $model;
33
34
    /**
35
     * Create a new Config instance.
36
     *
37
     * @param string $configKey
38
     */
39
    public function __construct(Model $model, $configKey = null)
40
    {
41
        $this->model = $model;
42
43
        $this->configKey = $configKey;
44
45
        $this->data = $this->getRawData();
46
    }
47
48
    /**
49
     * Get an attribute from config.
50
     *
51
     * @param  string $key
52
     * @param  mixed $default
53
     * @return mixed
54
     */
55
    public function get(string $key, $default = null)
56
    {
57
        return Arr::get($this->data, $key, $default);
58
    }
59
60
    /**
61
     * Determine if an attribute exists in config.
62
     *
63
     * @param  string  $key
64
     * @return bool
65
     */
66
    public function has(string $key)
67
    {
68
        return Arr::has($this->data, $key);
69
    }
70
71
    /**
72
     * Set an attribute in config.
73
     *
74
     * @param string $key
75
     * @param bool $value
76
     * @return bool|null
77
     */
78
    public function set(string $key, $value)
79
    {
80
        Arr::set($this->data, $key, $value);
81
82
        $this->model->{$this->getConfigKey()} = $this->data;
83
    }
84
85
    /**
86
     * Remove an attribute from config.
87
     *
88
     * @param  string $key
89
     * @return Config
90
     */
91
    public function remove(string $key)
92
    {
93
        $this->model->{$this->getConfigKey()} = Arr::except($this->data, $key);
94
95
        return $this;
96
    }
97
98
    /**
99
     * Get all attributes from config.
100
     *
101
     * @return array
102
     */
103
    public function all()
104
    {
105
        return $this->getRawData();
106
    }
107
108
    /**
109
     * Count attributes in config.
110
     *
111
     * @return int
112
     */
113
    public function count()
114
    {
115
        return count($this->data);
116
    }
117
118
    /**
119
     * Get a specific attribute as a collection.
120
     *
121
     * @param  string $key
122
     * @return \Illuminate\Support\Collection
123
     */
124
    public function collect(string $key)
125
    {
126
        return new Collection($this->get($key));
127
    }
128
129
    /**
130
     * Get the config key.
131
     *
132
     * @return string
133
     */
134
    protected function getConfigKey()
135
    {
136
        return $this->configKey ?? $this->model->getConfigKey();
137
    }
138
139
    /**
140
     * Get the raw data from the model.
141
     *
142
     * @return array
143
     */
144
    protected function getRawData()
145
    {
146
        return json_decode($this->model->getAttributes()[$this->getConfigKey()] ?? '{}', true);
147
    }
148
149
    /**
150
     * Determine if the given attribute exists.
151
     *
152
     * @param  string $offset
153
     * @return bool
154
     */
155
    public function offsetExists($offset)
156
    {
157
        return $this->has($offset);
158
    }
159
160
    /**
161
     * Get the value for a given offset.
162
     *
163
     * @param  string $offset
164
     * @return mixed
165
     */
166
    public function offsetGet($offset)
167
    {
168
        return $this->$offset;
169
    }
170
171
    /**
172
     * Set the value for a given offset.
173
     *
174
     * @param  string $offset
175
     * @param  mixed  $value
176
     * @return void
177
     */
178
    public function offsetSet($offset, $value)
179
    {
180
        $this->{$offset} = $value;
181
    }
182
183
    /**
184
     * Unset the value for a given offset.
185
     *
186
     * @param  string $offset
187
     * @return void
188
     */
189
    public function offsetUnset($offset)
190
    {
191
        $this->remove($offset);
192
    }
193
194
    /**
195
     * Get an attribute from config.
196
     *
197
     * @param  string $key
198
     * @return mixed
199
     */
200
    public function __get($key)
201
    {
202
        return $this->get($key);
203
    }
204
205
    /**
206
     * Determine if an attribute exists in config.
207
     *
208
     * @param  string  $key
209
     * @return bool
210
     */
211
    public function __isset($key)
212
    {
213
        return $this->has($key);
214
    }
215
216
    /**
217
     * Set an attribute in config.
218
     *
219
     * @param string $key
220
     * @param bool $value
221
     */
222
    public function __set($key, $value)
223
    {
224
        return $this->set($key, $value);
225
    }
226
227
    /**
228
     * Remove an attribute from config.
229
     *
230
     * @param string $key
231
     */
232
    public function __unset($key)
233
    {
234
        return $this->remove($key);
235
    }
236
}
237