1 | <?php namespace Arcanedev\LaravelSettings\Stores; |
||
13 | class DatabaseStore extends AbstractStore |
||
14 | { |
||
15 | /* ----------------------------------------------------------------- |
||
16 | | Properties |
||
17 | | ----------------------------------------------------------------- |
||
18 | */ |
||
19 | |||
20 | /** |
||
21 | * The eloquent model. |
||
22 | * |
||
23 | * @var \Illuminate\Database\Eloquent\Model |
||
24 | */ |
||
25 | protected $model; |
||
26 | |||
27 | /** |
||
28 | * The key column name to query from. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $keyColumn; |
||
33 | |||
34 | /** |
||
35 | * The value column name to query from. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $valueColumn; |
||
40 | |||
41 | /** |
||
42 | * Any query constraints that should be applied. |
||
43 | * |
||
44 | * @var \Closure|null |
||
45 | */ |
||
46 | protected $queryConstraint; |
||
47 | |||
48 | /** |
||
49 | * Any extra columns that should be added to the rows. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $extraColumns = []; |
||
54 | |||
55 | /* ----------------------------------------------------------------- |
||
56 | | Post-Constructor |
||
57 | | ----------------------------------------------------------------- |
||
58 | */ |
||
59 | |||
60 | /** |
||
61 | * Fire the post options to customize the store. |
||
62 | * |
||
63 | * @param array $options |
||
64 | */ |
||
65 | 36 | protected function postOptions(array $options) |
|
75 | |||
76 | /* ----------------------------------------------------------------- |
||
77 | | Getters & Setters |
||
78 | | ----------------------------------------------------------------- |
||
79 | */ |
||
80 | |||
81 | /** |
||
82 | * Set the db connection to query from. |
||
83 | * |
||
84 | * @param string $name |
||
85 | * |
||
86 | * @return self |
||
87 | */ |
||
88 | 36 | public function setConnection($name) |
|
94 | |||
95 | /** |
||
96 | * Set the table to query from. |
||
97 | * |
||
98 | * @param string $name |
||
99 | * |
||
100 | * @return self |
||
101 | */ |
||
102 | 36 | public function setTable($name) |
|
108 | |||
109 | /** |
||
110 | * Set the key column name to query from. |
||
111 | * |
||
112 | * @param string $name |
||
113 | * |
||
114 | * @return self |
||
115 | */ |
||
116 | 36 | public function setKeyColumn($name) |
|
122 | |||
123 | /** |
||
124 | * Set the value column name to query from. |
||
125 | * |
||
126 | * @param string $name |
||
127 | * |
||
128 | * @return self |
||
129 | */ |
||
130 | 36 | public function setValueColumn($name) |
|
136 | |||
137 | /** |
||
138 | * Set the query constraint. |
||
139 | * |
||
140 | * @param \Closure $callback |
||
141 | * |
||
142 | * @return self |
||
143 | */ |
||
144 | 3 | public function setConstraint(Closure $callback) |
|
152 | |||
153 | /** |
||
154 | * Set extra columns to be added to the rows. |
||
155 | * |
||
156 | * @param array $columns |
||
157 | * |
||
158 | * @return self |
||
159 | */ |
||
160 | 3 | public function setExtraColumns(array $columns) |
|
168 | |||
169 | /* ----------------------------------------------------------------- |
||
170 | | Main Methods |
||
171 | | ----------------------------------------------------------------- |
||
172 | */ |
||
173 | |||
174 | /** |
||
175 | * Unset a key in the settings data. |
||
176 | * |
||
177 | * @param string $key |
||
178 | * |
||
179 | * @return self |
||
180 | */ |
||
181 | 6 | public function forget($key) |
|
182 | { |
||
183 | 6 | parent::forget($key); |
|
184 | |||
185 | // because the database store cannot store empty arrays, remove empty |
||
186 | // arrays to keep data consistent before and after saving |
||
187 | 6 | $segments = explode('.', $key); |
|
188 | 6 | array_pop($segments); |
|
189 | |||
190 | 6 | while ( ! empty($segments)) { |
|
191 | 3 | $segment = implode('.', $segments); |
|
192 | |||
193 | // non-empty array - exit out of the loop |
||
194 | 3 | if ($this->get($segment)) break; |
|
195 | |||
196 | // remove the empty array and move on to the next segment |
||
197 | 3 | $this->forget($segment); |
|
198 | 3 | array_pop($segments); |
|
199 | 1 | } |
|
200 | |||
201 | 6 | return $this; |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Read the data from the store. |
||
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | 30 | protected function read() |
|
215 | |||
216 | /** |
||
217 | * Write the data into the store. |
||
218 | * |
||
219 | * @param array $data |
||
220 | */ |
||
221 | 24 | protected function write(array $data) |
|
229 | |||
230 | /* ----------------------------------------------------------------- |
||
231 | | Other Methods |
||
232 | | ----------------------------------------------------------------- |
||
233 | */ |
||
234 | |||
235 | /** |
||
236 | * Create a new query builder instance. |
||
237 | * |
||
238 | * @param $insert bool |
||
239 | * |
||
240 | * @return \Illuminate\Database\Query\Builder |
||
241 | */ |
||
242 | 30 | protected function newQuery($insert = false) |
|
259 | |||
260 | /** |
||
261 | * Transforms settings data into an array ready to be inserted into the database. |
||
262 | * Call array_dot on a multidimensional array before passing it into this method! |
||
263 | * |
||
264 | * @param array $data |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | 24 | protected function prepareInsertData(array $data) |
|
282 | |||
283 | /** |
||
284 | * Check if the query constraint exists. |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | 30 | protected function hasQueryConstraint() |
|
292 | |||
293 | /** |
||
294 | * Get the changed settings data. |
||
295 | * |
||
296 | * @param array $data |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | 24 | private function getChanges(array $data) |
|
319 | |||
320 | /** |
||
321 | * Sync the updated records. |
||
322 | * |
||
323 | * @param array $updated |
||
324 | */ |
||
325 | 24 | private function syncUpdated(array $updated) |
|
333 | |||
334 | /** |
||
335 | * Sync the inserted records. |
||
336 | * |
||
337 | * @param array $inserted |
||
338 | */ |
||
339 | 24 | private function syncInserted(array $inserted) |
|
347 | |||
348 | /** |
||
349 | * Sync the deleted records. |
||
350 | * |
||
351 | * @param array $deleted |
||
352 | */ |
||
353 | 24 | private function syncDeleted(array $deleted) |
|
359 | } |
||
360 |