Skip to content

Commit

Permalink
bug #18759 [Validator] Support for DateTimeImmutable (krzysiekpiasecki)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicolas-grekas committed May 19, 2016
2 parents 720dac1 + f49659f commit 5c3962e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
Expand Up @@ -30,7 +30,7 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime');
}

if (null === $value || '' === $value || $value instanceof \DateTime) {
if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
return;
}

Expand Down
Expand Up @@ -47,7 +47,7 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Date');
}

if (null === $value || '' === $value || $value instanceof \DateTime) {
if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
return;
}

Expand Down
Expand Up @@ -42,6 +42,13 @@ public function testDateTimeClassIsValid()
$this->assertNoViolation();
}

public function testDateTimeImmutableClassIsValid()
{
$this->validator->validate(new \DateTimeImmutable(), new DateTime());

$this->assertNoViolation();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
Expand Down
Expand Up @@ -42,6 +42,13 @@ public function testDateTimeClassIsValid()
$this->assertNoViolation();
}

public function testDateTimeImmutableClassIsValid()
{
$this->validator->validate(new \DateTimeImmutable(), new Date());

$this->assertNoViolation();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
Expand Down

0 comments on commit 5c3962e

Please sign in to comment.