You are here

class TimestampNormalizer in JSON:API 8.2

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

@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

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

Expanded class hierarchy of TimestampNormalizer

See also

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

jsonapi.api.php

\Drupal\serialization\Normalizer\TimestampNormalizer

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

File

src/ForwardCompatibility/Normalizer/TimestampNormalizer.php, line 20

Namespace

Drupal\jsonapi\ForwardCompatibility\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}
   */
  public function normalize($datetime, $format = NULL, array $context = []) {
    return DrupalDateTime::createFromTimestamp($datetime
      ->getValue())
      ->setTimezone($this
      ->getNormalizationTimezone())
      ->format(\DateTime::RFC3339);
  }

  /**
   * {@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.
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
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
TimestampNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides DateTimeNormalizer::normalize