koel/app/Casts/LicenseMetaCast.php

45 lines
1.2 KiB
PHP
Raw Normal View History

2024-01-05 16:42:50 +00:00
<?php
namespace App\Casts;
use App\Values\LicenseMeta;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\Facades\Log;
use Throwable;
class LicenseMetaCast implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes): ?LicenseMeta
{
try {
return $value ? LicenseMeta::fromJsonObject(json_decode($value)) : null;
} catch (Throwable) {
Log::error('Failed to cast-get license meta', [
'model' => $model,
'key' => $key,
'value' => $value,
'attributes' => $attributes,
]);
return null;
}
}
2024-01-07 12:43:10 +00:00
/** @param ?LicenseMeta $value */
2024-01-05 16:42:50 +00:00
public function set($model, string $key, $value, array $attributes): ?string
{
try {
return $value?->toJson();
} catch (Throwable) {
Log::error('Failed to cast-set license meta', [
'model' => $model,
'key' => $key,
'value' => $value,
'attributes' => $attributes,
]);
return null;
}
}
}