2020-03-13 22:10:36 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes;
|
|
|
|
|
|
|
|
class Options
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2020-04-02 10:40:13 +02:00
|
|
|
* Compile both default and user options
|
2020-03-13 22:10:36 +01:00
|
|
|
*
|
2020-04-02 10:40:13 +02:00
|
|
|
* @return Options collection or a signle
|
2020-03-13 22:10:36 +01:00
|
|
|
*/
|
2020-04-02 10:40:13 +02:00
|
|
|
public static function get($option = null)
|
2020-03-13 22:10:36 +01:00
|
|
|
{
|
|
|
|
// Get a collection of user saved options
|
|
|
|
$userOptions = \Illuminate\Support\Facades\DB::table('options')->pluck('value', 'key');
|
|
|
|
|
|
|
|
// We replace patterned string that represent booleans with real booleans
|
|
|
|
$userOptions->transform(function ($item, $key) {
|
|
|
|
if( $item === '{{}}' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if( $item === '{{1}}' ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Merge options from App configuration. It ensures we have a complete options collection with
|
|
|
|
// fallback values for every options
|
|
|
|
$options = collect(config('app.options'))->merge($userOptions);
|
|
|
|
|
2020-04-02 10:40:13 +02:00
|
|
|
return !is_null($option) ? $options[$option] : $options;
|
2020-03-13 22:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set user options
|
|
|
|
*
|
|
|
|
* @param array All options to store
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function store($userOptions)
|
|
|
|
{
|
|
|
|
foreach($userOptions as $opt => $val) {
|
|
|
|
|
|
|
|
// We replace boolean values by a patterned string in order to retrieve
|
|
|
|
// them later (as the Laravel Options package do not support var type)
|
|
|
|
// Not a beatufilly solution but, hey, it works ^_^
|
|
|
|
option([$opt => is_bool($val) ? '{{' . $val . '}}' : $val]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|