Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| EventConfig | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| getValueAttribute | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| setValueAttribute | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| getValueCast | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem\Models; |
| 6 | |
| 7 | use DateTimeInterface; |
| 8 | use Engelsystem\Helpers\Carbon; |
| 9 | use Illuminate\Database\Query\Builder as QueryBuilder; |
| 10 | |
| 11 | /** |
| 12 | * @property string $name |
| 13 | * @property string $value |
| 14 | * @property Carbon|null $created_at |
| 15 | * @property Carbon|null $updated_at |
| 16 | * |
| 17 | * @method static QueryBuilder|EventConfig[] whereName($value) |
| 18 | * @method static QueryBuilder|EventConfig[] whereValue($value) |
| 19 | * @method static QueryBuilder|EventConfig[] whereCreatedAt($value) |
| 20 | * @method static QueryBuilder|EventConfig[] whereUpdatedAt($value) |
| 21 | */ |
| 22 | class EventConfig extends BaseModel |
| 23 | { |
| 24 | /** @var string The primary key for the model */ |
| 25 | protected $primaryKey = 'name'; // phpcs:ignore |
| 26 | |
| 27 | /** @var bool Indicates if the IDs are auto-incrementing */ |
| 28 | public $incrementing = false; // phpcs:ignore |
| 29 | |
| 30 | /** @var string Required because it is not event_configs */ |
| 31 | protected $table = 'event_config'; // phpcs:ignore |
| 32 | |
| 33 | /** @var array Values that are mass assignable */ |
| 34 | protected $fillable = ['name', 'value']; // phpcs:ignore |
| 35 | |
| 36 | /** @var array<string, string> The configuration values that should be cast to native types */ |
| 37 | protected array $valueCasts = [ |
| 38 | 'buildup_start' => 'datetime_human', |
| 39 | 'event_start' => 'datetime_human', |
| 40 | 'event_end' => 'datetime_human', |
| 41 | 'teardown_end' => 'datetime_human', |
| 42 | 'last_metrics' => 'datetime', |
| 43 | ]; |
| 44 | |
| 45 | /** @var bool It could be interesting to know when a value changed the last time */ |
| 46 | public $timestamps = true; // phpcs:ignore |
| 47 | |
| 48 | /** |
| 49 | * Value accessor |
| 50 | */ |
| 51 | public function getValueAttribute(mixed $value): mixed |
| 52 | { |
| 53 | $value = $value ? $this->fromJson($value) : null; |
| 54 | |
| 55 | /** @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::castAttribute */ |
| 56 | if (!empty($value)) { |
| 57 | return match ($this->getValueCast($this->name)) { |
| 58 | 'datetime_human' => Carbon::make($value), |
| 59 | 'datetime' => Carbon::createFromFormat(DateTimeInterface::ATOM, $value), |
| 60 | default => $value, |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | return $value; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Value mutator |
| 69 | */ |
| 70 | public function setValueAttribute(mixed $value): static |
| 71 | { |
| 72 | if (!empty($value)) { |
| 73 | /** @var Carbon $value */ |
| 74 | $value = match ($this->getValueCast($this->name)) { |
| 75 | 'datetime_human' => $value->toDateTimeString('minute'), |
| 76 | 'datetime' => $value->format(DateTimeInterface::ATOM), |
| 77 | default => $value, |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | $value = $this->castAttributeAsJson('value', $value); |
| 82 | $this->attributes['value'] = $value; |
| 83 | |
| 84 | return $this; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Check if the value has to be casted |
| 89 | */ |
| 90 | protected function getValueCast(string $value): ?string |
| 91 | { |
| 92 | return $this->valueCasts[$value] ?? null; |
| 93 | } |
| 94 | } |