Same name in this branch
  1. 10 core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase
  2. 10 core/modules/serialization/src/Normalizer/NormalizerBase.php \Drupal\serialization\Normalizer\NormalizerBase
Same name and namespace in other branches
  1. 8.9.x core/modules/serialization/src/Normalizer/NormalizerBase.php \Drupal\serialization\Normalizer\NormalizerBase
  2. 9 core/modules/serialization/src/Normalizer/NormalizerBase.php \Drupal\serialization\Normalizer\NormalizerBase

Base class for Normalizers.

Hierarchy

Expanded class hierarchy of NormalizerBase

1 file declares its use of NormalizerBase
NormalizerBase.php in core/modules/jsonapi/src/Normalizer/NormalizerBase.php

File

core/modules/serialization/src/Normalizer/NormalizerBase.php, line 12

Namespace

Drupal\serialization\Normalizer
View source
abstract class NormalizerBase implements SerializerAwareInterface, CacheableNormalizerInterface {
  use SerializerAwareTrait;

  /**
   * List of formats which supports (de-)normalization.
   *
   * @var string|string[]
   */
  protected $format;

  /**
   * {@inheritdoc}
   */
  public function supportsNormalization($data, string $format = NULL, array $context = []) : bool {

    // If we aren't dealing with an object or the format is not supported return
    // now.
    if (!is_object($data) || !$this
      ->checkFormat($format)) {
      return FALSE;
    }
    if (property_exists($this, 'supportedInterfaceOrClass')) {
      @trigger_error('Defining ' . static::class . '::supportedInterfaceOrClass property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use getSupportedTypes() instead. See https://www.drupal.org/node/3359695', E_USER_DEPRECATED);
      $supported = (array) $this->supportedInterfaceOrClass;
    }
    else {
      $supported = array_keys($this
        ->getSupportedTypes($format));
    }
    return (bool) array_filter($supported, function ($name) use ($data) {
      return $data instanceof $name;
    });
  }

  /**
   * Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization()
   *
   * This class doesn't implement DenormalizerInterface, but most of its child
   * classes do. Therefore, this method is implemented at this level to reduce
   * code duplication.
   */
  public function supportsDenormalization($data, string $type, string $format = NULL, array $context = []) : bool {

    // If the format is not supported return now.
    if (!$this
      ->checkFormat($format)) {
      return FALSE;
    }
    if (property_exists($this, 'supportedInterfaceOrClass')) {
      @trigger_error('Defining ' . static::class . '::supportedInterfaceOrClass property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use getSupportedTypes() instead. See https://www.drupal.org/node/3359695', E_USER_DEPRECATED);
      $supported = (array) $this->supportedInterfaceOrClass;
    }
    else {
      $supported = array_keys($this
        ->getSupportedTypes($format));
    }
    $subclass_check = function ($name) use ($type) {
      return (class_exists($name) || interface_exists($name)) && is_subclass_of($type, $name, TRUE);
    };
    return in_array($type, $supported) || array_filter($supported, $subclass_check);
  }

  /**
   * Checks if the provided format is supported by this normalizer.
   *
   * @param string $format
   *   The format to check.
   *
   * @return bool
   *   TRUE if the format is supported, FALSE otherwise. If no format is
   *   specified this will return TRUE.
   */
  protected function checkFormat($format = NULL) {
    if (!isset($format) || !isset($this->format)) {
      return TRUE;
    }
    return in_array($format, (array) $this->format, TRUE);
  }

  /**
   * Adds cacheability if applicable.
   *
   * @param array $context
   *   Context options for the normalizer.
   * @param $data
   *   The data that might have cacheability information.
   */
  protected function addCacheableDependency(array $context, $data) {
    if ($data instanceof CacheableDependencyInterface && isset($context[static::SERIALIZATION_CONTEXT_CACHEABILITY])) {
      $context[static::SERIALIZATION_CONTEXT_CACHEABILITY]
        ->addCacheableDependency($data);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function hasCacheableSupportsMethod() : bool {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use getSupportedTypes() instead. See https://www.drupal.org/node/3359695', E_USER_DEPRECATED);
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getSupportedTypes(?string $format) : array {
    return [
      '*' => FALSE,
    ];
  }

}

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. 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::getSupportedTypes public function
NormalizerBase::hasCacheableSupportsMethod public function
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization()
NormalizerBase::supportsNormalization public function