Skip to content

Commit

Permalink
Conditionally cast value when passing it down to field
Browse files Browse the repository at this point in the history
  • Loading branch information
marttinnotta committed Oct 9, 2023
1 parent ff74c34 commit a0b6052
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,28 @@ protected static function booted()

public function setValueAttribute($value)
{
$this->attributes['value'] = is_array($value) || $value instanceof \JsonSerializable
? json_encode($value)
: $value;
$this->casts = NovaSettings::getCasts();

$castType = null;
if ($this->hasCast($this->key)) $castType = $this->getCastType($this->key);

switch ($castType) {
case 'datetime':
case 'date':
$this->attributes['value'] = $value;
return;

default:
$this->attributes['value'] = is_array($value) || $value instanceof \JsonSerializable
? json_encode($value)
: $value;
}
}

public function getValueAttribute($value)
{
$originalCasts = $this->casts;
$this->casts = NovaSettings::getCasts();
$this->casts = NovaSettings::getCasts();

if ($this->hasCast($this->key)) {
$value = $this->castAttribute($this->key, $value);
Expand Down

0 comments on commit a0b6052

Please sign in to comment.