class DateTimeIso8601Normalizer

Same name in other branches
  1. 9 core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php \Drupal\serialization\Normalizer\DateTimeIso8601Normalizer
  2. 8.9.x core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php \Drupal\serialization\Normalizer\DateTimeIso8601Normalizer
  3. 11.x core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php \Drupal\serialization\Normalizer\DateTimeIso8601Normalizer

Converts values for the DateTimeIso8601 data type to RFC3339.

@internal

Hierarchy

Expanded class hierarchy of DateTimeIso8601Normalizer

1 file declares its use of DateTimeIso8601Normalizer
DateTimeIso8601NormalizerTest.php in core/modules/serialization/tests/src/Unit/Normalizer/DateTimeIso8601NormalizerTest.php
1 string reference to 'DateTimeIso8601Normalizer'
serialization.services.yml in core/modules/serialization/serialization.services.yml
core/modules/serialization/serialization.services.yml
1 service uses DateTimeIso8601Normalizer
serializer.normalizer.datetimeiso8601 in core/modules/serialization/serialization.services.yml
\Drupal\serialization\Normalizer\DateTimeIso8601Normalizer

File

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

Namespace

Drupal\serialization\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}
     */
    public function normalize($datetime, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
        assert($datetime instanceof DateTimeIso8601);
        $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) {
            $drupal_date_time = $datetime->getDateTime();
            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 = []) : mixed {
        // @todo Move the date-only handling out of here in https://www.drupal.org/project/drupal/issues/2958416.
        if (isset($context['target_instance'])) {
            $field_definition = $context['target_instance']->getFieldDefinition();
        }
        elseif (isset($context['field_definition'])) {
            $field_definition = $context['field_definition'];
        }
        else {
            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,
        ]);
        $datetime = parent::denormalize($data, $class, $format, $context);
        if (!$datetime instanceof \DateTime) {
            return $datetime;
        }
        $datetime->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
        return $datetime->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSupportedTypes(?string $format) : array {
        return [
            DateTimeIso8601::class => TRUE,
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title 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::denormalize public function Overrides DateTimeNormalizer::denormalize
DateTimeIso8601Normalizer::getSupportedTypes public function Overrides DateTimeNormalizer::getSupportedTypes
DateTimeIso8601Normalizer::normalize public function Overrides DateTimeNormalizer::normalize
DateTimeNormalizer::$systemDateConfig protected property The system's date configuration.
DateTimeNormalizer::getNormalizationTimezone protected function Gets the timezone to be used during normalization. 1
DateTimeNormalizer::hasCacheableSupportsMethod public function Overrides NormalizerBase::hasCacheableSupportsMethod
DateTimeNormalizer::__construct public function Constructs a new DateTimeNormalizer instance.
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 1
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 1
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function 1

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