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));
|
), array_flip($this->fields));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
|
if (!$dateTime instanceof \DateTimeInterface) {
|
||||||
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
|
throw new TransformationFailedException('Expected a \DateTimeInterface.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->inputTimezone !== $this->outputTimezone) {
|
if ($this->inputTimezone !== $this->outputTimezone) {
|
||||||
|
@ -84,8 +84,8 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
|
if (!$dateTime instanceof \DateTimeInterface) {
|
||||||
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
|
throw new TransformationFailedException('Expected a \DateTimeInterface.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = $this->getIntlDateFormatter()->format($dateTime->getTimestamp());
|
$value = $this->getIntlDateFormatter()->format($dateTime->getTimestamp());
|
||||||
|
@ -27,8 +27,8 @@ class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
|
if (!$dateTime instanceof \DateTimeInterface) {
|
||||||
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
|
throw new TransformationFailedException('Expected a \DateTimeInterface.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->inputTimezone !== $this->outputTimezone) {
|
if ($this->inputTimezone !== $this->outputTimezone) {
|
||||||
|
@ -99,8 +99,8 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
|
if (!$value instanceof \DateTimeInterface) {
|
||||||
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
|
throw new TransformationFailedException('Expected a \DateTimeInterface.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$value instanceof \DateTimeImmutable) {
|
if (!$value instanceof \DateTimeImmutable) {
|
||||||
|
@ -38,8 +38,8 @@ class DateTimeToTimestampTransformer extends BaseDateTimeTransformer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
|
if (!$value instanceof \DateTimeInterface) {
|
||||||
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
|
throw new TransformationFailedException('Expected a \DateTimeInterface.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value->getTimestamp();
|
return $value->getTimestamp();
|
||||||
|
@ -51,7 +51,7 @@ class Cookie
|
|||||||
}
|
}
|
||||||
|
|
||||||
// convert expiration time to a Unix timestamp
|
// convert expiration time to a Unix timestamp
|
||||||
if ($expire instanceof \DateTime || $expire instanceof \DateTimeInterface) {
|
if ($expire instanceof \DateTimeInterface) {
|
||||||
$expire = $expire->format('U');
|
$expire = $expire->format('U');
|
||||||
} elseif (!is_numeric($expire)) {
|
} elseif (!is_numeric($expire)) {
|
||||||
$expire = strtotime($expire);
|
$expire = strtotime($expire);
|
||||||
|
@ -28,7 +28,7 @@ class ValueExporter
|
|||||||
public function exportValue($value, $depth = 1, $deep = false)
|
public function exportValue($value, $depth = 1, $deep = false)
|
||||||
{
|
{
|
||||||
if (is_object($value)) {
|
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));
|
return sprintf('Object(%s) - %s', get_class($value), $value->format(\DateTime::ISO8601));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
|
|||||||
* (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
|
* (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
|
||||||
* in double quotes ("). Objects, arrays and resources are formatted as
|
* in double quotes ("). Objects, arrays and resources are formatted as
|
||||||
* "object", "array" and "resource". If the $format bitmask contains
|
* "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").
|
* as RFC-3339 dates ("Y-m-d H:i:s").
|
||||||
*
|
*
|
||||||
* Be careful when passing message parameters to a constraint violation
|
* 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)
|
protected function formatValue($value, $format = 0)
|
||||||
{
|
{
|
||||||
$isDateTime = $value instanceof \DateTime || $value instanceof \DateTimeInterface;
|
$isDateTime = $value instanceof \DateTimeInterface;
|
||||||
|
|
||||||
if (($format & self::PRETTY_DATE) && $isDateTime) {
|
if (($format & self::PRETTY_DATE) && $isDateTime) {
|
||||||
if (class_exists('IntlDateFormatter')) {
|
if (class_exists('IntlDateFormatter')) {
|
||||||
|
@ -47,7 +47,7 @@ abstract class AbstractComparisonValidator extends ConstraintValidator
|
|||||||
// If $value is immutable, convert the compared value to a
|
// If $value is immutable, convert the compared value to a
|
||||||
// DateTimeImmutable too
|
// DateTimeImmutable too
|
||||||
$comparedValue = new \DatetimeImmutable($comparedValue);
|
$comparedValue = new \DatetimeImmutable($comparedValue);
|
||||||
} elseif ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
|
} elseif ($value instanceof \DateTimeInterface) {
|
||||||
// Otherwise use DateTime
|
// Otherwise use DateTime
|
||||||
$comparedValue = new \DateTime($comparedValue);
|
$comparedValue = new \DateTime($comparedValue);
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ class RangeValidator extends ConstraintValidator
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_numeric($value) && !$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
|
if (!is_numeric($value) && !$value instanceof \DateTimeInterface) {
|
||||||
$this->context->buildViolation($constraint->invalidMessage)
|
$this->context->buildViolation($constraint->invalidMessage)
|
||||||
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
|
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
|
||||||
->setCode(Range::INVALID_CHARACTERS_ERROR)
|
->setCode(Range::INVALID_CHARACTERS_ERROR)
|
||||||
@ -49,7 +49,7 @@ class RangeValidator extends ConstraintValidator
|
|||||||
// This allows to compare with any date/time value supported by
|
// This allows to compare with any date/time value supported by
|
||||||
// the DateTime constructor:
|
// the DateTime constructor:
|
||||||
// http://php.net/manual/en/datetime.formats.php
|
// http://php.net/manual/en/datetime.formats.php
|
||||||
if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
|
if ($value instanceof \DateTimeInterface) {
|
||||||
if (is_string($min)) {
|
if (is_string($min)) {
|
||||||
$min = new \DateTime($min);
|
$min = new \DateTime($min);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user