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