| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Services; |
| 4 |
use ResponsiveMenu\Repositories\OptionRepository; |
| 5 |
use ResponsiveMenu\Translation\Translator; |
| 6 |
use ResponsiveMenu\Factories\OptionFactory; |
| 7 |
use ResponsiveMenu\Collections\OptionsCollection; |
| 8 |
use ResponsiveMenu\Filesystem\ScriptsBuilder; |
| 9 |
|
| 10 |
class OptionService { |
| 11 |
|
| 12 |
public function __construct(OptionRepository $repository, OptionFactory $factory, Translator $translator, ScriptsBuilder $builder) { |
| 13 |
$this->repository = $repository; |
| 14 |
$this->factory = $factory; |
| 15 |
$this->translator = $translator; |
| 16 |
$this->builder = $builder; |
| 17 |
} |
| 18 |
|
| 19 |
public function all() { |
| 20 |
return $this->repository->all(); |
| 21 |
} |
| 22 |
|
| 23 |
public function updateOptions(array $options) { |
| 24 |
|
| 25 |
foreach($options as $key => $val) |
| 26 |
$this->repository->update($this->factory->build($key, $val)); |
| 27 |
|
| 28 |
return $this->processAfterSavingOptions(); |
| 29 |
} |
| 30 |
|
| 31 |
public function createOptions(array $options) { |
| 32 |
|
| 33 |
foreach($options as $key => $val) |
| 34 |
$this->repository->create($this->factory->build($key, $val)); |
| 35 |
|
| 36 |
return $this->processAfterSavingOptions(); |
| 37 |
} |
| 38 |
|
| 39 |
private function processAfterSavingOptions() { |
| 40 |
$options = $this->all(); |
| 41 |
$this->translator->saveTranslations($options); |
| 42 |
if($options['external_files'] == 'on') |
| 43 |
$this->builder->build($options); |
| 44 |
return $options; |
| 45 |
} |
| 46 |
|
| 47 |
public function buildFromPostArray(array $post) { |
| 48 |
return $this->repository->buildFromArray($post); |
| 49 |
} |
| 50 |
|
| 51 |
public function combineOptions($default_options, $new_options) { |
| 52 |
return array_merge($default_options, array_filter($new_options, function($value) { |
| 53 |
return ($value !== null && $value !== false && $value !== ''); |
| 54 |
})); |
| 55 |
} |
| 56 |
|
| 57 |
} |
| 58 |
|