Skip to content

Commit f0ef1e7

Browse files
committed
Extract relativeTime string parsing for readability
This makes it easier to read that we properly escape the `+` and the overall Regex is easier to verify.
1 parent b84190a commit f0ef1e7

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

webapp/src/Validator/Constraints/TimeStringValidator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class TimeStringValidator extends ConstraintValidator
1010
{
1111
public function validate(mixed $value, Constraint $constraint): void
1212
{
13+
$relativeRegex = "\d+:\d\d(:\d\d(\.\d{1,6})?)?";
1314
if (!$constraint instanceof TimeString) {
1415
throw new UnexpectedTypeException($constraint, TimeString::class);
1516
}
@@ -24,8 +25,8 @@ public function validate(mixed $value, Constraint $constraint): void
2425

2526
if ($constraint->allowRelative) {
2627
$regex = $constraint->relativeIsPositive ?
27-
"/^(\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}|\+\d+:\d\d(:\d\d(\.\d{1,6})?)?)$/" :
28-
"/^(\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}|-\d+:\d\d(:\d\d(\.\d{1,6})?)?)$/";
28+
"/^(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d{2,6})? [A-Za-z][A-Za-z0-9_\/+-]{1,35}|\+" . $relativeRegex . ")$/" :
29+
"/^(\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}|-" . $relativeRegex . ")$/";
2930
$message = $constraint->absoluteRelativeMessage;
3031
} else {
3132
$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}$/";

0 commit comments

Comments
 (0)