FleetCart/Modules/Setting/Entities/Setting.php

175 lines
3.6 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Modules\Setting\Entities;
use Modules\Support\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Modules\Setting\Events\SettingSaved;
use Modules\Support\Eloquent\Translatable;
2023-12-03 14:07:47 +00:00
use Illuminate\Database\Eloquent\Collection;
2023-06-11 12:14:03 +00:00
class Setting extends Model
{
use Translatable;
/**
* The relations to eager load on every query.
*
* @var array
*/
protected $with = ['translations'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['key', 'is_translatable', 'plain_value'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'is_translatable' => 'boolean',
];
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'saved' => SettingSaved::class,
];
/**
* The attributes that are translatable.
*
* @var array
*/
protected $translatedAttributes = ['value'];
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Get all settings with cache support.
*
2023-12-03 14:07:47 +00:00
* @return Collection
2023-06-11 12:14:03 +00:00
*/
public static function allCached()
{
return Cache::rememberForever(md5('settings.all:' . locale()), function () {
return self::all()->mapWithKeys(function ($setting) {
return [$setting->key => $setting->value];
});
});
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Determine if the given setting key exists.
*
* @param string $key
2023-12-03 14:07:47 +00:00
*
2023-06-11 12:14:03 +00:00
* @return bool
*/
public static function has($key)
{
return static::where('key', $key)->exists();
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Get setting for the given key.
*
* @param string $key
* @param mixed $default
2023-12-03 14:07:47 +00:00
*
2023-06-11 12:14:03 +00:00
* @return string|array
*/
public static function get($key, $default = null)
{
return static::where('key', $key)->first()->value ?? $default;
}
2023-12-03 14:07:47 +00:00
/**
* Set the given settings.
*
* @param array $settings
*
* @return void
*/
public static function setMany($settings)
{
foreach ($settings as $key => $value) {
self::set($key, $value);
}
}
2023-06-11 12:14:03 +00:00
/**
* Set the given setting.
*
* @param string $key
* @param mixed $value
2023-12-03 14:07:47 +00:00
*
2023-06-11 12:14:03 +00:00
* @return void
*/
public static function set($key, $value)
{
if ($key === 'translatable') {
return static::setTranslatableSettings($value);
}
static::updateOrCreate(['key' => $key], ['plain_value' => $value]);
}
/**
* Set a translatable settings.
*
* @param array $settings
2023-12-03 14:07:47 +00:00
*
2023-06-11 12:14:03 +00:00
* @return void
*/
public static function setTranslatableSettings($settings = [])
{
foreach ($settings as $key => $value) {
static::updateOrCreate(['key' => $key], [
'is_translatable' => true,
'value' => $value,
]);
}
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Get the value of the setting.
*
* @return mixed
*/
public function getValueAttribute()
{
if ($this->is_translatable) {
return $this->translateOrDefault(locale())->value ?? null;
}
return unserialize($this->plain_value);
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Set the value of the setting.
*
* @param mixed $value
2023-12-03 14:07:47 +00:00
*
2023-06-11 12:14:03 +00:00
* @return mixed
*/
public function setPlainValueAttribute($value)
{
$this->attributes['plain_value'] = serialize($value);
}
}