function DateTimeIso8601Normalizer::denormalize

Same name and namespace in other branches
  1. 9 core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php \Drupal\serialization\Normalizer\DateTimeIso8601Normalizer::denormalize()
  2. 10 core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php \Drupal\serialization\Normalizer\DateTimeIso8601Normalizer::denormalize()
  3. 11.x core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php \Drupal\serialization\Normalizer\DateTimeIso8601Normalizer::denormalize()

Overrides DateTimeNormalizer::denormalize

File

core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php, line 57

Class

DateTimeIso8601Normalizer
Converts values for the DateTimeIso8601 data type to RFC3339.

Namespace

Drupal\serialization\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
    // @todo Move the date-only handling out of here in https://www.drupal.org/project/drupal/issues/2958416.
    $field_definition = isset($context['target_instance']) ? $context['target_instance']->getFieldDefinition() : (isset($context['field_definition']) ? $context['field_definition'] : NULL);
    if ($field_definition === NULL) {
        throw new InvalidArgumentException('$context[\'target_instance\'] or $context[\'field_definition\'] must be set to denormalize with the DateTimeIso8601Normalizer');
    }
    $datetime_type = $field_definition->getSetting('datetime_type');
    $is_date_only = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE;
    if ($is_date_only) {
        $context['datetime_allowed_formats'] = array_intersect_key($this->allowedFormats, [
            'date-only' => TRUE,
        ]);
        $datetime = parent::denormalize($data, $class, $format, $context);
        if (!$datetime instanceof \DateTime) {
            return $datetime;
        }
        return $datetime->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
    }
    $context['datetime_allowed_formats'] = array_diff_key($this->allowedFormats, [
        'date-only' => TRUE,
    ]);
    try {
        $datetime = parent::denormalize($data, $class, $format, $context);
    } catch (\UnexpectedValueException $e) {
        // If denormalization didn't work using any of the actively supported
        // formats, try again with the BC format too. Explicitly label it as
        // being deprecated and trigger a deprecation error.
        $using_deprecated_format = TRUE;
        $context['datetime_allowed_formats']['backward compatibility — deprecated'] = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
        $datetime = parent::denormalize($data, $class, $format, $context);
    }
    if (!$datetime instanceof \DateTime) {
        return $datetime;
    }
    if (isset($using_deprecated_format)) {
        @trigger_error('The provided datetime string format (Y-m-d\\TH:i:s) is deprecated and will be removed before Drupal 9.0.0. Use the RFC3339 format instead (Y-m-d\\TH:i:sP).', E_USER_DEPRECATED);
    }
    $datetime->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
    return $datetime->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.