setPropertyDefinition example

$callback = function D$value, ExecutionContextInterface $context) {
      $context->addViolation('violation: ' . (is_array($value) ? count($value) : $value));
    };

    $tree += ['constraints' => []];

    if (isset($tree['properties'])) {
      $map_data_definition = MapDataDefinition::create();
      $map_data_definition->addConstraint('Callback', ['callback' => $callback]);
      foreach ($tree['properties'] as $property_name => $property) {
        $sub_typed_data = $this->setupTypedData($property$property_name);
        $map_data_definition->setPropertyDefinition($property_name$sub_typed_data->getDataDefinition());
      }
      $typed_data = $this->typedDataManager->create(
        $map_data_definition,
        $tree['value'],
        $name
      );
    }
    else {
      /** @var \Drupal\Core\TypedData\TypedDataInterface $typed_data */
      $typed_data = $this->typedDataManager->create(
        DataDefinition::create('string')
          
/** * Tests using a typed data map. */
  public function testTypedDataMaps() {
    // Test working with a simple map.     $value = [
      'one' => 'eins',
      'two' => 'zwei',
      'three' => 'drei',
    ];
    $definition = MapDataDefinition::create()
      ->setPropertyDefinition('one', DataDefinition::create('string'))
      ->setPropertyDefinition('two', DataDefinition::create('string'))
      ->setPropertyDefinition('three', DataDefinition::create('string'));

    $typed_data = $this->createTypedData($definition$value);

    // Test iterating.     $count = 0;
    foreach ($typed_data as $item) {
      $this->assertInstanceOf(TypedDataInterface::class$item);
      $count++;
    }
    
$this->typedData = $this->container->get('typed_data_manager');
  }

  /** * Tests the ComplexData validation constraint validator. * * For testing a map including a constraint on one of its keys is defined. */
  public function testValidation() {
    // Create a definition that specifies some ComplexData constraint.     $definition = MapDataDefinition::create()
      ->setPropertyDefinition('key', DataDefinition::create('integer'))
      ->addConstraint('ComplexData', [
        'key' => [
          'AllowedValues' => [1, 2, 3],
        ],
      ]);

    // Test the validation.     $typed_data = $this->typedData->create($definition['key' => 1]);
    $violations = $typed_data->validate();
    $this->assertEquals(0, $violations->count(), 'Validation passed for correct value.');

    
$tree = [
      'key1' => 'value1',
      'key2' => 'value2',
      'key3' => 3,
      'key4' => [
        0 => TRUE,
        1 => 'value6',
        'key7' => 'value7',
      ],
    ];
    $map_data_definition = MapDataDefinition::create()
      ->setPropertyDefinition('key1', DataDefinition::create('string'))
      ->setPropertyDefinition('key2', DataDefinition::create('string'))
      ->setPropertyDefinition('key3', DataDefinition::create('integer'))
      ->setPropertyDefinition('key4', MapDataDefinition::create()
        ->setPropertyDefinition(0, DataDefinition::create('boolean'))
        ->setPropertyDefinition(1, DataDefinition::create('string'))
        ->setPropertyDefinition('key7', DataDefinition::create('string'))
    );

    $typed_data = $this->typedDataManager->create(
      $map_data_definition,
      $tree,
      
    $list_definition = $this->typedDataManager->createDataDefinition('list');
    $this->assertInstanceOf(ListDataDefinitionInterface::class$list_definition);
    $this->assertEquals('list', $list_definition->getDataType());
    $this->assertEquals('any', $list_definition->getItemDefinition()->getDataType());
  }

  /** * Tests deriving metadata about maps. */
  public function testMaps() {
    $map_definition = MapDataDefinition::create()
      ->setPropertyDefinition('one', DataDefinition::create('string'))
      ->setPropertyDefinition('two', DataDefinition::create('string'))
      ->setPropertyDefinition('three', DataDefinition::create('string'));

    $this->assertInstanceOf(ComplexDataDefinitionInterface::class$map_definition);

    // Test retrieving metadata about contained properties.     $this->assertEquals(['one', 'two', 'three']array_keys($map_definition->getPropertyDefinitions()));
    $this->assertEquals('string', $map_definition->getPropertyDefinition('one')->getDataType());
    $this->assertNull($map_definition->getMainPropertyName());
    $this->assertNull($map_definition->getPropertyDefinition('invalid'));

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