You are here

abstract class NormalizerBase in Drupal 10

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 core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase
  2. 9 core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase

Base normalizer used in all JSON:API normalizers.

@internal JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.

Hierarchy

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

Expanded class hierarchy of NormalizerBase

See also

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

jsonapi.api.php

File

core/modules/jsonapi/src/Normalizer/NormalizerBase.php, line 17

Namespace

Drupal\jsonapi\Normalizer
View source
abstract class NormalizerBase extends SerializationNormalizerBase {

  /**
   * {@inheritdoc}
   */
  protected $format = 'api_json';

  /**
   * Rasterizes a value recursively.
   *
   * This is mainly for configuration entities where a field can be a tree of
   * values to rasterize.
   *
   * @param mixed $value
   *   Either a scalar, an array or a rasterizable object.
   *
   * @return mixed
   *   The rasterized value.
   */
  protected static function rasterizeValueRecursive($value) {
    if (!$value || is_scalar($value)) {
      return $value;
    }
    if (is_array($value)) {
      $output = [];
      foreach ($value as $key => $item) {
        $output[$key] = static::rasterizeValueRecursive($item);
      }
      return $output;
    }
    if ($value instanceof CacheableNormalization) {
      return $value
        ->getNormalization();
    }

    // If the object can be turned into a string it's better than nothing.
    if (method_exists($value, '__toString')) {
      return $value
        ->__toString();
    }

    // We give up, since we do not know how to rasterize this.
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  protected function checkFormat($format = NULL) {

    // The parent implementation allows format-specific normalizers to be used
    // for formatless normalization. The JSON:API module wants to be cautious.
    // Hence it only allows its normalizers to be used for the JSON:API format,
    // to avoid JSON:API-specific normalizations showing up in the REST API.
    return $format === $this->format;
  }

}

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::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports.
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::hasCacheableSupportsMethod public function
NormalizerBase::rasterizeValueRecursive protected static function Rasterizes a value recursively.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization()
NormalizerBase::supportsNormalization public function