8000 [Validator][ConstraintValidator] Safe fail on invalid timezones · symfony/symfony@cebb5ac · GitHub
[go: up one dir, main page]

Skip to content

Commit cebb5ac

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

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/Symfony/Component/Validator/ConstraintValidator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,14 @@ 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());
90+
$formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
91+
92+
$timezone = $value->getTimezone();
93+
if ('Z' === strtoupper($timezone->getName())) {
94+
$timezone = new \DateTimeZone('UTC');
95+
}
96+
97+
$formatter->setTimeZone($timezone);
9298

9399
// neither the native nor the stub IntlDateFormatter support
94100
// DateTimeImmutable as of yet

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', (new \DateTimeImmutable('@0'))->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', (new \DateTimeImmutable('1971-02-02T08:00:00'))->setTimezone(new \DateTimeZone('UTC')), 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)
0