public function testCreateFromFormatFailure() { $time = 'foo'; $immutable = DateTimeImmutable::createFromFormat(DateTime::RFC3339, $time); $mutable = DateTime::createFromFormat(DateTime::RFC3339, $time); $this->assertFalse($immutable); $this->assertSame(DateTime::getLastErrors(), DateTimeImmutable::getLastErrors()); }
/** * {@inheritdoc} * * @throws UnexpectedValueException */ public function denormalize($data, $class, $format = null, array $context = array()) { $dateTimeFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : null; if (null !== $dateTimeFormat) { $object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data); if (false !== $object) { return $object; } $dateTimeErrors = \DateTime::class === $class ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors(); throw new UnexpectedValueException(sprintf('Parsing datetime string "%s" using format "%s" resulted in %d errors:' . "\n" . '%s', $data, $dateTimeFormat, $dateTimeErrors['error_count'], implode("\n", $this->formatDateTimeErrors($dateTimeErrors['errors'])))); } try { return \DateTime::class === $class ? new \DateTime($data) : new \DateTimeImmutable($data); } catch (\Exception $e) { throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); } }
/** * Get last error caused by DateTimeImmutable. * * @return string */ protected static function _getLastDateTimeImmutableErrorsStr() { $errors = \DateTimeImmutable::getLastErrors()["errors"]; return implode(", ", $errors); }