1
|
|
|
<?php namespace Arcanedev\Settings\Bases; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Settings\Utilities\Arr; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Store |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\Settings\Bases |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
abstract class Store |
12
|
|
|
{ |
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
14
|
|
|
| Properties |
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
16
|
|
|
*/ |
17
|
|
|
/** |
18
|
|
|
* The settings data. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $data = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Whether the store has changed since it was last loaded. |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $unsaved = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Whether the settings data are loaded. |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $loaded = false; |
37
|
|
|
|
38
|
|
|
/* ------------------------------------------------------------------------------------------------ |
39
|
|
|
| Main Functions |
40
|
|
|
| ------------------------------------------------------------------------------------------------ |
41
|
|
|
*/ |
42
|
|
|
/** |
43
|
|
|
* Get a specific key from the settings data. |
44
|
|
|
* |
45
|
|
|
* @param string|array $key |
46
|
|
|
* @param mixed $default Optional default value. |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
15 |
|
public function get($key, $default = null) |
51
|
|
|
{ |
52
|
15 |
|
$this->checkLoaded(); |
53
|
|
|
|
54
|
12 |
|
return Arr::get($this->data, $key, $default); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Determine if a key exists in the settings data. |
59
|
|
|
* |
60
|
|
|
* @param string $key |
61
|
|
|
* |
62
|
|
|
* @return boolean |
63
|
|
|
*/ |
64
|
18 |
|
public function has($key) |
65
|
|
|
{ |
66
|
18 |
|
$this->checkLoaded(); |
67
|
|
|
|
68
|
18 |
|
return Arr::has($this->data, $key); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set a specific key to a value in the settings data. |
73
|
|
|
* |
74
|
|
|
* @param string|array $key Key string or associative array of key => value |
75
|
|
|
* @param mixed $value Optional only if the first argument is an array |
76
|
|
|
*/ |
77
|
48 |
|
public function set($key, $value = null) |
78
|
|
|
{ |
79
|
48 |
|
$this->checkLoaded(); |
80
|
48 |
|
$this->unsaved = true; |
81
|
|
|
|
82
|
48 |
|
if (is_array($key)) { |
83
|
3 |
|
foreach ($key as $k => $v) { |
84
|
3 |
|
Arr::set($this->data, $k, $v); |
85
|
3 |
|
} |
86
|
3 |
|
} else { |
87
|
45 |
|
Arr::set($this->data, $key, $value); |
88
|
|
|
} |
89
|
48 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Unset a key in the settings data. |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
*/ |
96
|
18 |
|
public function forget($key) |
97
|
|
|
{ |
98
|
18 |
|
$this->unsaved = true; |
99
|
|
|
|
100
|
18 |
|
if ($this->has($key)) { |
101
|
18 |
|
Arr::forget($this->data, $key); |
102
|
18 |
|
} |
103
|
18 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Unset all keys in the settings data. |
107
|
|
|
*/ |
108
|
9 |
|
public function reset() |
109
|
|
|
{ |
110
|
9 |
|
$this->unsaved = true; |
111
|
9 |
|
$this->data = []; |
112
|
9 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get all settings data. |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
45 |
|
public function all() |
120
|
|
|
{ |
121
|
45 |
|
$this->checkLoaded(); |
122
|
|
|
|
123
|
45 |
|
return $this->data; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Save any changes done to the settings data. |
128
|
|
|
*/ |
129
|
15 |
|
public function save() |
130
|
|
|
{ |
131
|
15 |
|
if ( ! $this->unsaved) { |
132
|
3 |
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
15 |
|
$this->write($this->data); |
136
|
15 |
|
$this->unsaved = false; |
137
|
15 |
|
} |
138
|
|
|
|
139
|
|
|
/* ------------------------------------------------------------------------------------------------ |
140
|
|
|
| Other Functions |
141
|
|
|
| ------------------------------------------------------------------------------------------------ |
142
|
|
|
*/ |
143
|
|
|
/** |
144
|
|
|
* Check if the settings data has been loaded. |
145
|
|
|
*/ |
146
|
66 |
|
protected function checkLoaded() |
147
|
|
|
{ |
148
|
66 |
|
if ( ! $this->loaded) { |
149
|
66 |
|
$this->data = $this->read(); |
150
|
63 |
|
$this->loaded = true; |
151
|
63 |
|
} |
152
|
63 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Read the data from the store. |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
abstract protected function read(); |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Write the data into the store. |
163
|
|
|
* |
164
|
|
|
* @param array $data |
165
|
|
|
*/ |
166
|
|
|
abstract protected function write(array $data); |
167
|
|
|
} |
168
|
|
|
|