function ComplexDataNormalizer::normalize

Same name and namespace in other branches
  1. 11.x core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php \Drupal\serialization\Normalizer\ComplexDataNormalizer::normalize()
1 method overrides ComplexDataNormalizer::normalize()
StringNormalizer::normalize in core/modules/jsonapi/tests/modules/jsonapi_test_field_type/src/Normalizer/StringNormalizer.php

File

core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php, line 28

Class

ComplexDataNormalizer
Converts the Drupal entity object structures to a normalized array.

Namespace

Drupal\serialization\Normalizer

Code

public function normalize($object, $format = NULL, array $context = []) {
  $attributes = [];
  // $object will not always match $supportedInterfaceOrClass.
  // @see \Drupal\serialization\Normalizer\EntityNormalizer
  // Other normalizers that extend this class may only provide $object that
  // implements \Traversable.
  if ($object instanceof ComplexDataInterface) {
    // If there are no properties to normalize, just normalize the value.
    $object = !empty($object->getProperties(TRUE)) ? TypedDataInternalPropertiesHelper::getNonInternalProperties($object) : $object->getValue();
  }
  /** @var \Drupal\Core\TypedData\TypedDataInterface $property */
  foreach ($object as $name => $property) {
    $attributes[$name] = $this->serializer
      ->normalize($property, $format, $context);
  }
  return $attributes;
}

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