-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Settings resources - Settings Helper class
- Loading branch information
Zlimon
committed
Sep 11, 2021
1 parent
614c81e
commit b7f4a17
Showing
16 changed files
with
624 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use App\Setting; | ||
|
||
class SettingHelper | ||
{ | ||
public static function getSetting($key, $default = null) | ||
{ | ||
if (is_null($key)) { | ||
return new Setting; | ||
} | ||
|
||
if (is_array($key)) { | ||
return Setting::set($key[0], $key[1]); | ||
} | ||
|
||
$value = Setting::get($key); | ||
|
||
return is_null($value) ? value($default) : $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Admin; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Setting; | ||
use Illuminate\Http\Request; | ||
|
||
class SettingsController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index() | ||
{ | ||
return view('admin.setting.index'); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function store(Request $request) | ||
{ | ||
$rules = Setting::getValidationRules(); | ||
$data = $this->validate($request, $rules); | ||
|
||
$validSettings = array_keys($rules); | ||
|
||
foreach ($data as $key => $val) { | ||
if (in_array($key, $validSettings)) { | ||
Setting::set($key, $val, Setting::getDataType($key)); | ||
} | ||
} | ||
|
||
return view('admin.setting.index'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class Setting extends Model | ||
{ | ||
use HasFactory; | ||
|
||
/** | ||
* The attributes that aren't mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $guarded = []; | ||
|
||
/** | ||
* Get a settings value | ||
* | ||
* @param $key | ||
* @param null $default | ||
* @return bool|int|mixed | ||
*/ | ||
public static function get($key, $default = null) | ||
{ | ||
if (self::has($key)) { | ||
$setting = self::getAllSettings()->where('key', $key)->first(); | ||
return self::castValue($setting->value, $setting->type); | ||
} | ||
|
||
return self::getDefaultValue($key, $default); | ||
} | ||
|
||
/** | ||
* Add a settings value | ||
* | ||
* @param $key | ||
* @param $value | ||
* @param string $type | ||
* @return bool | ||
*/ | ||
public static function add($key, $value, $type = 'string') | ||
{ | ||
if (self::has($key)) { | ||
return self::set($key, $value, $type); | ||
} | ||
|
||
return self::create(['key' => $key, 'value' => $value, 'type' => $type]) ? $value : false; | ||
} | ||
|
||
/** | ||
* Set a value for setting | ||
* | ||
* @param $key | ||
* @param $value | ||
* @param string $type | ||
* @return bool | ||
*/ | ||
public static function set($key, $value, $type = 'string') | ||
{ | ||
if ($setting = self::getAllSettings()->where('key', $key)->first()) { | ||
return $setting->update( | ||
[ | ||
'key' => $key, | ||
'value' => $value, | ||
'type' => $type | ||
] | ||
) ? $value : false; | ||
} | ||
|
||
return self::add($key, $value, $type); | ||
} | ||
|
||
/** | ||
* Remove a setting | ||
* | ||
* @param $key | ||
* @return bool | ||
*/ | ||
public static function remove($key) | ||
{ | ||
if (self::has($key)) { | ||
return self::whereName($key)->delete(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Check if setting exists | ||
* | ||
* @param $key | ||
* @return bool | ||
*/ | ||
public static function has($key) | ||
{ | ||
return (boolean)self::getAllSettings()->whereStrict('key', $key)->count(); | ||
} | ||
|
||
/** | ||
* Get the validation rules for setting fields | ||
* | ||
* @return array | ||
*/ | ||
public static function getValidationRules() | ||
{ | ||
return self::getDefinedSettingFields()->pluck('rules', 'key') | ||
->reject( | ||
function ($value) { | ||
return is_null($value); | ||
} | ||
)->toArray(); | ||
} | ||
|
||
/** | ||
* Get the data type of a setting | ||
* | ||
* @param $field | ||
* @return mixed | ||
*/ | ||
public static function getDataType($field) | ||
{ | ||
$type = self::getDefinedSettingFields() | ||
->pluck('data', 'key') | ||
->get($field); | ||
|
||
return is_null($type) ? 'string' : $type; | ||
} | ||
|
||
/** | ||
* Get default value for a setting | ||
* | ||
* @param $field | ||
* @return mixed | ||
*/ | ||
public static function getDefaultValueForField($field) | ||
{ | ||
return self::getDefinedSettingFields() | ||
->pluck('value', 'key') | ||
->get($field); | ||
} | ||
|
||
/** | ||
* Get default value from config if no value passed | ||
* | ||
* @param $key | ||
* @param $default | ||
* @return mixed | ||
*/ | ||
private static function getDefaultValue($key, $default) | ||
{ | ||
return is_null($default) ? self::getDefaultValueForField($key) : $default; | ||
} | ||
|
||
/** | ||
* Get all the settings fields from config | ||
* | ||
* @return Collection | ||
*/ | ||
private static function getDefinedSettingFields() | ||
{ | ||
return collect(config('settings'))->pluck('elements')->flatten(1); | ||
} | ||
|
||
/** | ||
* caste value into respective type | ||
* | ||
* @param $value | ||
* @param $castTo | ||
* @return bool|int | ||
*/ | ||
private static function castValue($value, $castTo) | ||
{ | ||
switch ($castTo) { | ||
case 'int': | ||
case 'integer': | ||
return intval($value); | ||
break; | ||
|
||
case 'bool': | ||
case 'boolean': | ||
return boolval($value); | ||
break; | ||
|
||
default: | ||
return $value; | ||
} | ||
} | ||
|
||
/** | ||
* Get all the settings | ||
* | ||
* @return mixed | ||
*/ | ||
public static function getAllSettings() | ||
{ | ||
return self::all(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.