public function DateTimeNormalizer::denormalize in Drupal 9
Same name and namespace in other branches
- 8 core/modules/serialization/src/Normalizer/DateTimeNormalizer.php \Drupal\serialization\Normalizer\DateTimeNormalizer::denormalize()
2 calls to DateTimeNormalizer::denormalize()
- DateTimeIso8601Normalizer::denormalize in core/
modules/ serialization/ src/ Normalizer/ DateTimeIso8601Normalizer.php - TimestampNormalizer::denormalize in core/
modules/ serialization/ src/ Normalizer/ TimestampNormalizer.php
2 methods override DateTimeNormalizer::denormalize()
- DateTimeIso8601Normalizer::denormalize in core/
modules/ serialization/ src/ Normalizer/ DateTimeIso8601Normalizer.php - TimestampNormalizer::denormalize in core/
modules/ serialization/ src/ Normalizer/ TimestampNormalizer.php
File
- core/
modules/ serialization/ src/ Normalizer/ DateTimeNormalizer.php, line 88
Class
- DateTimeNormalizer
- Converts values for datetime objects to RFC3339 and from common formats.
Namespace
Drupal\serialization\NormalizerCode
public function denormalize($data, $class, $format = NULL, array $context = []) {
// This only knows how to denormalize datetime strings and timestamps. If
// something else is received, let validation constraints handle this.
if (!is_string($data) && !is_numeric($data)) {
return $data;
}
// Loop through the allowed formats and create a \DateTime from the
// input data if it matches the defined pattern. Since the formats are
// unambiguous (i.e., they reference an absolute time with a defined time
// zone), only one will ever match.
$allowed_formats = isset($context['datetime_allowed_formats']) ? $context['datetime_allowed_formats'] : $this->allowedFormats;
foreach ($allowed_formats as $format) {
$date = \DateTime::createFromFormat($format, $data);
$errors = \DateTime::getLastErrors();
if ($date !== FALSE && empty($errors['errors']) && empty($errors['warnings'])) {
return $date;
}
}
$format_strings = [];
foreach ($allowed_formats as $label => $format) {
$format_strings[] = "\"{$format}\" ({$label})";
}
$formats = implode(', ', $format_strings);
throw new UnexpectedValueException(sprintf('The specified date "%s" is not in an accepted format: %s.', $data, $formats));
}