Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.5.2 under development

- Chg #789: Use `format` property for formatting dates in error messages when message format properties are not set (@WarLikeLaux)
Comment thread
samdark marked this conversation as resolved.
Outdated
Comment thread
samdark marked this conversation as resolved.
Outdated
- Enh #787: Explicitly import classes, functions, and constants in "use" section (@mspirkov)

## 2.5.1 December 12, 2025
Expand Down
10 changes: 8 additions & 2 deletions src/Rule/Date/BaseDateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
'Property' => $context->getCapitalizedTranslatedProperty(),
],
);
return $result;

Check warning on line 70 in src/Rule/Date/BaseDateHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ $date = $this->prepareValue($value, $rule, $timeZone, false); if ($date === null) { $result->addError($rule->getIncorrectInputMessage() ?? $this->incorrectInputMessage, ['property' => $context->getTranslatedProperty(), 'Property' => $context->getCapitalizedTranslatedProperty()]); - return $result; + } $min = $this->prepareValue($rule->getMin(), $rule, $timeZone, true); if ($min !== null && $date < $min) {
}

$min = $this->prepareValue($rule->getMin(), $rule, $timeZone, true);
Expand All @@ -81,7 +81,7 @@
'limit' => $this->formatDate($min, $rule, $timeZone),
],
);
return $result;

Check warning on line 84 in src/Rule/Date/BaseDateHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ $min = $this->prepareValue($rule->getMin(), $rule, $timeZone, true); if ($min !== null && $date < $min) { $result->addError($rule->getTooEarlyMessage() ?? $this->tooEarlyMessage, ['property' => $context->getTranslatedProperty(), 'Property' => $context->getCapitalizedTranslatedProperty(), 'value' => $this->formatDate($date, $rule, $timeZone), 'limit' => $this->formatDate($min, $rule, $timeZone)]); - return $result; + } $max = $this->prepareValue($rule->getMax(), $rule, $timeZone, true); if ($max !== null && $date > $max) {
}

$max = $this->prepareValue($rule->getMax(), $rule, $timeZone, true);
Expand Down Expand Up @@ -187,14 +187,20 @@

private function formatDate(DateTimeInterface $date, Date|DateTime|Time $rule, ?DateTimeZone $timeZone): string
{
$formatterDateType = $this->getMessageDateTypeFromRule($rule)
$ruleMessageDateType = $this->getMessageDateTypeFromRule($rule);
$ruleMessageTimeType = $this->getMessageTimeTypeFromRule($rule);

$formatterDateType = $ruleMessageDateType
?? $this->messageDateType
?? $this->getDateTypeFromRule($rule);
$formatterTimeType = $this->getMessageTimeTypeFromRule($rule)
$formatterTimeType = $ruleMessageTimeType
?? $this->messageTimeType
?? $this->getTimeTypeFromRule($rule);

$format = $rule->getMessageFormat() ?? $this->messageFormat;
if ($format === null && $ruleMessageDateType === null && $ruleMessageTimeType === null) {
$format = $rule->getFormat();
}
if (is_string($format) && str_starts_with($format, 'php:')) {
return $date->format(substr($format, 4));
}
Expand Down
34 changes: 26 additions & 8 deletions tests/Rule/Date/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static function dataValidationFailed(): array
'min' => [
'2024-03-29',
new Date(format: 'yyyy-MM-dd', min: '2025-01-01'),
['' => ['Value must be no early than 1/1/25.']],
['' => ['Value must be no early than 2025-01-01.']],
],
'min-custom-message' => [
['a' => '2024-03-29'],
Expand All @@ -88,12 +88,12 @@ public static function dataValidationFailed(): array
tooEarlyMessage: 'Prop — {property}. Date — {value}. Min — {limit}.',
),
],
['a' => ['Prop — a. Date — 3/29/24. Min — 1/1/25.']],
['a' => ['Prop — a. Date — 2024-03-29. Min — 2025-01-01.']],
],
'max' => [
'2024-03-29',
new Date(format: 'php:Y-m-d', max: '2024-01-01'),
['' => ['Value must be no late than 1/1/24.']],
['' => ['Value must be no late than 2024-01-01.']],
],
'max-custom-message' => [
['a' => '2024-03-29'],
Expand All @@ -104,18 +104,18 @@ public static function dataValidationFailed(): array
tooLateMessage: 'Prop — {property}. Date — {value}. Max — {limit}.',
),
],
['a' => ['Prop — a. Date — 3/29/24. Max — 1/1/24.']],
['a' => ['Prop — a. Date — 2024-03-29. Max — 2024-01-01.']],
],
'rule-and-handler-locales' => [
'2024-03-29',
new Date(format: 'php:Y-m-d', locale: 'ru', max: '2024-01-01'),
['' => ['Value must be no late than 01.01.2024.']],
['' => ['Value must be no late than 2024-01-01.']],
[DateHandler::class => new DateHandler(locale: 'en')],
],
'handler-locale' => [
'2024-03-29',
new Date(format: 'php:Y-m-d', max: '2024-01-01'),
['' => ['Value must be no late than 01.01.2024.']],
['' => ['Value must be no late than 2024-01-01.']],
[DateHandler::class => new DateHandler(locale: 'ru')],
],
'timestamp' => [
Expand All @@ -125,8 +125,8 @@ public static function dataValidationFailed(): array
],
'without-message-date-type' => [
'29*03*2024',
new Date(format: 'php:d*m*Y', max: '11*11*2023', ),
['' => ['Value must be no late than 11/11/23.']],
new Date(format: 'php:d*m*Y', max: '11*11*2023'),
['' => ['Value must be no late than 11*11*2023.']],
[DateHandler::class => new DateHandler(messageDateType: null)],
],
'rule-message-format' => [
Expand All @@ -153,6 +153,24 @@ public static function dataValidationFailed(): array
['' => ['Value must be no late than 10.11.2002.']],
[DateHandler::class => new DateHandler(locale: 'en')],
],
'format-used-for-message' => [
'01.01.2100',
new Date(
format: 'php:d.m.Y',
min: '19.11.2013',
max: '31.12.2099',
),
['' => ['Value must be no late than 31.12.2099.']],
],
'format-overridden-by-message-format' => [
'01.01.2100',
new Date(
format: 'php:d.m.Y',
max: '31.12.2099',
messageFormat: 'php:Y/m/d',
),
['' => ['Value must be no late than 2099/12/31.']],
],
];
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Rule/Date/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@ public static function dataValidationFailed(): array
'min' => [
'2024-03-29, 12:35',
new DateTime(format: 'yyyy-MM-dd, HH:mm', min: '2025-01-01, 11:00'),
['' => ['Value must be no early than 1/1/25, 11:00 AM.']],
['' => ['Value must be no early than 2025-01-01, 11:00.']],
],
'max' => [
'2024-03-29, 12:50',
new DateTime(format: 'php:Y-m-d, H:i', max: '2024-01-01, 00:00'),
['' => ['Value must be no late than 1/1/24, 12:00 AM.']],
['' => ['Value must be no late than 2024-01-01, 00:00.']],
],
'timestamp' => [
1711705158,
new DateTime(format: 'php:d.m.Y, H:i:s', min: 1711705200),
['' => ['Value must be no early than 3/29/24, 9:40 AM.']],
['' => ['Value must be no early than 29.03.2024, 09:40:00.']],
],
'without-message-date-and-time-type' => [
'29*03*2024*12*35',
new DateTime(format: 'php:d*m*Y*H*i', max: '11*11*2023*12*35'),
['' => ['Value must be no late than 11/11/23, 12:35 PM.']],
['' => ['Value must be no late than 11*11*2023*12*35.']],
[DateTimeHandler::class => new DateTimeHandler(messageDateType: null, messageTimeType: null)],
],
];
Expand Down
18 changes: 12 additions & 6 deletions tests/Rule/Date/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ public static function dataValidationFailed(): array
'min' => [
'15:30',
new Time(format: 'HH:mm', min: '15:40'),
['' => ['Value must be no early than 3:40 PM.']],
['' => ['Value must be no early than 15:40.']],
],
'max' => [
'15:30',
new Time(format: 'php:H:i', max: '12:00'),
['' => ['Value must be no late than 12:00 PM.']],
['' => ['Value must be no late than 12:00.']],
],
'timestamp' => [
1711705158,
new Time(format: 'php:d.m.Y, H:i:s', min: 1711705200),
['' => ['Value must be no early than 9:40 AM.']],
['' => ['Value must be no early than 29.03.2024, 09:40:00.']],
],
'without-message-time-type' => [
'13*30',
new Time(format: 'php:H*i', max: '11*30'),
['' => ['Value must be no late than 11:30 AM.']],
['' => ['Value must be no late than 11*30.']],
[TimeHandler::class => new TimeHandler(messageTimeType: null)],
],
'rule-message-format' => [
Expand All @@ -82,11 +82,17 @@ public static function dataValidationFailed(): array
[TimeHandler::class => new TimeHandler(messageFormat: 'php:H_i')],
],
'handler-message-type' => [
'13*30',
new Time(format: 'php:H*i', max: '11*30', timeType: IntlDateFormatter::SHORT),
"1:30\u{202F}PM",
new Time(max: "11:30\u{202F}AM", locale: 'en_US'),
Comment thread
samdark marked this conversation as resolved.
Outdated
['' => ['Value must be no late than 11:30:00 AM Coordinated Universal Time.']],
[TimeHandler::class => new TimeHandler(messageTimeType: IntlDateFormatter::FULL)],
],
'format-overrides-handler-message-type' => [
'13*30',
new Time(format: 'php:H*i', max: '11*30'),
['' => ['Value must be no late than 11*30.']],
[TimeHandler::class => new TimeHandler(messageTimeType: IntlDateFormatter::FULL)],
],
'rule-message-type-override-handler' => [
'13*30',
new Time(format: 'php:H*i', max: '11*30', messageTimeType: IntlDateFormatter::SHORT),
Expand Down
Loading