2021-10-08 16:23:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Casts;
|
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
use App\Values\SmartPlaylistRuleGroupCollection;
|
2021-10-08 16:23:45 +00:00
|
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
|
|
|
|
|
|
class SmartPlaylistRulesCast implements CastsAttributes
|
|
|
|
{
|
2022-11-27 15:29:29 +00:00
|
|
|
public function get($model, string $key, $value, array $attributes): ?SmartPlaylistRuleGroupCollection
|
2021-10-08 16:23:45 +00:00
|
|
|
{
|
2024-09-30 22:15:38 +00:00
|
|
|
return $value
|
|
|
|
? rescue(static fn () => SmartPlaylistRuleGroupCollection::create(json_decode($value, true)))
|
|
|
|
: null;
|
2021-10-08 16:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function set($model, string $key, $value, array $attributes): ?string
|
|
|
|
{
|
2022-11-27 15:29:29 +00:00
|
|
|
if (is_array($value)) {
|
|
|
|
$value = SmartPlaylistRuleGroupCollection::create($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value?->toJson() ?? null;
|
2021-10-08 16:23:45 +00:00
|
|
|
}
|
|
|
|
}
|