Passed
Pull Request — master (#94)
by Gombos
04:03
created

AbstractSettingsManager::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Glorand\Model\Settings\Managers;
4
5
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
6
use Glorand\Model\Settings\Exceptions\ModelSettingsException;
7
use Glorand\Model\Settings\Traits\HasSettings;
8
use Illuminate\Support\Facades\Validator;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class AbstractSettingsManager
14
 * @package Glorand\Model\Settings\Managers
15
 * @SuppressWarnings (PHPMD.StaticAccess)
16
 */
17
abstract class AbstractSettingsManager implements SettingsManagerContract
18
{
19
    /** @var \Illuminate\Database\Eloquent\Model */
20
    protected $model;
21
22
    /** @var array */
23
    protected $defaultSettings = [];
24
25
    /**
26
     * AbstractSettingsManager constructor.
27
     * @param \Illuminate\Database\Eloquent\Model|HasSettings $model
28 198
     * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
29
     */
30 198
    public function __construct(Model $model)
31 198
    {
32 3
        $this->model = $model;
33
        if (!in_array(HasSettings::class, class_uses_recursive($this->model))) {
34 195
            throw new ModelSettingsException('Wrong model, missing HasSettings trait.');
35
        }
36
    }
37
38
    /**
39 195
     * Check if array is associative and not sequential
40
     * @param array $arr
41 195
     * @return bool
42 195
     */
43 171
    private static function isAssoc(array $arr): bool
44
    {
45
        if ([] === $arr) {
46 186
            return false;
47
        }
48
49
        return array_keys($arr) !== range(0, count($arr) - 1);
50
    }
51
52 12
    /**
53
     * Flatten array with dots for settings package
54 12
     * @param array $array
55
     * @param string $prepend
56
     * @return array
57
     */
58
    public static function dotFlatten(array $array, string $prepend = ''): array
59
    {
60 12
        $results = [];
61
        foreach ($array as $key => $value) {
62 12
            // only re-run if nested array is associative (key-based)
63
            if (is_array($value) && static::isAssoc($value) && !empty($value)) {
64
                $results = array_merge($results, static::dotFlatten($value, $prepend . $key . '.'));
65
            } else {
66
                $results[$prepend . $key] = $value;
67
            }
68
        }
69 12
70
        return $results;
71 12
    }
72
73
    /**
74
     * Get nested merged array with all available keys
75
     * @return array
76
     */
77
    public function all(): array
78
    {
79 48
        return $this->getMultiple();
80
    }
81 48
82
    /**
83
     * Get flat merged array with dot-notation keys
84
     * @return array
85
     */
86
    public function allFlattened(): array
87
    {
88
        $flattenedDefaultSettings = static::dotFlatten($this->model->getDefaultSettings());
0 ignored issues
show
Bug introduced by
It seems like $this->model->getDefaultSettings() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $array of Glorand\Model\Settings\M...gsManager::dotFlatten() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        $flattenedDefaultSettings = static::dotFlatten(/** @scrutinizer ignore-type */ $this->model->getDefaultSettings());
Loading history...
89 12
        $flattenedSettingsValue = static::dotFlatten($this->model->getSettingsValue());
90
91 12
        return array_merge($flattenedDefaultSettings, $flattenedSettingsValue);
92 12
    }
93 12
94
    /**
95
     * @return bool
96 12
     */
97
    public function exist(): bool
98
    {
99
        return count($this->all()) > 0;
100
    }
101
102
    /**
103
     * @return bool
104 24
     */
105
    public function empty(): bool
106 24
    {
107 24
        return count($this->all()) <= 0;
108
    }
109 24
110
    /**
111
     * @param string $path
112
     * @return bool
113
     */
114
    public function has(string $path): bool
115
    {
116
        return Arr::has($this->all(), $path);
117 12
    }
118
119 12
    /**
120
     * @param string|null $path
121
     * @param null $default
122
     * @return array|\ArrayAccess|mixed
123
     */
124
    public function get(string $path = null, $default = null)
125
    {
126 60
        return $path ? Arr::get($this->all(), $path, $default) : $this->all();
127
    }
128 60
129 60
    /**
130
     * @param iterable|null $paths
131 12
     * @param null $default
132 12
     * @return array
133
     */
134
    public function getMultiple(iterable $paths = null, $default = null): array
135 60
    {
136
        $array = [];
137 60
        $allFlattened = $this->allFlattened();
138
        $settingsArray = [];
139
        foreach ($allFlattened as $key => $value) {
140
            Arr::set($settingsArray, $key, $value);
141
        }
142
        if (is_null($paths)) {
143 42
            return $settingsArray;
144
        }
145 42
146
        foreach ($paths as $path) {
147
            Arr::set($array, $path, Arr::get($settingsArray, $path, $default));
148
        }
149
150
        return $array;
151
    }
152 12
153
    /**
154 12
     * @param string $path
155 12
     * @param $value
156 12
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
157
     */
158
    public function set(string $path, $value): SettingsManagerContract
159 12
    {
160
        $settings = $this->all();
161
        Arr::set($settings, $path, $value);
162
163
        return $this->apply($settings);
164
    }
165
166 12
    /**
167
     * @param string $path
168 12
     * @param mixed $value
169 12
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
170 12
     */
171
    public function update(string $path, $value): SettingsManagerContract
172
    {
173 12
        return $this->set($path, $value);
174
    }
175 12
176
    /**
177
     * @param string|null $path
178
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
179
     */
180
    public function delete(string $path = null): SettingsManagerContract
181
    {
182
        if (!$path) {
183
            $settings = [];
184
        } else {
185
            $settings = $this->all();
186
            Arr::forget($settings, $path);
187
        }
188
189
        $this->apply($settings);
190
191
        return $this;
192
    }
193
194
    /**
195
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
196
     */
197
    public function clear(): SettingsManagerContract
198
    {
199
        return $this->delete();
200
    }
201
202
    /**
203
     * @param iterable $values
204
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
205
     */
206
    public function setMultiple(iterable $values): SettingsManagerContract
207
    {
208
        $settings = $this->all();
209
        foreach ($values as $path => $value) {
210
            Arr::set($settings, $path, $value);
211
        }
212
213
        return $this->apply($settings);
214
    }
215
216
    /**
217
     * @param iterable $paths
218
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
219
     */
220
    public function deleteMultiple(iterable $paths): SettingsManagerContract
221
    {
222
        $settings = $this->all();
223
        foreach ($paths as $path) {
224
            Arr::forget($settings, $path);
225
        }
226
227
        $this->apply($settings);
228
229
        return $this;
230
    }
231
232
    /**
233
     * @param  array  $settings
234
     * @throws \Illuminate\Validation\ValidationException
235
     */
236
    protected function validate(array $settings)
237
    {
238
        Validator::make(Arr::wrap($settings), Arr::wrap($this->model->getRules()))->validate();
239
    }
240
}
241