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