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:
commit
6aa00aa27e
@ -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) {
|
||||
|
@ -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());
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
|
@ -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')) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user