jsonapi.services.yml | 11 --- .../Normalizer/DateTimeIso8601Normalizer.php | 103 ------------------- .../Normalizer/DateTimeNormalizer.php | 109 --------------------- .../Normalizer/TimestampNormalizer.php | 55 ----------- 4 files changed, 278 deletions(-) diff --git a/jsonapi.services.yml b/jsonapi.services.yml index 7afc085..cb73997 100644 --- a/jsonapi.services.yml +++ b/jsonapi.services.yml @@ -215,17 +215,6 @@ services: deprecated: The "%service_id%" service is deprecated. You should use the 'serializer.normalizer.http_exception.jsonapi' service instead. # Forward compatibility. - # @todo Remove in Drupal 8.6 (assuming it contains https://www.drupal.org/project/drupal/issues/2926508). - serializer.normalizer.timestamp.jsonapi: - class: \Drupal\jsonapi\ForwardCompatibility\Normalizer\TimestampNormalizer - tags: - # Priority must be higher than serializer.normalizer.primitive_data. - - { name: normalizer, priority: 20, bc: bc_timestamp_normalizer_unix, bc_config_name: 'serialization.settings' } - serializer.normalizer.datetimeiso8601.jsonapi: - class: \Drupal\jsonapi\ForwardCompatibility\Normalizer\DateTimeIso8601Normalizer - tags: - # Priority must be higher than serializer.normalizer.primitive_data. - - { name: normalizer, priority: 20 } # @todo Remove in Drupal 8.7 (assuming it contains https://www.drupal.org/project/drupal/issues/2940383) jsonapi.file.uploader.field: class: Drupal\jsonapi\ForwardCompatibility\FileFieldUploader diff --git a/src/ForwardCompatibility/Normalizer/DateTimeIso8601Normalizer.php b/src/ForwardCompatibility/Normalizer/DateTimeIso8601Normalizer.php deleted file mode 100644 index 7091a2a..0000000 --- a/src/ForwardCompatibility/Normalizer/DateTimeIso8601Normalizer.php +++ /dev/null @@ -1,103 +0,0 @@ - \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.7) - ? $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); - } - } - -} diff --git a/src/ForwardCompatibility/Normalizer/DateTimeNormalizer.php b/src/ForwardCompatibility/Normalizer/DateTimeNormalizer.php deleted file mode 100644 index 53f01e0..0000000 --- a/src/ForwardCompatibility/Normalizer/DateTimeNormalizer.php +++ /dev/null @@ -1,109 +0,0 @@ - \DateTime::RFC3339, - 'ISO 8601' => \DateTime::ISO8601, - ]; - - /** - * {@inheritdoc} - */ - protected $supportedInterfaceOrClass = DateTimeInterface::class; - - /** - * {@inheritdoc} - */ - public function normalize($datetime, $format = NULL, array $context = []) { - // @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.7) - ? $datetime->getDateTime() - : ($datetime->getValue() ? new DrupalDateTime($datetime->getValue(), 'UTC') : NULL); - if ($drupal_date_time === NULL) { - return $drupal_date_time; - } - return $drupal_date_time - // Set an explicit timezone. Otherwise, timestamps may end up being - // normalized using the user's preferred timezone. Which would result in - // many variations and complex caching. - // @see \Drupal\Core\Datetime\DrupalDateTime::prepareTimezone() - // @see drupal_get_user_timezone() - ->setTimezone($this->getNormalizationTimezone()) - ->format(\DateTime::RFC3339); - } - - /** - * Gets the timezone to be used during normalization. - * - * @see ::normalize - * - * @returns \DateTimeZone - * The timezone to use. - */ - protected function getNormalizationTimezone() { - $default_site_timezone = \Drupal::config('system.date')->get('timezone.default'); - return new \DateTimeZone($default_site_timezone); - } - - /** - * {@inheritdoc} - */ - public function denormalize($data, $class, $format = NULL, array $context = []) { - // This only knows how to denormalize datetime strings and timestamps. If - // something else is received, let validation constraints handle this. - if (!is_string($data) && !is_numeric($data)) { - return $data; - } - - // Loop through the allowed formats and create a \DateTime from the - // input data if it matches the defined pattern. Since the formats are - // unambiguous (i.e., they reference an absolute time with a defined time - // zone), only one will ever match. - $allowed_formats = isset($context['datetime_allowed_formats']) - ? $context['datetime_allowed_formats'] - : $this->allowedFormats; - foreach ($allowed_formats as $format) { - $date = \DateTime::createFromFormat($format, $data); - $errors = \DateTime::getLastErrors(); - if ($date !== FALSE && empty($errors['errors']) && empty($errors['warnings'])) { - return $date; - } - } - - $format_strings = []; - - foreach ($allowed_formats as $label => $format) { - $format_strings[] = "\"$format\" ($label)"; - } - - $formats = implode(', ', $format_strings); - throw new UnexpectedValueException(sprintf('The specified date "%s" is not in an accepted format: %s.', $data, $formats)); - } - -} diff --git a/src/ForwardCompatibility/Normalizer/TimestampNormalizer.php b/src/ForwardCompatibility/Normalizer/TimestampNormalizer.php deleted file mode 100644 index 9894e63..0000000 --- a/src/ForwardCompatibility/Normalizer/TimestampNormalizer.php +++ /dev/null @@ -1,55 +0,0 @@ - '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(); - } - -}