Skip to content

Commit 7fbfa3c

Browse files
fancywebScott Dawson
andcommitted
[Validator][ConstraintValidator] Safe fail on invalid timezones
Co-authored-by: Scott Dawson <scott@loyaltycorp.com.au>
1 parent bf877b8 commit 7fbfa3c

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/Symfony/Component/Validator/ConstraintValidator.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,12 @@ protected function formatValue($value, $format = 0)
8787
{
8888
if (($format & self::PRETTY_DATE) && $value instanceof \DateTimeInterface) {
8989
if (class_exists('IntlDateFormatter')) {
90-
$locale = \Locale::getDefault();
91-
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $value->getTimezone());
92-
93-
// neither the native nor the stub IntlDateFormatter support
94-
// DateTimeImmutable as of yet
95-
if (!$value instanceof \DateTime) {
96-
$value = new \DateTime(
97-
$value->format('Y-m-d H:i:s.u e'),
98-
$value->getTimezone()
99-
);
100-
}
101-
102-
return $formatter->format($value);
90+
$formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC');
91+
92+
return $formatter->format(new \DateTime(
93+
$value->format('Y-m-d H:i:s.u'),
94+
new \DateTimeZone('UTC')
95+
));
10396
}
10497

10598
return $value->format('Y-m-d H:i:s');

src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ final class ConstraintValidatorTest extends TestCase
2222
*/
2323
public function testFormatValue($expected, $value, $format = 0)
2424
{
25+
$default = date_default_timezone_get();
26+
date_default_timezone_set('Europe/Moscow');
27+
2528
$this->assertSame($expected, (new TestFormatValueConstraintValidator())->formatValueProxy($value, $format));
29+
30+
date_default_timezone_set($default);
2631
}
2732

2833
public function formatValueProvider()
2934
{
30-
$data = [
35+
return [
3136
['true', true],
3237
['false', false],
3338
['null', null],
@@ -36,11 +41,11 @@ public function formatValueProvider()
3641
['array', []],
3742
['object', $toString = new TestToStringObject()],
3843
['ccc', $toString, ConstraintValidator::OBJECT_TO_STRING],
39-
['object', $dateTime = (new \DateTimeImmutable('@0'))->setTimezone(new \DateTimeZone('UTC'))],
40-
[class_exists(\IntlDateFormatter::class) ? 'Jan 1, 1970, 12:00 AM' : '1970-01-01 00:00:00', $dateTime, ConstraintValidator::PRETTY_DATE],
44+
['object', $dateTime = (new \DateTimeImmutable('1971-02-02T08:00:00'))->setTimezone(new \DateTimeZone('UTC'))],
45+
[class_exists(\IntlDateFormatter::class) ? 'Oct 4, 2019, 11:02 AM' : '2019-10-04 11:02:03', new \DateTimeImmutable('2019-10-04T11:02:03+09:00'), ConstraintValidator::PRETTY_DATE],
46+
[class_exists(\IntlDateFormatter::class) ? 'Feb 2, 1971, 8:00 AM' : '1971-02-02 08:00:00', $dateTime, ConstraintValidator::PRETTY_DATE],
47+
[class_exists(\IntlDateFormatter::class) ? 'Jan 1, 1970, 6:00 AM' : '1970-01-01 6:00:00', (new \DateTimeImmutable('1970-01-01T06:00:00'))->setTimezone(new \DateTimeZone('Z')), ConstraintValidator::PRETTY_DATE],
4148
];
42-
43-
return $data;
4449
}
4550
}
4651

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy