Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Serializer.php

function Serializer::getNormalizer

Returns a matching normalizer.

Parameters

mixed $data Data to get the serializer for:

string|null $format Format name, present to give the option to normalizers to act differently based on formats:

array $context Options available to the normalizer:

2 calls to Serializer::getNormalizer()
Serializer::normalize in vendor/symfony/serializer/Serializer.php
Normalizes data into a set of arrays/scalars.
Serializer::supportsNormalization in vendor/symfony/serializer/Serializer.php
Checks whether the given class is supported for normalization by this normalizer.

File

vendor/symfony/serializer/Serializer.php, line 263

Class

Serializer
Serializer serializes and deserializes data.

Namespace

Symfony\Component\Serializer

Code

private function getNormalizer(mixed $data, ?string $format, array $context) : ?NormalizerInterface {
    if (\is_object($data)) {
        $type = $data::class;
        $genericType = 'object';
    }
    else {
        $type = 'native-' . \gettype($data);
        $genericType = '*';
    }
    if (!isset($this->normalizerCache[$format][$type])) {
        $this->normalizerCache[$format][$type] = [];
        foreach ($this->normalizers as $k => $normalizer) {
            if (!$normalizer instanceof NormalizerInterface) {
                continue;
            }
            $supportedTypes = $normalizer->getSupportedTypes($format);
            foreach ($supportedTypes as $supportedType => $isCacheable) {
                if (\in_array($supportedType, [
                    '*',
                    'object',
                ], true) || $type !== $supportedType && ('object' !== $genericType || !is_subclass_of($type, $supportedType))) {
                    continue;
                }
                if (null === $isCacheable) {
                    unset($supportedTypes['*'], $supportedTypes['object']);
                }
                elseif ($this->normalizerCache[$format][$type][$k] = $isCacheable && $normalizer->supportsNormalization($data, $format, $context)) {
                    break 2;
                }
                break;
            }
            if (null === ($isCacheable = $supportedTypes[\array_key_exists($genericType, $supportedTypes) ? $genericType : '*'] ?? null)) {
                continue;
            }
            if ($this->normalizerCache[$format][$type][$k] ??= $isCacheable && $normalizer->supportsNormalization($data, $format, $context)) {
                break;
            }
        }
    }
    foreach ($this->normalizerCache[$format][$type] as $k => $cached) {
        $normalizer = $this->normalizers[$k];
        if ($cached || $normalizer->supportsNormalization($data, $format, $context)) {
            return $normalizer;
        }
    }
    return null;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal