1 | <?php |
||||
2 | /** |
||||
3 | * @link https://github.com/lav45/yii2-settings |
||||
4 | * @copyright Copyright (c) 2016 LAV45 |
||||
5 | * @author Aleksey Loban <[email protected]> |
||||
6 | * @license http://opensource.org/licenses/BSD-3-Clause |
||||
7 | */ |
||||
8 | |||||
9 | namespace lav45\settings; |
||||
10 | |||||
11 | use yii\di\Instance; |
||||
12 | use yii\helpers\ArrayHelper; |
||||
13 | |||||
14 | /** |
||||
15 | * Class Model represents settings model |
||||
16 | * @package lav45\settings |
||||
17 | */ |
||||
18 | class Model extends \yii\base\Model |
||||
19 | { |
||||
20 | /** |
||||
21 | * @var Settings|string|array |
||||
22 | */ |
||||
23 | private $settings = 'settings'; |
||||
24 | |||||
25 | /** |
||||
26 | * @inheritdoc |
||||
27 | */ |
||||
28 | 2 | public function init() |
|||
29 | { |
||||
30 | 2 | $this->setAttributes($this->getData(), false); |
|||
31 | } |
||||
32 | |||||
33 | /** |
||||
34 | * @return Settings |
||||
35 | * @throws \yii\base\InvalidConfigException |
||||
36 | */ |
||||
37 | 2 | public function getSettings() |
|||
38 | { |
||||
39 | 2 | if ($this->settings instanceof Settings === false) { |
|||
40 | 2 | $this->settings = Instance::ensure($this->settings, Settings::class); |
|||
41 | } |
||||
42 | 2 | return $this->settings; |
|||
43 | } |
||||
44 | |||||
45 | /** |
||||
46 | * @param Settings|string|array $data |
||||
47 | */ |
||||
48 | public function setSettings($data) |
||||
49 | { |
||||
50 | $this->settings = $data; |
||||
51 | } |
||||
52 | |||||
53 | /** |
||||
54 | * @param bool $runValidation |
||||
55 | * @return bool |
||||
56 | */ |
||||
57 | 2 | public function save($runValidation = true) |
|||
58 | { |
||||
59 | 2 | if ($runValidation === false || $this->validate()) { |
|||
60 | 2 | return $this->setData($this->getSaveAttributes($runValidation)); |
|||
61 | } |
||||
62 | return false; |
||||
63 | } |
||||
64 | |||||
65 | /** |
||||
66 | * @return array|string|integer |
||||
67 | */ |
||||
68 | 2 | protected function getSettingsKey() |
|||
69 | { |
||||
70 | 2 | return [static::class]; |
|||
71 | } |
||||
72 | |||||
73 | /** |
||||
74 | * List of attributes to save |
||||
75 | * @param bool $safeOnly |
||||
76 | * @return string[] |
||||
77 | * @since 1.2.2 |
||||
78 | */ |
||||
79 | 2 | protected function getSaveAttributeList($safeOnly = false) |
|||
80 | { |
||||
81 | 2 | return $safeOnly ? $this->safeAttributes() : $this->attributes(); |
|||
82 | } |
||||
83 | |||||
84 | /** |
||||
85 | * @param bool $safeOnly |
||||
86 | * @return array |
||||
87 | */ |
||||
88 | 2 | protected function getSaveAttributes($safeOnly = false) |
|||
89 | { |
||||
90 | 2 | $data = $this->getAttributes($this->getSaveAttributeList($safeOnly)); |
|||
91 | 2 | return array_filter($data, static function ($val) { |
|||
92 | 2 | return $val !== '' || $val !== [] || $val !== null; |
|||
93 | 2 | }); |
|||
94 | } |
||||
95 | |||||
96 | /** |
||||
97 | * @return array |
||||
98 | */ |
||||
99 | 2 | protected function getData() |
|||
100 | { |
||||
101 | 2 | return $this->getSettings()->get($this->getSettingsKey(), []); |
|||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
102 | } |
||||
103 | |||||
104 | /** |
||||
105 | * @param array $data |
||||
106 | * @return bool |
||||
107 | */ |
||||
108 | 2 | protected function setData($data) |
|||
109 | { |
||||
110 | 2 | return $this->getSettings()->set($this->getSettingsKey(), ArrayHelper::toArray($data)); |
|||
111 | } |
||||
112 | |||||
113 | /** |
||||
114 | * @return bool |
||||
115 | */ |
||||
116 | 1 | public function flush() |
|||
117 | { |
||||
118 | 1 | $this->setAttributes(array_fill_keys($this->getSaveAttributeList(), null)); |
|||
119 | 1 | return $this->getSettings()->delete($this->getSettingsKey()); |
|||
0 ignored issues
–
show
$this->getSettingsKey() of type array<integer,string> is incompatible with the type string expected by parameter $key of lav45\settings\Settings::delete() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
120 | } |
||||
121 | } |
||||
122 |