2018-09-24 14:19:13 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-24 14:19:13 +02:00
|
|
|
namespace Engelsystem\Models;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2019-11-06 13:43:57 +01:00
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder;
|
2018-09-24 14:19:13 +02:00
|
|
|
|
2018-10-28 16:54:15 +01:00
|
|
|
/**
|
2019-11-06 13:43:57 +01:00
|
|
|
* @property string $name
|
|
|
|
* @property string $value
|
|
|
|
* @property Carbon $created_at
|
|
|
|
* @property Carbon $updated_at
|
2018-10-28 16:54:15 +01:00
|
|
|
*
|
2019-11-06 13:43:57 +01:00
|
|
|
* @method static QueryBuilder|EventConfig[] whereName($value)
|
|
|
|
* @method static QueryBuilder|EventConfig[] whereValue($value)
|
|
|
|
* @method static QueryBuilder|EventConfig[] whereCreatedAt($value)
|
|
|
|
* @method static QueryBuilder|EventConfig[] whereUpdatedAt($value)
|
2018-10-28 16:54:15 +01:00
|
|
|
*/
|
2018-09-24 14:19:13 +02:00
|
|
|
class EventConfig extends BaseModel
|
|
|
|
{
|
|
|
|
/** @var string The primary key for the model */
|
2022-12-15 19:50:56 +01:00
|
|
|
protected $primaryKey = 'name'; // phpcs:ignore
|
2018-09-24 14:19:13 +02:00
|
|
|
|
|
|
|
/** @var bool Indicates if the IDs are auto-incrementing */
|
2022-12-15 19:50:56 +01:00
|
|
|
public $incrementing = false; // phpcs:ignore
|
2018-09-24 14:19:13 +02:00
|
|
|
|
|
|
|
/** @var string Required because it is not event_configs */
|
2022-12-15 19:50:56 +01:00
|
|
|
protected $table = 'event_config'; // phpcs:ignore
|
2018-09-24 14:19:13 +02:00
|
|
|
|
|
|
|
/** @var array Values that are mass assignable */
|
2022-12-15 19:50:56 +01:00
|
|
|
protected $fillable = ['name', 'value']; // phpcs:ignore
|
2018-09-24 14:19:13 +02:00
|
|
|
|
2022-12-15 19:50:56 +01:00
|
|
|
/** @var array<string, string> The configuration values that should be cast to native types */
|
|
|
|
protected array $valueCasts = [
|
2021-01-02 01:22:05 +01:00
|
|
|
'buildup_start' => 'datetime_human',
|
|
|
|
'event_start' => 'datetime_human',
|
|
|
|
'event_end' => 'datetime_human',
|
|
|
|
'teardown_end' => 'datetime_human',
|
2019-08-21 14:22:31 +02:00
|
|
|
'last_metrics' => 'datetime',
|
2018-09-24 14:19:13 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/** @var bool It could be interesting to know when a value changed the last time */
|
2022-12-15 19:50:56 +01:00
|
|
|
public $timestamps = true; // phpcs:ignore
|
2018-09-24 14:19:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Value accessor
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function getValueAttribute(mixed $value): mixed
|
2018-09-24 14:19:13 +02:00
|
|
|
{
|
2021-12-01 00:27:46 +01:00
|
|
|
$value = $value ? $this->fromJson($value) : null;
|
2018-09-24 14:19:13 +02:00
|
|
|
|
|
|
|
/** @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::castAttribute */
|
|
|
|
if (!empty($value)) {
|
2022-12-22 00:08:54 +01:00
|
|
|
return match ($this->getValueCast($this->name)) {
|
|
|
|
'datetime_human' => Carbon::make($value),
|
|
|
|
'datetime' => Carbon::createFromFormat(Carbon::ISO8601, $value),
|
|
|
|
default => $value,
|
|
|
|
};
|
2018-09-24 14:19:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Value mutator
|
|
|
|
*
|
|
|
|
* @return static
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function setValueAttribute(mixed $value): static
|
2018-09-24 14:19:13 +02:00
|
|
|
{
|
|
|
|
if (!empty($value)) {
|
2022-12-22 00:08:54 +01:00
|
|
|
$value = match ($this->getValueCast($this->name)) {
|
|
|
|
/** @var Carbon $value */
|
|
|
|
'datetime_human' => $value->toDateTimeString('minute'),
|
|
|
|
/** @var Carbon $value */
|
|
|
|
'datetime' => $value->toIso8601String(),
|
|
|
|
default => $value,
|
|
|
|
};
|
2018-09-24 14:19:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$value = $this->castAttributeAsJson('value', $value);
|
|
|
|
$this->attributes['value'] = $value;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the value has to be casted
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function getValueCast(string $value): ?string
|
2018-09-24 14:19:13 +02:00
|
|
|
{
|
|
|
|
return isset($this->valueCasts[$value]) ? $this->valueCasts[$value] : null;
|
|
|
|
}
|
|
|
|
}
|