| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Repositories; |
| 4 |
use ResponsiveMenu\Models\Option as Option; |
| 5 |
use ResponsiveMenu\Collections\OptionsCollection as OptionsCollection; |
| 6 |
|
| 7 |
class OptionRepository { |
| 8 |
|
| 9 |
protected static $table = 'responsive_menu'; |
| 10 |
|
| 11 |
public function __construct($db, $factory, $defaults) { |
| 12 |
$this->db = $db; |
| 13 |
$this->factory = $factory; |
| 14 |
$this->defaults = $defaults; |
| 15 |
} |
| 16 |
|
| 17 |
public function all() { |
| 18 |
$options = $this->db->all(self::$table); |
| 19 |
$collection = new OptionsCollection; |
| 20 |
foreach($options as $option) |
| 21 |
$collection->add($this->factory->build($option->name, $option->value)); |
| 22 |
return $collection; |
| 23 |
} |
| 24 |
|
| 25 |
public function update(Option $option) { |
| 26 |
$this->db->update(self::$table, |
| 27 |
['value' => $option->getFiltered()], |
| 28 |
['name' => $option->getName()] |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
public function create(Option $option) { |
| 33 |
$arguments['name'] = $option->getName(); |
| 34 |
$arguments['value'] = $option->getFiltered(); |
| 35 |
$arguments['created_at'] = current_time('mysql'); |
| 36 |
$this->db->insert(self::$table, $arguments); |
| 37 |
} |
| 38 |
|
| 39 |
public function remove($name) { |
| 40 |
$this->db->delete(self::$table, $name); |
| 41 |
} |
| 42 |
|
| 43 |
public function buildFromArray(array $array) { |
| 44 |
$collection = new OptionsCollection; |
| 45 |
foreach(array_merge($this->defaults, $array) as $name => $value): |
| 46 |
$option = $this->factory->build($name, $value); |
| 47 |
$option->setValue($option->getFiltered()); |
| 48 |
$collection->add($option); |
| 49 |
endforeach; |
| 50 |
|
| 51 |
return $collection; |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|