class ComplexDataNormalizer

Same name and namespace in other branches
  1. 11.x core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php \Drupal\serialization\Normalizer\ComplexDataNormalizer

Converts the Drupal entity object structures to a normalized array.

This is the default Normalizer for entities. All formats that have Encoders registered with the Serializer in the DIC will be normalized with this class unless another Normalizer is registered which supersedes it. If a module wants to use format-specific or class-specific normalization, then that module can register a new Normalizer and give it a higher priority than this one.

Hierarchy

Expanded class hierarchy of ComplexDataNormalizer

1 file declares its use of ComplexDataNormalizer
ComplexDataNormalizerTest.php in core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php
Contains \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest.
1 string reference to 'ComplexDataNormalizer'
serialization.services.yml in core/modules/serialization/serialization.services.yml
core/modules/serialization/serialization.services.yml
1 service uses ComplexDataNormalizer
serializer.normalizer.complex_data in core/modules/serialization/serialization.services.yml
Drupal\serialization\Normalizer\ComplexDataNormalizer

File

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

Namespace

Drupal\serialization\Normalizer
View source
class ComplexDataNormalizer extends NormalizerBase {
  
  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = ComplexDataInterface::class;
  
  /**
   * {@inheritdoc}
   */
  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;
  }
  
  /**
   * {@inheritdoc}
   */
  public function hasCacheableSupportsMethod() : bool {
    return TRUE;
  }

}

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