ldc-legaulois/app/Casts/NiceDate.php
2026-09-25 16:25:31 +02:00

39 lines
1.3 KiB
PHP

<?php
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
class NiceDate implements CastsAttributes
{
/**
* Cast the given value.
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
*/
public function get(?Model $model, string $key, mixed $value, array $attributes): string
{
// phpcs:enable
$value = Carbon::parse($value);
return $value->isToday() ?
trans('Aujourd\'hui à :time', ['time' => $value->format('H:i')]) : $value->format('Y/m/d à H:i');
}
/**
* Prepare the given value for storage.
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
*/
public function set(Model $model, string $key, mixed $value, array $attributes): string
{
// phpcs:enable
return Carbon::parse($value)->format('Y-m-d H:i:s');
}
}