ogsoft-example/app/Rules/DateHI.php

29 lines
617 B
PHP

<?php
declare(strict_types=1);
namespace App\Rules;
use Brick\DateTime\DateTimeException;
use Brick\DateTime\LocalTime;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
/**
* Validate time in format HH:MM
*/
class DateHI implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!preg_match('/^\d{2}:\d{2}$/', $value)) {
$fail('validation.date_hi');
}
try {
LocalTime::parse($value);
} catch (DateTimeException $e) {
$fail('validation.date_hi');
}
}
}