You are here

public function Serializer::normalize in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/serializer/Serializer.php \Symfony\Component\Serializer\Serializer::normalize()

Normalizes an object into a set of arrays/scalars.

Parameters

object $object object to normalize:

string $format format the normalization result will be encoded as:

array $context Context options for the normalizer:

Return value

array|string|bool|int|float|null

Overrides NormalizerInterface::normalize

1 call to Serializer::normalize()
Serializer::serialize in vendor/symfony/serializer/Serializer.php
Serializes data in the appropriate format.

File

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

Class

Serializer
Serializer serializes and deserializes data.

Namespace

Symfony\Component\Serializer

Code

public function normalize($data, $format = null, array $context = array()) {

  // If a normalizer supports the given data, use it
  if ($normalizer = $this
    ->getNormalizer($data, $format)) {
    return $normalizer
      ->normalize($data, $format, $context);
  }
  if (null === $data || is_scalar($data)) {
    return $data;
  }
  if (is_object($data) && $this
    ->supportsNormalization($data, $format)) {
    return $this
      ->normalizeObject($data, $format, $context);
  }
  if ($data instanceof \Traversable) {
    $normalized = array();
    foreach ($data as $key => $val) {
      $normalized[$key] = $this
        ->normalize($val, $format, $context);
    }
    return $normalized;
  }
  if (is_object($data)) {
    return $this
      ->normalizeObject($data, $format, $context);
  }
  if (is_array($data)) {
    foreach ($data as $key => $val) {
      $data[$key] = $this
        ->normalize($val, $format, $context);
    }
    return $data;
  }
  throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
}