1<?php 2 3namespace Drupal\serialization\Normalizer; 4 5use Drupal\Core\TypedData\ComplexDataInterface; 6use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper; 7 8/** 9 * Converts the Drupal entity object structures to a normalized array. 10 * 11 * This is the default Normalizer for entities. All formats that have Encoders 12 * registered with the Serializer in the DIC will be normalized with this 13 * class unless another Normalizer is registered which supersedes it. If a 14 * module wants to use format-specific or class-specific normalization, then 15 * that module can register a new Normalizer and give it a higher priority than 16 * this one. 17 */ 18class ComplexDataNormalizer extends NormalizerBase { 19 20 /** 21 * {@inheritdoc} 22 */ 23 protected $supportedInterfaceOrClass = ComplexDataInterface::class; 24 25 /** 26 * {@inheritdoc} 27 */ 28 public function normalize($object, $format = NULL, array $context = []) { 29 $attributes = []; 30 // $object will not always match $supportedInterfaceOrClass. 31 // @see \Drupal\serialization\Normalizer\EntityNormalizer 32 // Other normalizers that extend this class may only provide $object that 33 // implements \Traversable. 34 if ($object instanceof ComplexDataInterface) { 35 // If there are no properties to normalize, just normalize the value. 36 $object = !empty($object->getProperties(TRUE)) 37 ? TypedDataInternalPropertiesHelper::getNonInternalProperties($object) 38 : $object->getValue(); 39 } 40 /** @var \Drupal\Core\TypedData\TypedDataInterface $property */ 41 foreach ($object as $name => $property) { 42 $attributes[$name] = $this->serializer->normalize($property, $format, $context); 43 } 44 return $attributes; 45 } 46 47} 48