1 | <?php namespace Arcanedev\Settings\Bases; |
||
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 | 18 | public function get($key, $default = null) |
|
51 | { |
||
52 | 18 | $this->checkLoaded(); |
|
53 | |||
54 | 15 | 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 | 24 | public function has($key) |
|
65 | { |
||
66 | 24 | $this->checkLoaded(); |
|
67 | |||
68 | 24 | 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 | 69 | public function set($key, $value = null) |
|
78 | { |
||
79 | 69 | $this->checkLoaded(); |
|
80 | 69 | $this->unsaved = true; |
|
81 | |||
82 | 69 | 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 | 66 | Arr::set($this->data, $key, $value); |
|
88 | } |
||
89 | 69 | } |
|
90 | |||
91 | /** |
||
92 | * Unset a key in the settings data. |
||
93 | * |
||
94 | * @param string $key |
||
95 | */ |
||
96 | 24 | public function forget($key) |
|
97 | { |
||
98 | 24 | $this->unsaved = true; |
|
99 | |||
100 | 24 | if ($this->has($key)) { |
|
101 | 24 | Arr::forget($this->data, $key); |
|
102 | 24 | } |
|
103 | 24 | } |
|
104 | |||
105 | /** |
||
106 | * Unset all keys in the settings data. |
||
107 | */ |
||
108 | 12 | public function reset() |
|
109 | { |
||
110 | 12 | $this->unsaved = true; |
|
111 | 12 | $this->data = []; |
|
112 | 12 | } |
|
113 | |||
114 | /** |
||
115 | * Get all settings data. |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | 63 | public function all() |
|
120 | { |
||
121 | 63 | $this->checkLoaded(); |
|
122 | |||
123 | 63 | return $this->data; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Save any changes done to the settings data. |
||
128 | */ |
||
129 | 39 | public function save() |
|
130 | { |
||
131 | 39 | if ( ! $this->unsaved) { |
|
132 | 9 | return; |
|
133 | } |
||
134 | |||
135 | 36 | $this->write($this->data); |
|
136 | 36 | $this->unsaved = false; |
|
137 | 36 | } |
|
138 | |||
139 | /* ------------------------------------------------------------------------------------------------ |
||
140 | | Other Functions |
||
141 | | ------------------------------------------------------------------------------------------------ |
||
142 | */ |
||
143 | /** |
||
144 | * Check if the settings data has been loaded. |
||
145 | */ |
||
146 | 93 | protected function checkLoaded() |
|
147 | { |
||
148 | 93 | if ( ! $this->loaded) { |
|
149 | 93 | $this->data = $this->read(); |
|
150 | 90 | $this->loaded = true; |
|
151 | 90 | } |
|
152 | 90 | } |
|
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 |