diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-24 14:19:13 +0200 |
---|---|---|
committer | msquare <msquare@notrademark.de> | 2018-10-30 21:13:56 +0100 |
commit | 63d1292bf80d88be40eec7695c1a59f29e6609c5 (patch) | |
tree | 0a17403ee3ebea784f3bab0ed4a158d645b450d5 /src/Models/EventConfig.php | |
parent | c4867811e26bc32798ba24dd701bc2c35d49b027 (diff) |
Added EventConfig model
Diffstat (limited to 'src/Models/EventConfig.php')
-rw-r--r-- | src/Models/EventConfig.php | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/Models/EventConfig.php b/src/Models/EventConfig.php new file mode 100644 index 00000000..e2f832cb --- /dev/null +++ b/src/Models/EventConfig.php @@ -0,0 +1,93 @@ +<?php + +namespace Engelsystem\Models; + +use Carbon\Carbon; + +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', + ]; + + /** @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; + } +} |