From 5454e6fc1df07f8b3d45c8d93a8714a451de0a1c Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sat, 18 Nov 2017 13:47:08 +0100 Subject: [PATCH] [Validator] Deprecate validating DateTimeInterface in Date|Time|DateTime constraints --- UPGRADE-4.2.md | 1 + UPGRADE-5.0.md | 1 + src/Symfony/Component/Validator/CHANGELOG.md | 2 ++ src/Symfony/Component/Validator/Constraints/DateTime.php | 5 +++++ .../Component/Validator/Constraints/DateTimeValidator.php | 8 +++++++- .../Component/Validator/Constraints/DateValidator.php | 8 +++++++- .../Component/Validator/Constraints/TimeValidator.php | 8 +++++++- .../Validator/Tests/Constraints/DateTimeValidatorTest.php | 8 ++++++++ .../Validator/Tests/Constraints/DateValidatorTest.php | 8 ++++++++ .../Validator/Tests/Constraints/TimeValidatorTest.php | 8 ++++++++ 10 files changed, 54 insertions(+), 3 deletions(-) diff --git a/UPGRADE-4.2.md b/UPGRADE-4.2.md index 1ccb8d866717b..9647fcfd890be 100644 --- a/UPGRADE-4.2.md +++ b/UPGRADE-4.2.md @@ -162,3 +162,4 @@ Validator * The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead * The `ValidatorBuilderInterface` has been deprecated and `ValidatorBuilder` made final + * Deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`. Use `Type` instead or remove the constraint if the underlying model is type hinted to `\DateTimeInterface` already. diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index 5c397e9c96d60..23be0dcf556ce 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -163,6 +163,7 @@ Validator * Removed the `checkDNS` and `dnsMessage` options from the `Url` constraint. * The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead * The `ValidatorBuilderInterface` has been removed and `ValidatorBuilder` is now final + * Removed support for validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`. Use `Type` instead or remove the constraint if the underlying model is type hinted to `\DateTimeInterface` already. Workflow -------- diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 3c9be13a5d433..a1f068f3145a1 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -8,6 +8,8 @@ CHANGELOG * decoupled from `symfony/translation` by using `Symfony\Contracts\Translation\TranslatorInterface` * deprecated `ValidatorBuilderInterface` * made `ValidatorBuilder` final + * marked `format` the default option in `DateTime` constraint + * deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`. 4.1.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/DateTime.php b/src/Symfony/Component/Validator/Constraints/DateTime.php index c65f185ae428c..0a29b66abffc5 100644 --- a/src/Symfony/Component/Validator/Constraints/DateTime.php +++ b/src/Symfony/Component/Validator/Constraints/DateTime.php @@ -33,4 +33,9 @@ class DateTime extends Constraint public $format = 'Y-m-d H:i:s'; public $message = 'This value is not a valid datetime.'; + + public function getDefaultOption() + { + return 'format'; + } } diff --git a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php index 6ed25a3dfc291..ac6bf975d27dc 100644 --- a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php @@ -29,7 +29,13 @@ public function validate($value, Constraint $constraint) throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime'); } - if (null === $value || '' === $value || $value instanceof \DateTimeInterface) { + if (null === $value || '' === $value) { + return; + } + + if ($value instanceof \DateTimeInterface) { + @trigger_error(sprintf('Validating a \\DateTimeInterface with "%s" is deprecated since version 4.2. Use "%s" instead or remove the constraint if the underlying model is already type hinted to \\DateTimeInterface.', DateTime::class, Type::class), E_USER_DEPRECATED); + return; } diff --git a/src/Symfony/Component/Validator/Constraints/DateValidator.php b/src/Symfony/Component/Validator/Constraints/DateValidator.php index a1492c54c9a03..7b5825408aa05 100644 --- a/src/Symfony/Component/Validator/Constraints/DateValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateValidator.php @@ -47,7 +47,13 @@ public function validate($value, Constraint $constraint) throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Date'); } - if (null === $value || '' === $value || $value instanceof \DateTimeInterface) { + if (null === $value || '' === $value) { + return; + } + + if ($value instanceof \DateTimeInterface) { + @trigger_error(sprintf('Validating a \\DateTimeInterface with "%s" is deprecated since version 4.2. Use "%s" instead or remove the constraint if the underlying model is already type hinted to \\DateTimeInterface.', Date::class, Type::class), E_USER_DEPRECATED); + return; } diff --git a/src/Symfony/Component/Validator/Constraints/TimeValidator.php b/src/Symfony/Component/Validator/Constraints/TimeValidator.php index b1ee40c53051b..9bdd541c68872 100644 --- a/src/Symfony/Component/Validator/Constraints/TimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimeValidator.php @@ -47,7 +47,13 @@ public function validate($value, Constraint $constraint) throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Time'); } - if (null === $value || '' === $value || $value instanceof \DateTimeInterface) { + if (null === $value || '' === $value) { + return; + } + + if ($value instanceof \DateTimeInterface) { + @trigger_error(sprintf('Validating a \\DateTimeInterface with "%s" is deprecated since version 4.2. Use "%s" instead or remove the constraint if the underlying model is already type hinted to \\DateTimeInterface.', Time::class, Type::class), E_USER_DEPRECATED); + return; } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php index fe553100339f8..918d6577c928a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php @@ -36,6 +36,10 @@ public function testEmptyStringIsValid() $this->assertNoViolation(); } + /** + * @group legacy + * @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\DateTime" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface. + */ public function testDateTimeClassIsValid() { $this->validator->validate(new \DateTime(), new DateTime()); @@ -43,6 +47,10 @@ public function testDateTimeClassIsValid() $this->assertNoViolation(); } + /** + * @group legacy + * @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\DateTime" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface. + */ public function testDateTimeImmutableClassIsValid() { $this->validator->validate(new \DateTimeImmutable(), new DateTime()); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php index 3b2b189a55215..bf9c103f5810b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php @@ -36,6 +36,10 @@ public function testEmptyStringIsValid() $this->assertNoViolation(); } + /** + * @group legacy + * @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\Date" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface. + */ public function testDateTimeClassIsValid() { $this->validator->validate(new \DateTime(), new Date()); @@ -43,6 +47,10 @@ public function testDateTimeClassIsValid() $this->assertNoViolation(); } + /** + * @group legacy + * @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\Date" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface. + */ public function testDateTimeImmutableClassIsValid() { $this->validator->validate(new \DateTimeImmutable(), new Date()); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php index a22251d0ee3c5..3b5e1e594d4e7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php @@ -36,6 +36,10 @@ public function testEmptyStringIsValid() $this->assertNoViolation(); } + /** + * @group legacy + * @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\Time" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface. + */ public function testDateTimeClassIsValid() { $this->validator->validate(new \DateTime(), new Time()); @@ -100,6 +104,10 @@ public function getInvalidTimes() ); } + /** + * @group legacy + * @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\Time" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface. + */ public function testDateTimeImmutableIsValid() { $this->validator->validate(new \DateTimeImmutable(), new Time());