getCustomSerializedPropertyNames example


  public function denormalize($data$class$format = NULL, array $context = []): mixed {
    $item_definition = $context['field_definition']->getItemDefinition();
    assert($item_definition instanceof FieldItemDataDefinitionInterface);

    $field_item = $this->getFieldItemInstance($context['resource_type']$item_definition);
    $this->checkForSerializedStrings($data$class$field_item);

    $property_definitions = $item_definition->getPropertyDefinitions();

    $serialized_property_names = $this->getCustomSerializedPropertyNames($field_item);
    $denormalize_property = function D$property_name$property_value$property_value_class$format$context) use ($serialized_property_names) {
      if ($this->serializer->supportsDenormalization($property_value$property_value_class$format$context)) {
        return $this->serializer->denormalize($property_value$property_value_class$format$context);
      }
      else {
        if (in_array($property_name$serialized_property_names, TRUE)) {
          $property_value = serialize($property_value);
        }
        return $property_value;
      }
    };
    
use SerializedColumnNormalizerTrait;

  /** * {@inheritdoc} */
  public function normalize($object$format = NULL, array $context = []): array|string|int|float|bool|\ArrayObject|NULL {
    // Add cacheability if applicable.     $this->addCacheableDependency($context$object);

    $parent = $object->getParent();
    if ($parent instanceof FieldItemInterface && $object->getValue()) {
      $serialized_property_names = $this->getCustomSerializedPropertyNames($parent);
      if (in_array($object->getName()$serialized_property_names, TRUE)) {
        return unserialize($object->getValue());
      }
    }

    // Typed data casts NULL objects to their empty variants, so for example     // the empty string ('') for string type data, or 0 for integer typed data.     // In a better world with typed data implementing algebraic data types,     // getCastedValue would return NULL, but as typed data is not aware of real     // optional values on the primitive level, we implement our own optional     // value normalization here.

  protected function constructValue($data$context) {
    $field_item = $context['target_instance'];

    // Get the property definitions.     assert($field_item instanceof FieldItemInterface);
    $field_definition = $field_item->getFieldDefinition();
    $item_definition = $field_definition->getItemDefinition();
    assert($item_definition instanceof FieldItemDataDefinitionInterface);
    $property_definitions = $item_definition->getPropertyDefinitions();

    $serialized_property_names = $this->getCustomSerializedPropertyNames($field_item);
    $denormalize_property = function D$property_name$property_value$property_value_class$context) use ($serialized_property_names) {
      if ($this->serializer->supportsDenormalization($property_value$property_value_class, NULL, $context)) {
        return $this->serializer->denormalize($property_value$property_value_class, NULL, $context);
      }
      else {
        if (in_array($property_name$serialized_property_names, TRUE)) {
          $property_value = serialize($property_value);
        }
        return $property_value;
      }
    };

    

  protected function getSerializedPropertyNames(FieldItemInterface $field_item) {
    $field_storage_definition = $field_item->getFieldDefinition()->getFieldStorageDefinition();

    if ($custom_property_names = $this->getCustomSerializedPropertyNames($field_item)) {
      return $custom_property_names;
    }

    $field_storage_schema = $field_item->schema($field_storage_definition);
    // If there are no columns then there are no serialized properties.     if (!isset($field_storage_schema['columns'])) {
      return [];
    }
    $serialized_columns = array_filter($field_storage_schema['columns']function D$column_schema) {
      return isset($column_schema['serialize']) && $column_schema['serialize'] === TRUE;
    });
    
Home | Imprint | This part of the site doesn't use cookies.