88 lines
2.1 KiB
PHP
88 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace kernel\modules\option\service;
|
|
|
|
use kernel\FormModel;
|
|
use kernel\modules\option\models\Option;
|
|
|
|
class OptionService
|
|
{
|
|
|
|
public function create(FormModel $form_model): false|Option
|
|
{
|
|
$model = new Option();
|
|
$model->key = $form_model->getItem('key');
|
|
$model->value = $form_model->getItem('value');
|
|
$model->label = $form_model->getItem('label');
|
|
$model->status = $form_model->getItem('status');
|
|
if ($model->save()) {
|
|
return $model;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function update(FormModel $form_model, Option $option): false|Option
|
|
{
|
|
$option->key = $form_model->getItem('key');
|
|
$option->value = $form_model->getItem('value');
|
|
$option->label = $form_model->getItem('label');
|
|
$option->status = $form_model->getItem('status');
|
|
if ($option->save()) {
|
|
return $option;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function createFromParams(string $key, string $value, string $label): false|Option
|
|
{
|
|
$model = new Option();
|
|
$model->key = $key;
|
|
$model->value = $value;
|
|
$model->label = $label;
|
|
|
|
if ($model->save()) {
|
|
return $model;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param $key
|
|
* @return false|array|string
|
|
*/
|
|
public static function getItem($key): false|array|string
|
|
{
|
|
$item = Option::where("key", $key)->first();
|
|
if ($item){
|
|
return getConst($item->value);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function removeOptionByKey(string $key): bool
|
|
{
|
|
$option = Option::where("key", $key)->first();
|
|
if (!$option){
|
|
return false;
|
|
}
|
|
|
|
$option->delete();
|
|
return true;
|
|
}
|
|
|
|
// public function createOptionArr(): array
|
|
// {
|
|
// foreach (Option::all()->toArray() as $option) {
|
|
// $optionArr[$option['id']] = $option['key'];
|
|
// }
|
|
// if (!empty($optionArr)) {
|
|
// return $optionArr;
|
|
// }
|
|
// return [];
|
|
// }
|
|
|
|
} |