mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-09-03 19:24:59 +02:00
45 lines
1.2 KiB
PHP
45 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Casts;
|
|
|
|
use App\ValueObjects\FuzzyDate;
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Arr;
|
|
use InvalidArgumentException;
|
|
|
|
class AsFuzzyDate implements CastsAttributes
|
|
{
|
|
public function get(Model $model, string $key, mixed $value, array $attributes): ?FuzzyDate
|
|
{
|
|
return FuzzyDate::fromString($value);
|
|
}
|
|
|
|
public function set(Model $model, string $key, mixed $value, array $attributes): ?string
|
|
{
|
|
if (blank($value) || $value === '00000000') {
|
|
return null;
|
|
}
|
|
|
|
if ($value instanceof FuzzyDate) {
|
|
return $value->__toString();
|
|
}
|
|
|
|
if (is_string($value)) {
|
|
return FuzzyDate::fromString($value)?->__toString();
|
|
}
|
|
|
|
if (is_array($value)) {
|
|
return new FuzzyDate(
|
|
(int) Arr::get($value, 'year') ?: null,
|
|
(int) Arr::get($value, 'month') ?: null,
|
|
(int) Arr::get($value, 'day') ?: null,
|
|
)->__toString();
|
|
}
|
|
|
|
throw new InvalidArgumentException('Invalid FuzzyDate value.');
|
|
}
|
|
}
|