cd14cc014dd828a141a4f25e65b96836f822ae82
[yaffs-website] / web / core / modules / serialization / src / Normalizer / ComplexDataNormalizer.php
1 <?php
2
3 namespace Drupal\serialization\Normalizer;
4
5 use Drupal\Core\TypedData\ComplexDataInterface;
6 use 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  */
18 class ComplexDataNormalizer extends NormalizerBase {
19
20   /**
21    * The interface or class that this Normalizer supports.
22    *
23    * @var string
24    */
25   protected $supportedInterfaceOrClass = 'Drupal\Core\TypedData\ComplexDataInterface';
26
27   /**
28    * {@inheritdoc}
29    */
30   public function normalize($object, $format = NULL, array $context = []) {
31     $attributes = [];
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();
41     }
42     /** @var \Drupal\Core\TypedData\TypedDataInterface $property */
43     foreach ($object as $name => $property) {
44       $attributes[$name] = $this->serializer->normalize($property, $format, $context);
45     }
46     return $attributes;
47   }
48
49 }