3 namespace Drupal\serialization\Normalizer;
5 use Drupal\Core\TypedData\ComplexDataInterface;
6 use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper;
9 * Converts the Drupal entity object structures to a normalized array.
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
18 class ComplexDataNormalizer extends NormalizerBase {
21 * The interface or class that this Normalizer supports.
25 protected $supportedInterfaceOrClass = 'Drupal\Core\TypedData\ComplexDataInterface';
30 public function normalize($object, $format = NULL, array $context = []) {
32 // $object will not always match $supportedInterfaceOrClass.
33 // @see \Drupal\serialization\Normalizer\EntityNormalizer
34 // Other normalizers that extend this class may only provide $object that
35 // implements \Traversable.
36 if ($object instanceof ComplexDataInterface) {
37 // If there are no properties to normalize, just normalize the value.
38 $object = !empty($object->getProperties(TRUE))
39 ? TypedDataInternalPropertiesHelper::getNonInternalProperties($object)
40 : $object->getValue();
42 /** @var \Drupal\Core\TypedData\TypedDataInterface $property */
43 foreach ($object as $name => $property) {
44 $attributes[$name] = $this->serializer->normalize($property, $format, $context);