leenooks/src/Casts/LeenooksCarbon.php

31 lines
810 B
PHP
Raw Normal View History

2024-07-07 11:50:57 +10:00
<?php
namespace Leenooks\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Leenooks\Carbon;
2024-07-07 11:50:57 +10:00
class LeenooksCarbon implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
2024-07-31 22:40:35 +10:00
public function get(Model $model, string $key, mixed $value, array $attributes): ?Carbon
2024-07-07 11:50:57 +10:00
{
2024-07-31 22:40:35 +10:00
return $value ? Carbon::create($value) : NULL;
2024-07-07 11:50:57 +10:00
}
/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): string
2024-07-07 11:50:57 +10:00
{
return ($value instanceof Carbon ? $value : $this->get($model,$key,$value,$attributes))->format('Y-m-d H:i:s');
2024-07-07 11:50:57 +10:00
}
}