You are here

class TimestampNormalizer in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/serialization/src/Normalizer/TimestampNormalizer.php \Drupal\serialization\Normalizer\TimestampNormalizer

Converts values for the Timestamp data type to and from common formats.

@internal

Note that \Drupal\Core\TypedData\Plugin\DataType\Timestamp::getDateTime() explicitly sets a default timezone of UTC. This ensures the string representation generated by DateTimeNormalizer::normalize() is also in UTC.

Hierarchy

  • class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
    • class \Drupal\serialization\Normalizer\DateTimeNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface

Expanded class hierarchy of TimestampNormalizer

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

File

core/modules/serialization/src/Normalizer/TimestampNormalizer.php, line 16

Namespace

Drupal\serialization\Normalizer
View source
class TimestampNormalizer extends DateTimeNormalizer {

  /**
   * {@inheritdoc}
   */
  protected $allowedFormats = [
    'UNIX timestamp' => 'U',
    'ISO 8601' => \DateTime::ISO8601,
    'RFC 3339' => \DateTime::RFC3339,
  ];

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

  /**
   * {@inheritdoc}
   */
  protected function getNormalizationTimezone() {
    return new \DateTimeZone('UTC');
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    $denormalized = parent::denormalize($data, $class, $format, $context);
    return $denormalized
      ->getTimestamp();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
DateTimeNormalizer::$systemDateConfig protected property The system's date configuration.
DateTimeNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. 1
DateTimeNormalizer::__construct public function Constructs a new DateTimeNormalizer instance.
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 3
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
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
TimestampNormalizer::$allowedFormats protected property Allowed datetime formats for the denormalizer. Overrides DateTimeNormalizer::$allowedFormats
TimestampNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides DateTimeNormalizer::$supportedInterfaceOrClass
TimestampNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides DateTimeNormalizer::denormalize
TimestampNormalizer::getNormalizationTimezone protected function Gets the timezone to be used during normalization. Overrides DateTimeNormalizer::getNormalizationTimezone