|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of ibrand/setting. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) iBrand <https://www.ibrand.cc> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace iBrand\Component\Setting\Repositories; |
|
13
|
|
|
|
|
14
|
|
|
use iBrand\Component\Setting\Models\SystemSetting; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class EloquentSetting |
|
18
|
|
|
* @package iBrand\Component\Setting\Repositories |
|
19
|
|
|
*/ |
|
20
|
|
|
class EloquentSetting implements SettingInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var SystemSetting |
|
24
|
|
|
*/ |
|
25
|
|
|
private $model; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* EloquentSetting constructor. |
|
29
|
|
|
* @param SystemSetting $model |
|
30
|
|
|
*/ |
|
31
|
7 |
|
public function __construct(SystemSetting $model) |
|
32
|
|
|
{ |
|
33
|
7 |
|
$this->model = $model; |
|
34
|
7 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $settings |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
6 |
|
public function setSetting(array $settings) |
|
41
|
|
|
{ |
|
42
|
6 |
|
if (count($settings) <= 0) { |
|
43
|
2 |
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
6 |
|
foreach ($settings as $key => $val) { |
|
47
|
6 |
|
$var = $this->model->where('key', $key)->first(); |
|
|
|
|
|
|
48
|
6 |
|
if ($var) { |
|
49
|
1 |
|
$var->value = $val; |
|
50
|
1 |
|
$var->save(); |
|
51
|
|
|
} else { |
|
52
|
6 |
|
$var = $this->model->create(['key' => $key, 'value' => $val]); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
6 |
|
return $var; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $key |
|
61
|
|
|
* @param null $default |
|
62
|
|
|
* @return bool|mixed|string |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function getSetting($key, $default = null) |
|
65
|
|
|
{ |
|
66
|
1 |
|
$value = $this->model->where('key', $key)->get(['value'])->first(); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
1 |
|
if (!is_null($value)) { |
|
69
|
1 |
|
return $value->value; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
if (!is_null($default)) { |
|
73
|
1 |
|
return $default; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
return ''; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
4 |
|
public function allToArray() |
|
83
|
|
|
{ |
|
84
|
4 |
|
$collection = $this->model->all(); |
|
85
|
4 |
|
$keyed = $collection->mapWithKeys(function ($item) { |
|
86
|
4 |
|
return [$item->key => $item->value]; |
|
87
|
4 |
|
}); |
|
88
|
|
|
|
|
89
|
4 |
|
return $keyed->toArray(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: