"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/serialization/src/Normalizer/PrimitiveDataNormalizer.php" (26 Nov 2020, 1349 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "PrimitiveDataNormalizer.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\serialization\Normalizer;
    4 
    5 use Drupal\Core\Field\FieldItemInterface;
    6 use Drupal\Core\TypedData\PrimitiveInterface;
    7 
    8 /**
    9  * Converts primitive data objects to their casted values.
   10  */
   11 class PrimitiveDataNormalizer extends NormalizerBase {
   12 
   13   use SerializedColumnNormalizerTrait;
   14 
   15   /**
   16    * {@inheritdoc}
   17    */
   18   protected $supportedInterfaceOrClass = PrimitiveInterface::class;
   19 
   20   /**
   21    * {@inheritdoc}
   22    */
   23   public function normalize($object, $format = NULL, array $context = []) {
   24     $parent = $object->getParent();
   25     if ($parent instanceof FieldItemInterface && $object->getValue()) {
   26       $serialized_property_names = $this->getCustomSerializedPropertyNames($parent);
   27       if (in_array($object->getName(), $serialized_property_names, TRUE)) {
   28         return unserialize($object->getValue());
   29       }
   30     }
   31 
   32     // Typed data casts NULL objects to their empty variants, so for example
   33     // the empty string ('') for string type data, or 0 for integer typed data.
   34     // In a better world with typed data implementing algebraic data types,
   35     // getCastedValue would return NULL, but as typed data is not aware of real
   36     // optional values on the primitive level, we implement our own optional
   37     // value normalization here.
   38     return $object->getValue() === NULL ? NULL : $object->getCastedValue();
   39   }
   40 
   41 }