Skip to content

Commit c5a5413

Browse files
committed
Extract the absolute time away to show the differences in parsing
This makes it much easier to read what are the differences in parsing the positive & negative relative times. Next step is allowing for the CLICS absolute time format
1 parent 9fb4db7 commit c5a5413

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

webapp/src/Validator/Constraints/TimeStringValidator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class TimeStringValidator extends ConstraintValidator
1111
public function validate(mixed $value, Constraint $constraint): void
1212
{
1313
$timezoneRegex = "[A-Za-z][A-Za-z0-9_\/+-]{1,35}"; # See: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
14+
$absoluteRegex = "\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d{1,6})? " . $timezoneRegex;
1415
$relativeRegex = "\d+:\d\d(:\d\d(\.\d{1,6})?)?";
1516
if (!$constraint instanceof TimeString) {
1617
throw new UnexpectedTypeException($constraint, TimeString::class);
@@ -26,11 +27,11 @@ public function validate(mixed $value, Constraint $constraint): void
2627

2728
if ($constraint->allowRelative) {
2829
$regex = $constraint->relativeIsPositive ?
29-
"/^(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d{1,6})? " . $timeZoneRegex . "|\+?" . $relativeRegex . ")$/" :
30-
"/^(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d{1,6})? " . $timeZoneRegex . "|-" . $relativeRegex . ")$/";
30+
"/^(" . $absoluteRegex . "|\+?" . $relativeRegex . ")$/" :
31+
"/^(" . $absoluteRegex . "|-" . $relativeRegex . ")$/";
3132
$message = $constraint->absoluteRelativeMessage;
3233
} else {
33-
$regex = "/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d{1,6})? [A-Za-z][A-Za-z0-9_\/+-]{1,35}$/";
34+
$regex = "/^" . $absoluteRegex . "$/";
3435
$message = $constraint->absoluteMessage;
3536
}
3637
if (preg_match($regex, $value) !== 1) {

0 commit comments

Comments
 (0)