class TimestampItemNormalizer

Same name in this branch
  1. 9 core/modules/hal/src/Normalizer/TimestampItemNormalizer.php \Drupal\hal\Normalizer\TimestampItemNormalizer
Same name and namespace in other branches
  1. 11.x core/modules/serialization/src/Normalizer/TimestampItemNormalizer.php \Drupal\serialization\Normalizer\TimestampItemNormalizer
  2. 10 core/modules/serialization/src/Normalizer/TimestampItemNormalizer.php \Drupal\serialization\Normalizer\TimestampItemNormalizer
  3. 8.9.x core/modules/serialization/src/Normalizer/TimestampItemNormalizer.php \Drupal\serialization\Normalizer\TimestampItemNormalizer
  4. 8.9.x core/modules/hal/src/Normalizer/TimestampItemNormalizer.php \Drupal\hal\Normalizer\TimestampItemNormalizer

Converts values for TimestampItem to and from common formats.

Overrides FieldItemNormalizer to use \Drupal\serialization\Normalizer\TimestampNormalizer

Overrides FieldItemNormalizer to

Hierarchy

Expanded class hierarchy of TimestampItemNormalizer

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

File

core/modules/serialization/src/Normalizer/TimestampItemNormalizer.php, line 17

Namespace

Drupal\serialization\Normalizer
View source
class TimestampItemNormalizer extends FieldItemNormalizer {
  
  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = TimestampItem::class;
  
  /**
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) {
    return parent::normalize($object, $format, $context) + [
      // 'format' is not a property on Timestamp objects. This is present to
      // assist consumers of this data.
'format' => \DateTime::RFC3339,
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  protected function constructValue($data, $context) {
    if (!empty($data['format'])) {
      $context['datetime_allowed_formats'] = [
        $data['format'],
      ];
    }
    return [
      'value' => $this->serializer
        ->denormalize($data['value'], Timestamp::class, NULL, $context),
    ];
  }

}

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