| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Management; |
| 4 |
use ResponsiveMenu\Collections\OptionsCollection; |
| 5 |
use ResponsiveMenu\Database\Database; |
| 6 |
|
| 7 |
class OptionManager { |
| 8 |
|
| 9 |
private $db; |
| 10 |
private $default_options; |
| 11 |
|
| 12 |
public function __construct(Database $db, $default_options) { |
| 13 |
$this->db = $db; |
| 14 |
$this->default_options = $default_options; |
| 15 |
} |
| 16 |
|
| 17 |
public function all() { |
| 18 |
$options = $this->db->all('responsive_menu'); |
| 19 |
return new OptionsCollection($options); |
| 20 |
} |
| 21 |
|
| 22 |
public function updateOptions(array $options) { |
| 23 |
$updated_options = $this->combineOptions($options); |
| 24 |
foreach($updated_options as $name => $val): |
| 25 |
$val = is_array($val) ? json_encode($val) : $val; |
| 26 |
$val = stripslashes($val); |
| 27 |
$updated_options[$name] = $val; |
| 28 |
$this->db->update('responsive_menu', ['value' => $val], ['name' => $name]); |
| 29 |
endforeach; |
| 30 |
return new OptionsCollection($updated_options); |
| 31 |
} |
| 32 |
|
| 33 |
public function createOptions(array $options) { |
| 34 |
$updated_options = $this->combineOptions($options); |
| 35 |
foreach($options as $name => $val): |
| 36 |
$val = is_array($val) ? json_encode($val) : $val; |
| 37 |
$val = stripslashes($val); |
| 38 |
$updated_options[$name] = $val; |
| 39 |
$this->db->insert('responsive_menu', ['name' => $name, 'value' => $val]); |
| 40 |
endforeach; |
| 41 |
return new OptionsCollection($updated_options); |
| 42 |
} |
| 43 |
|
| 44 |
public function removeOptions(array $options) { |
| 45 |
$updated_options = $this->combineOptions($options); |
| 46 |
foreach($options as $name => $val): |
| 47 |
$val = is_array($val) ? json_encode($val) : $val; |
| 48 |
$val = stripslashes($val); |
| 49 |
$updated_options[$name] = $val; |
| 50 |
unset($updated_options[$name]); |
| 51 |
$this->db->delete('responsive_menu', ['name' => $name]); |
| 52 |
endforeach; |
| 53 |
return new OptionsCollection($updated_options); |
| 54 |
} |
| 55 |
|
| 56 |
public function buildFromArray(array $options) { |
| 57 |
$new_options = $this->combineOptions($options); |
| 58 |
foreach($options as $name => $val): |
| 59 |
$val = is_array($val) ? json_encode($val) : $val; |
| 60 |
$val = stripslashes($val); |
| 61 |
$new_options[$name] = $val; |
| 62 |
endforeach; |
| 63 |
return new OptionsCollection($new_options); |
| 64 |
} |
| 65 |
|
| 66 |
private function combineOptions($new_options) { |
| 67 |
return array_merge($this->default_options, $new_options); |
| 68 |
} |
| 69 |
|
| 70 |
} |