Version 1
[yaffs-website] / web / core / modules / serialization / src / Normalizer / ListNormalizer.php
1 <?php
2
3 namespace Drupal\serialization\Normalizer;
4
5 /**
6  * Converts list objects to arrays.
7  *
8  * Ordinarily, this would be handled automatically by Serializer, but since
9  * there is a TypedDataNormalizer and the Field class extends TypedData, any
10  * Field will be handled by that Normalizer instead of being traversed. This
11  * class ensures that TypedData classes that also implement ListInterface are
12  * traversed instead of simply returning getValue().
13  */
14 class ListNormalizer extends NormalizerBase {
15
16   /**
17    * The interface or class that this Normalizer supports.
18    *
19    * @var string
20    */
21   protected $supportedInterfaceOrClass = 'Drupal\Core\TypedData\ListInterface';
22
23   /**
24    * {@inheritdoc}
25    */
26   public function normalize($object, $format = NULL, array $context = []) {
27     $attributes = [];
28     foreach ($object as $fieldItem) {
29       $attributes[] = $this->serializer->normalize($fieldItem, $format, $context);
30     }
31     return $attributes;
32   }
33
34 }