schneespur/app/Models/Setting.php
Michael 2c63440ed8 Revert: move code back to project root from schneespur/ subdirectory
- Reverts the schneespur/ subdirectory restructure (b8e426b)
- Restores package.json and vite.config.js (needed for npm build, were
  removed in an earlier cleanup before the restructure)
- Updates public/build/ assets with current Vite output (new content hashes)
2026-05-17 18:24:26 +00:00

58 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
class Setting extends Model
{
protected $fillable = [
'key',
'value',
'type',
];
public static function get(string $key, mixed $default = null): mixed
{
try {
$row = static::where('key', $key)->first();
} catch (QueryException|\PDOException) {
return $default;
}
if (! $row) {
return $default;
}
return self::coerce($row->value, $row->type);
}
public static function set(string $key, mixed $value, string $type = 'string'): void
{
$stored = match ($type) {
'json' => json_encode($value),
'bool' => $value ? '1' : '0',
default => (string) $value,
};
static::updateOrCreate(
['key' => $key],
['value' => $stored, 'type' => $type],
);
}
private static function coerce(?string $value, string $type): mixed
{
if ($value === null) {
return null;
}
return match ($type) {
'int' => (int) $value,
'bool' => in_array($value, ['1', 'true', 'yes'], true),
'json' => json_decode($value, true),
default => $value,
};
}
}