Version 1
[yaffs-website] / web / core / modules / serialization / src / Normalizer / PrimitiveDataNormalizer.php
1 <?php
2
3 namespace Drupal\serialization\Normalizer;
4
5 use Drupal\Core\TypedData\PrimitiveInterface;
6
7 /**
8  * Converts primitive data objects to their casted values.
9  */
10 class PrimitiveDataNormalizer extends NormalizerBase {
11
12   /**
13    * The interface or class that this Normalizer supports.
14    *
15    * @var string
16    */
17   protected $supportedInterfaceOrClass = PrimitiveInterface::class;
18
19   /**
20    * {@inheritdoc}
21    */
22   public function normalize($object, $format = NULL, array $context = []) {
23     // Typed data casts NULL objects to their empty variants, so for example
24     // the empty string ('') for string type data, or 0 for integer typed data.
25     // In a better world with typed data implementing algebraic data types,
26     // getCastedValue would return NULL, but as typed data is not aware of real
27     // optional values on the primitive level, we implement our own optional
28     // value normalization here.
29     return $object->getValue() === NULL ? NULL : $object->getCastedValue();
30   }
31
32 }