public function DateTimeNormalizer::denormalize in JSON:API 8.2
2 calls to DateTimeNormalizer::denormalize()
- DateTimeIso8601Normalizer::denormalize in src/
ForwardCompatibility/ Normalizer/ DateTimeIso8601Normalizer.php - Denormalizes data back into an object of the given class.
- TimestampNormalizer::denormalize in src/
ForwardCompatibility/ Normalizer/ TimestampNormalizer.php - Denormalizes data back into an object of the given class.
2 methods override DateTimeNormalizer::denormalize()
- DateTimeIso8601Normalizer::denormalize in src/
ForwardCompatibility/ Normalizer/ DateTimeIso8601Normalizer.php - Denormalizes data back into an object of the given class.
- TimestampNormalizer::denormalize in src/
ForwardCompatibility/ Normalizer/ TimestampNormalizer.php - Denormalizes data back into an object of the given class.
File
- src/
ForwardCompatibility/ Normalizer/ DateTimeNormalizer.php, line 82
Class
- DateTimeNormalizer
- Converts values for datetime objects to RFC3339 and from common formats.
Namespace
Drupal\jsonapi\ForwardCompatibility\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));
}