class NormalizerBase

Same name in this branch
  1. 9 core/modules/serialization/src/Normalizer/NormalizerBase.php \Drupal\serialization\Normalizer\NormalizerBase
  2. 9 core/modules/hal/src/Normalizer/NormalizerBase.php \Drupal\hal\Normalizer\NormalizerBase
Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase
  2. 8.9.x core/modules/serialization/src/Normalizer/NormalizerBase.php \Drupal\serialization\Normalizer\NormalizerBase
  3. 8.9.x core/modules/hal/src/Normalizer/NormalizerBase.php \Drupal\hal\Normalizer\NormalizerBase
  4. 10 core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase
  5. 10 core/modules/serialization/src/Normalizer/NormalizerBase.php \Drupal\serialization\Normalizer\NormalizerBase
  6. 11.x core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase
  7. 11.x core/modules/serialization/src/Normalizer/NormalizerBase.php \Drupal\serialization\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

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 normalization without a format. 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

Title Sort descending Modifiers Object type Summary Overriden Title 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. 22
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 22
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 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.