2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-07-07 10:45:47 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2020-11-14 16:57:25 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
2017-12-09 18:34:27 +00:00
|
|
|
/**
|
|
|
|
* @property string $key
|
2021-06-05 10:47:56 +00:00
|
|
|
* @property mixed $value
|
2019-08-05 10:57:36 +00:00
|
|
|
*
|
2019-08-05 10:56:48 +00:00
|
|
|
* @method static self find(string $key)
|
|
|
|
* @method static self updateOrCreate(array $where, array $params)
|
2017-12-09 18:34:27 +00:00
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
class Setting extends Model
|
|
|
|
{
|
2020-11-14 16:57:25 +00:00
|
|
|
use HasFactory;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
protected $primaryKey = 'key';
|
2015-12-13 04:42:28 +00:00
|
|
|
public $timestamps = false;
|
|
|
|
protected $guarded = [];
|
|
|
|
|
2022-07-07 10:45:47 +00:00
|
|
|
public static function get(string $key): mixed
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-07-07 10:45:47 +00:00
|
|
|
return self::find($key)?->value;
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a setting (no pun) value.
|
|
|
|
*
|
2022-07-07 10:45:47 +00:00
|
|
|
* @param array|string $key the key of the setting, or an associative array of settings,
|
2020-09-06 21:20:42 +00:00
|
|
|
* in which case $value will be discarded
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2022-07-07 10:45:47 +00:00
|
|
|
public static function set(array|string $key, $value = null): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
if (is_array($key)) {
|
|
|
|
foreach ($key as $k => $v) {
|
|
|
|
self::set($k, $v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::updateOrCreate(compact('key'), compact('value'));
|
|
|
|
}
|
|
|
|
|
2022-07-07 10:45:47 +00:00
|
|
|
protected function value(): Attribute
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-07-07 10:45:47 +00:00
|
|
|
return new Attribute(
|
|
|
|
get: static fn ($value) => unserialize($value),
|
|
|
|
set: static fn ($value) => serialize($value)
|
|
|
|
);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|