/var/www/html/back/app/DtoCasts/DateTimeCast.php
<?php

declare(strict_types=1);

namespace App\DtoCasts;

use Carbon\Carbon;
use WendellAdriel\ValidatedDTO\Casting\Castable;

final class DateTimeCast implements Castable
{
    private string $format;

    public function __construct(string $format = 'Y-m-d H:i:s')
    {
        $this->format = $format;
    }

    public function cast(string $property, mixed $value): mixed
    {
        if (empty($value)) {
            return null;
        }

        return Carbon::createFromFormat(
            format: $this->format,
            time: $value
        );
    }
}