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

Breadcrumb

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

function Serializer::normalize

Same name in this branch
  1. 11.1.x core/modules/jsonapi/src/Serializer/Serializer.php \Drupal\jsonapi\Serializer\Serializer::normalize()

Overrides NormalizerInterface::normalize

3 calls to Serializer::normalize()
Serializer::normalize in core/modules/jsonapi/src/Serializer/Serializer.php
Normalizes data into a set of arrays/scalars.
Serializer::normalize in core/modules/jsonapi/src/Serializer/Serializer.php
Normalizes data into a set of arrays/scalars.
Serializer::serialize in vendor/symfony/serializer/Serializer.php
Serializes data in the appropriate format.
1 method overrides Serializer::normalize()
Serializer::normalize in core/modules/jsonapi/src/Serializer/Serializer.php
Normalizes data into a set of arrays/scalars.

File

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

Class

Serializer
Serializer serializes and deserializes data.

Namespace

Symfony\Component\Serializer

Code

public function normalize(mixed $data, ?string $format = null, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
    // If a normalizer supports the given data, use it
    if ($normalizer = $this->getNormalizer($data, $format, $context)) {
        return $normalizer->normalize($data, $format, $context);
    }
    if (null === $data || \is_scalar($data)) {
        return $data;
    }
    if (\is_array($data) && !$data && ($context[self::EMPTY_ARRAY_AS_OBJECT] ?? false)) {
        return new \ArrayObject();
    }
    if (is_iterable($data)) {
        if ($data instanceof \Countable && ($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && !\count($data)) {
            return new \ArrayObject();
        }
        $normalized = [];
        foreach ($data as $key => $val) {
            $normalized[$key] = $this->normalize($val, $format, $context);
        }
        return $normalized;
    }
    if (\is_object($data)) {
        if (!$this->normalizers) {
            throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
        }
        throw new NotNormalizableValueException(\sprintf('Could not normalize object of type "%s", no supporting normalizer found.', get_debug_type($data)));
    }
    throw new NotNormalizableValueException('An unexpected value could not be normalized: ' . (!\is_resource($data) ? var_export($data, true) : \sprintf('"%s" resource', get_resource_type($data))));
}

API Navigation

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