You are here

class DateTimeIso8601Normalizer in JSON:API 8.2

Converts values for the DateTimeIso8601 data type to RFC3339.

@internal JSON:API maintains no PHP API. The API is the HTTP API. This class may change at any time and could break any dependencies on it.

@todo Remove when JSON:API requires a version of Drupal core that includes https://www.drupal.org/project/drupal/issues/2926508.

Hierarchy

Expanded class hierarchy of DateTimeIso8601Normalizer

See also

https://www.drupal.org/project/jsonapi/issues/3032787

jsonapi.api.php

\Drupal\serialization\Normalizer\DateTimeIso8601Normalizer

1 string reference to 'DateTimeIso8601Normalizer'
jsonapi.services.yml in ./jsonapi.services.yml
jsonapi.services.yml
1 service uses DateTimeIso8601Normalizer
serializer.normalizer.datetimeiso8601.jsonapi in ./jsonapi.services.yml
\Drupal\jsonapi\ForwardCompatibility\Normalizer\DateTimeIso8601Normalizer

File

src/ForwardCompatibility/Normalizer/DateTimeIso8601Normalizer.php, line 22

Namespace

Drupal\jsonapi\ForwardCompatibility\Normalizer
View source
class DateTimeIso8601Normalizer extends DateTimeNormalizer {

  /**
   * {@inheritdoc}
   */
  protected $allowedFormats = [
    'RFC 3339' => \DateTime::RFC3339,
    'ISO 8601' => \DateTime::ISO8601,
    // @todo Remove this in https://www.drupal.org/project/drupal/issues/2958416.
    // RFC3339 only covers combined date and time representations. For date-only
    // representations, we need to use ISO 8601. There isn't a constant on the
    // \DateTime class that we can use, so we have to hardcode the format.
    // @see https://en.wikipedia.org/wiki/ISO_8601#Calendar_dates
    // @see \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATE_STORAGE_FORMAT
    'date-only' => 'Y-m-d',
  ];

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = DateTimeIso8601::class;

  /**
   * {@inheritdoc}
   */
  public function normalize($datetime, $format = NULL, array $context = []) {
    $field_item = $datetime
      ->getParent();

    // @todo Remove this in https://www.drupal.org/project/drupal/issues/2958416.
    if ($field_item instanceof DateTimeItem && $field_item
      ->getFieldDefinition()
      ->getFieldStorageDefinition()
      ->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {

      // @todo Remove when JSON:API only supports Drupal >=8.7, which fixed this in https://www.drupal.org/project/drupal/issues/3002164.
      $drupal_date_time = floatval(floatval(\Drupal::VERSION) >= 8.699999999999999) ? $datetime
        ->getDateTime() : ($datetime
        ->getValue() ? new DrupalDateTime($datetime
        ->getValue(), 'UTC') : NULL);
      if ($drupal_date_time === NULL) {
        return $drupal_date_time;
      }
      return $drupal_date_time
        ->format($this->allowedFormats['date-only']);
    }
    return parent::normalize($datetime, $format, $context);
  }

  /**
   * {@inheritdoc}
   */
  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);
    $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);
      unset($context['datetime_allowed_formats']);
      if (!$datetime instanceof \DateTime) {
        return $datetime;
      }
      return $datetime
        ->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
    }
    else {
      $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);
      }
      unset($context['datetime_allowed_formats']);
      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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
DateTimeIso8601Normalizer::$allowedFormats protected property Allowed datetime formats for the denormalizer. Overrides DateTimeNormalizer::$allowedFormats
DateTimeIso8601Normalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides DateTimeNormalizer::$supportedInterfaceOrClass
DateTimeIso8601Normalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides DateTimeNormalizer::denormalize
DateTimeIso8601Normalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides DateTimeNormalizer::normalize
DateTimeNormalizer::getNormalizationTimezone protected function Gets the timezone to be used during normalization. 1
NormalizerBase::$format protected property List of formats which supports (de-)normalization. Overrides NormalizerBase::$format
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. Overrides NormalizerBase::checkFormat
NormalizerBase::rasterizeValueRecursive protected static function Rasterizes a value recursively.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1