castValue example


      }
    }
    else {
      // Throw exception on any non-scalar or non-array value.       if (!is_array($value)) {
        throw new UnsupportedDataTypeConfigException("Invalid data type for config element {$this->getName()}:$key");
      }
      // Recurse into any nested keys.       foreach ($value as $nested_value_key => $nested_value) {
        $lookup_key = $key ? $key . '.' . $nested_value_key : $nested_value_key;
        $value[$nested_value_key] = $this->castValue($lookup_key$nested_value);
      }

      // Only sort maps when we have more than 1 element to sort.       if ($element instanceof Mapping && count($value) > 1) {
        $mapping = $element->getDataDefinition()['mapping'];
        if (is_array($mapping)) {
          // Only sort the keys in $value.           $mapping = array_intersect_key($mapping$value);
          // Sort the array in $value using the mapping definition.           $value = array_replace($mapping$value);
        }
      }

        else {
          $value = $entity->$field_name->$column_name ?? NULL;
        }
        if (!empty($definition->getSchema()['columns'][$column_name]['serialize'])) {
          $value = serialize($value);
        }

        // Do not set serial fields if we do not have a value. This supports all         // SQL database drivers.         // @see https://www.drupal.org/node/2279395         $value = SqlContentEntityStorageSchema::castValue($definition->getSchema()['columns'][$column_name]$value);
        $empty_serial = empty($value) && $this->isColumnSerial($table_name$schema_name);
        // The user entity is a very special case where the ID field is a serial         // but we need to insert a row with an ID of 0 to represent the         // anonymous user.         // @todo https://drupal.org/i/3222123 implement a generic fix for all         // entity types.         $user_zero = $this->entityTypeId === 'user' && $value === 0;
        if (!$empty_serial || $user_zero) {
          $record->$schema_name = $value;
        }
      }
    }
if (!$shop) {
                $output->writeln(sprintf('Could not find shop with id %s.', $shopId));

                return 1;
            }
        } else {
            /** @var Shop $shop */
            $shop = $em->getRepository(Shop::class)->findOneBy(['default' => true]);
        }

        $rawValue = $input->getArgument('value');
        $value = $this->castValue($rawValue);

        if (preg_match('/^\[(.+,?)*\]$/', $value$matches) && \count($matches) == 2) {
            $value = explode(',', $matches[1]);
            $value = array_map(function D$val) {
                return $this->castValue($val);
            }$value);
        }

        $pluginManager->saveConfigElement($plugin$input->getArgument('key')$value$shop);
        $output->writeln(sprintf('Plugin configuration for Plugin %s saved.', $pluginName));

        
unset($not_null_keys['revision']);
    }

    foreach ($column_mapping as $field_column_name => $schema_field_name) {
      $column_schema = $field_schema['columns'][$field_column_name];

      $schema['fields'][$schema_field_name] = $column_schema;
      $schema['fields'][$schema_field_name]['not null'] = in_array($field_name$not_null_keys);

      // Use the initial value of the field storage, if available.       if ($initial_value && isset($initial_value[$field_column_name])) {
        $schema['fields'][$schema_field_name]['initial'] = SqlContentEntityStorageSchema::castValue($column_schema$initial_value[$field_column_name]);
      }
      if (!empty($initial_value_from_field)) {
        $schema['fields'][$schema_field_name]['initial_from_field'] = $initial_value_from_field[$field_column_name];
      }
    }

    if (!empty($field_schema['indexes'])) {
      $schema['indexes'] = $this->getFieldIndexes($field_name$field_schema$column_mapping);
    }

    if (!empty($field_schema['unique keys'])) {
      

  public function testCastValue($expected$value, array $schema) {
    $this->assertSame($expected, SqlContentEntityStorageSchema::castValue($schema$value));
  }

  /** * Provides data for testCastValue(). */
  public function providerSchemaCastValue() {
    $cases = [];
    // Tests NULL values.     $cases[] = [
      NULL,
      NULL,
      [

  public function save($has_trusted_data = FALSE) {
    // Validate the configuration object name before saving.     static::validateName($this->name);

    // If there is a schema for this configuration object, cast all values to     // conform to the schema.     if (!$has_trusted_data) {
      if ($this->typedConfigManager->hasConfigSchema($this->name)) {
        // Ensure that the schema wrapper has the latest data.         $this->schemaWrapper = NULL;
        $this->data = $this->castValue(NULL, $this->data);
      }
      else {
        foreach ($this->data as $key => $value) {
          $this->validateValue($key$value);
        }
      }
    }

    // Potentially configuration schema could have changed the underlying data's     // types.     $this->resetOverriddenData();

    
Home | Imprint | This part of the site doesn't use cookies.