8000 deprecate using date and time types with date objects with not-matchi… · symfony/symfony@bf7222a · GitHub
[go: up one dir, main page]

Skip to content

Commit bf7222a

Browse files
committed
deprecate using date and time types with date objects with not-matching timezones
1 parent 16befcd commit bf7222a

File tree

8 files changed

+144
-0
lines changed

8 files changed

+144
-0
lines changed

UPGRADE-6.2.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
UPGRADE FROM 6.1 to 6.2
2+
=======================
3+
4+
Form
5+
----
6+
7+
* Deprecate using `DateTime` or `DateTimeImmutable` model data with a different timezone than configured with the
8+
`model_timezone` option in `DateType`, `DateTimeType`, and `TimeType`.

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Deprecate using `DateTime` or `DateTimeImmutable` model data with a different timezone than configured with the
8+
`model_timezone` option in `DateType`, `DateTimeType`, and `TimeType`.
9+
410
6.1
511
---
612

src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
2323
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
2424
use Symfony\Component\Form\FormBuilderInterface;
25+
use Symfony\Component\Form\FormEvent;
26+
use Symfony\Component\Form\FormEvents;
2527
use Symfony\Component\Form\FormInterface;
2628
use Symfony\Component\Form\FormView;
2729
use Symfony\Component\Form\ReversedTransformer;
@@ -200,6 +202,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
200202
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts)
201203
));
202204
}
205+
206+
if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
207+
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
208+
$date = $event->getData();
209+
210+
if (!$date instanceof \DateTimeInterface) {
211+
return;
212+
}
213+
214+
if ($date->getTimezone()->getName() !== $options['model_timezone']) {
215+
trigger_deprecation('symfony/form', '6.2', sprintf('Using a "%s" instance with a timezone ("%s") not matching the configured model timezone "%s" is deprecated.', \get_class($date), $date->getTimezone()->getName(), $options['model_timezone']));
216+
}
217+
});
218+
}
203219
}
204220

205221
/**

src/Symfony/Component/Form/Extension/Core/Type/DateType.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
2020
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
2121
use Symfony\Component\Form\FormBuilderInterface;
22+
use Symfony\Component\Form\FormEvent;
23+
use Symfony\Component\Form\FormEvents;
2224
use Symfony\Component\Form\FormInterface;
2325
use Symfony\Component\Form\FormView;
2426
use Symfony\Component\Form\ReversedTransformer;
@@ -180,6 +182,20 @@ class_exists(\IntlTimeZone::class, false) ? \IntlTimeZone::createDefault() : nul
180182
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], ['year', 'month', 'day'])
181183
));
182184
}
185+
186+
if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
187+
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
188+
$date = $event->getData();
189+
190+
if (!$date instanceof \DateTimeInterface) {
191+
return;
192+
}
193+
194+
if ($date->getTimezone()->getName() !== $options['model_timezone']) {
195+
trigger_deprecation('symfony/form', '6.2', sprintf('Using a "%s" instance with a timezone ("%s") not matching the configured model timezone "%s" is deprecated.', \get_class($date), $date->getTimezone()->getName(), $options['model_timezone']));
196+
}
197+
});
198+
}
183199
}
184200

185201
/**

src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
210210
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts, 'text' === $options['widget'], $options['reference_date'])
211211
));
212212
}
213+
214+
if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
215+
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
216+
$date = $event->getData();
217+
218+
if (!$date instanceof \DateTimeInterface) {
219+
return;
220+
}
221+
222+
if ($date->getTimezone()->getName() !== $options['model_timezone']) {
223+
trigger_deprecation('symfony/form', '6.2', sprintf('Using a "%s" instance with a timezone ("%s") not matching the configured model timezone "%s" is deprecated.', \get_class($date), $date->getTimezone()->getName(), $options['model_timezone']));
224+
}
225+
});
226+
}
213227
}
214228

215229
/**

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111

1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

14+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1415
use Symfony\Component\Form\FormError;
1516
use Symfony\Component\Form\FormInterface;
1617

1718
class DateTimeTypeTest extends BaseTypeTest
1819
{
20+
use ExpectDeprecationTrait;
21+
1922
public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\DateTimeType';
2023

2124
private $defaultLocale;
@@ -737,4 +740,29 @@ public function testSubmitStringWithCustomInputFormat()
737740

738741
$this->assertSame('14/01/2018 21:29:00 +00:00', $form->getData());
739742
}
743+
744+
/**
745+
* @group legacy
746+
*/
747+
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
748+
{
749+
$this->expectDeprecation('Since symfony/form 6.2: Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
750+
751+
$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
752+
'model_timezone' => 'Europe/Berlin',
753+
]);
754+
}
755+
756+
/**
757+
* @group legacy
758+
*/
759+
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
760+
{
761+
$this->expectDeprecation('Since symfony/form 6.2: Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
762+
763+
$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
764+
'input' => 'datetime_immutable',
765+
'model_timezone' => 'Europe/Berlin',
766+
]);
767+
}
740768
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

14+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1415
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
1516
use Symfony\Component\Form\FormError;
1617
use Symfony\Component\Form\FormInterface;
@@ -19,6 +20,8 @@
1920

2021
class DateTypeTest extends BaseTypeTest
2122
{
23+
use ExpectDeprecationTrait;
24+
2225
public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\DateType';
2326

2427
private $defaultTimezone;
@@ -1085,4 +1088,29 @@ public function testSubmitStringWithCustomInputFormat()
10851088

10861089
$this->assertSame('14/01/2018', $form->getData());
10871090
}
1091+
1092+
/**
1093+
* @group legacy
1094+
*/
1095+
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
1096+
{
1097+
$this->expectDeprecation('Since symfony/form 6.2: Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
1098+
1099+
$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
1100+
'model_timezone' => 'Europe/Berlin',
1101+
]);
1102+
}
1103+
1104+
/**
1105+
* @group legacy
1106+
*/
1107+
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
1108+
{
1109+
$this->expectDeprecation('Since symfony/form 6.2: Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
1110+
1111+
$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
1112+
'input' => 'datetime_immutable',
1113+
'model_timezone' => 'Europe/Berlin',
1114+
]);
1115+
}
10881116
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

14+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1415
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
1516
use Symfony\Component\Form\Exception\InvalidConfigurationException;
1617
use Symfony\Component\Form\Exception\LogicException;
@@ -20,6 +21,8 @@
2021

2122
class TimeTypeTest extends BaseTypeTest
2223
{
24+
use ExpectDeprecationTrait;
25+
2326
public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\TimeType';
2427

2528
public function testSubmitDateTime()
@@ -1126,4 +1129,29 @@ public function provideEmptyData()
11261129
'Compound choice field lazy' => ['choice', $lazyEmptyData, $expectedData],
11271130
];
11281131
}
1132+
1133+
/**
1134+
* @group legacy
1135+
*/
1136+
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
1137+
{
1138+
$this->expectDeprecation('Since symfony/form 6.2: Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
1139+
1140+
$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
1141+
'model_timezone' => 'Europe/Berlin',
1142+
]);
1143+
}
1144+
1145+
/**
1146+
* @group legacy
1147+
*/
1148+
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
1149+
{
1150+
$this->expectDeprecation('Since symfony/form 6.2: Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
1151+
1152+
$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
1153+
'input' => 'datetime_immutable',
1154+
'model_timezone' => 'Europe/Berlin',
1155+
]);
1156+
}
11291157
}

0 commit comments

Comments
 (0)
0