bug #18759 [Validator] Support for DateTimeImmutable (krzysiekpiasecki)
This PR was squashed before being merged into the 3.0 branch (closes #18759).
Discussion
----------
[Validator] Support for DateTimeImmutable
| Q | A
| ------------- | ---
| Branch? | 3.0
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #18752
| License | MIT
| Doc PR |
When validating with DateTime constraint UnexpectedTypeException is thrown for DateTimeImmutable instances.
Why PR?
- DateTimeImmutable behaves like a DateTime. Both implements the same interface DateTimeInterface.
- DateTimeInterface cannot be implemented by the client.
Commits
-------
f49659f
[Validator] Support for DateTimeImmutable
This commit is contained in:
commit
5c3962e1b0
@ -30,7 +30,7 @@ class DateTimeValidator extends DateValidator
|
|||||||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime');
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === $value || '' === $value || $value instanceof \DateTime) {
|
if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ class DateValidator extends ConstraintValidator
|
|||||||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Date');
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Date');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === $value || '' === $value || $value instanceof \DateTime) {
|
if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,6 +42,13 @@ class DateTimeValidatorTest extends AbstractConstraintValidatorTest
|
|||||||
$this->assertNoViolation();
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDateTimeImmutableClassIsValid()
|
||||||
|
{
|
||||||
|
$this->validator->validate(new \DateTimeImmutable(), new DateTime());
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
|
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
|
||||||
*/
|
*/
|
||||||
|
@ -42,6 +42,13 @@ class DateValidatorTest extends AbstractConstraintValidatorTest
|
|||||||
$this->assertNoViolation();
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDateTimeImmutableClassIsValid()
|
||||||
|
{
|
||||||
|
$this->validator->validate(new \DateTimeImmutable(), new Date());
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
|
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user