minor #18751 [3.0] Fixed DateTimeInterface comparison (francisbesset)

This PR was merged into the 3.0 branch.

Discussion
----------

[3.0] Fixed DateTimeInterface comparison

| Q             | A
| ------------- | ---
| Branch?       | 3.0
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

DateTime class implements the DateTimeInterface.
It is useless to compare is a value is an instance of DateTime or DateTimeInterface.

Commits
-------

9a0bc99 Fixed DateTimeInterface comparaison
This commit is contained in:
Fabien Potencier 2016-05-13 09:48:28 -05:00
commit 6aa00aa27e
10 changed files with 17 additions and 17 deletions

View File

@ -72,8 +72,8 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
), array_flip($this->fields));
}
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
if (!$dateTime instanceof \DateTimeInterface) {
throw new TransformationFailedException('Expected a \DateTimeInterface.');
}
if ($this->inputTimezone !== $this->outputTimezone) {

View File

@ -84,8 +84,8 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
return '';
}
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
if (!$dateTime instanceof \DateTimeInterface) {
throw new TransformationFailedException('Expected a \DateTimeInterface.');
}
$value = $this->getIntlDateFormatter()->format($dateTime->getTimestamp());

View File

@ -27,8 +27,8 @@ class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
return '';
}
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
if (!$dateTime instanceof \DateTimeInterface) {
throw new TransformationFailedException('Expected a \DateTimeInterface.');
}
if ($this->inputTimezone !== $this->outputTimezone) {

View File

@ -99,8 +99,8 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
return '';
}
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
if (!$value instanceof \DateTimeInterface) {
throw new TransformationFailedException('Expected a \DateTimeInterface.');
}
if (!$value instanceof \DateTimeImmutable) {

View File

@ -38,8 +38,8 @@ class DateTimeToTimestampTransformer extends BaseDateTimeTransformer
return;
}
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
if (!$value instanceof \DateTimeInterface) {
throw new TransformationFailedException('Expected a \DateTimeInterface.');
}
return $value->getTimestamp();

View File

@ -51,7 +51,7 @@ class Cookie
}
// convert expiration time to a Unix timestamp
if ($expire instanceof \DateTime || $expire instanceof \DateTimeInterface) {
if ($expire instanceof \DateTimeInterface) {
$expire = $expire->format('U');
} elseif (!is_numeric($expire)) {
$expire = strtotime($expire);

View File

@ -28,7 +28,7 @@ class ValueExporter
public function exportValue($value, $depth = 1, $deep = false)
{
if (is_object($value)) {
if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
if ($value instanceof \DateTimeInterface) {
return sprintf('Object(%s) - %s', get_class($value), $value->format(\DateTime::ISO8601));
}

View File

@ -72,7 +72,7 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
* (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
* in double quotes ("). Objects, arrays and resources are formatted as
* "object", "array" and "resource". If the $format bitmask contains
* the PRETTY_DATE bit, then {@link \DateTime} objects will be formatted
* the PRETTY_DATE bit, then {@link \DateTime} objects will be formatted
* as RFC-3339 dates ("Y-m-d H:i:s").
*
* Be careful when passing message parameters to a constraint violation
@ -89,7 +89,7 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
*/
protected function formatValue($value, $format = 0)
{
$isDateTime = $value instanceof \DateTime || $value instanceof \DateTimeInterface;
$isDateTime = $value instanceof \DateTimeInterface;
if (($format & self::PRETTY_DATE) && $isDateTime) {
if (class_exists('IntlDateFormatter')) {

View File

@ -47,7 +47,7 @@ abstract class AbstractComparisonValidator extends ConstraintValidator
// If $value is immutable, convert the compared value to a
// DateTimeImmutable too
$comparedValue = new \DatetimeImmutable($comparedValue);
} elseif ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
} elseif ($value instanceof \DateTimeInterface) {
// Otherwise use DateTime
$comparedValue = new \DateTime($comparedValue);
}

View File

@ -33,7 +33,7 @@ class RangeValidator extends ConstraintValidator
return;
}
if (!is_numeric($value) && !$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
if (!is_numeric($value) && !$value instanceof \DateTimeInterface) {
$this->context->buildViolation($constraint->invalidMessage)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setCode(Range::INVALID_CHARACTERS_ERROR)
@ -49,7 +49,7 @@ class RangeValidator extends ConstraintValidator
// This allows to compare with any date/time value supported by
// the DateTime constructor:
// http://php.net/manual/en/datetime.formats.php
if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
if ($value instanceof \DateTimeInterface) {
if (is_string($min)) {
$min = new \DateTime($min);
}