useAttributeAsKey() public method

This is useful when you have an indexed array that should be an associative array. You can select an item from within the array to be the key of the particular item. For example, if "id" is the "key", then: array( array('id' => 'my_name', 'foo' => 'bar'), ); becomes array( 'my_name' => array('foo' => 'bar'), ); If you'd like "'id' => 'my_name'" to still be present in the resulting array, then you can set the second argument of this method to false. This method is applicable to prototype nodes only.
public useAttributeAsKey ( string $name, boolean $removeKeyItem = true ) : ArrayNodeDefinition
$name string The name of the key
$removeKeyItem boolean Whether or not the key item should be removed
return ArrayNodeDefinition
Exemplo n.º 1
0
 /**
  * Create a configuration node to define available next states of a step (or a processs)
  *
  * @return ArrayNodeDefinition
  */
 private function createNextStatesNodeDefinition()
 {
     $flowTypes = array(NextStateInterface::TYPE_STEP, NextStateInterface::TYPE_STEP_OR, NextStateInterface::TYPE_PROCESS);
     $nextStatesNode = new ArrayNodeDefinition('next_states');
     $nextStatesNode->useAttributeAsKey('name')->prototype('array')->addDefaultsIfNotSet()->children()->scalarNode('type')->defaultValue('step')->validate()->ifNotInArray($flowTypes)->thenInvalid('Invalid next element type "%s". Please use one of the following types: ' . implode(', ', $flowTypes))->end()->end()->variableNode('target')->cannotBeEmpty()->end()->end()->end();
     return $nextStatesNode;
 }
Exemplo n.º 2
0
 /**
  * @param ArrayNodeDefinition $routesNode
  */
 protected function configureRoutesNodeDefinition(ArrayNodeDefinition $routesNode)
 {
     $self = $this;
     $routesNode->useAttributeAsKey(true)->beforeNormalization()->always(function (array $vendors) use($self) {
         $self->assertKeysAreValidVendorNames($vendors);
         return $vendors;
     })->end()->prototype('array')->children()->scalarNode('server')->end()->scalarNode('uri')->end()->scalarNode('link')->end()->end()->end();
 }
Exemplo n.º 3
0
 private function getPathsSection()
 {
     $node = new ArrayNodeDefinition('paths');
     $node->useAttributeAsKey('path')->normalizeKeys(false);
     /** @var ArrayNodeDefinition $prototype */
     $prototype = $node->prototype('array');
     $this->appendPrototypedConfiguration($prototype);
     return $node;
 }
Exemplo n.º 4
0
 /**
  * Builds configuration node by given filter name.
  *
  * @param string $filter
  *
  * @return ArrayNodeDefinition
  */
 private function buildFilterNode($filter)
 {
     $node = new ArrayNodeDefinition($filter);
     $children = $node->useAttributeAsKey('name')->beforeNormalization()->always(function ($a) {
         foreach ($a as $name => $v) {
             if ($name == 'pager') {
                 if (!isset($v['request_parameter'])) {
                     $a[$name]['request_parameter'] = 'page';
                 }
                 continue;
             }
             if (!is_array($v)) {
                 $a[$name] = ['field' => $v, 'request_parameter' => $name];
             } elseif (!isset($v['request_parameter'])) {
                 $a[$name]['request_parameter'] = $name;
             }
         }
         return $a;
     })->end()->prototype('array')->children()->scalarNode('name')->end()->scalarNode('request_parameter')->info('Request parameter name.')->end()->end();
     if ($filter != 'sort' && $filter != 'pager') {
         $children->children()->scalarNode('field')->info('Entity field name.')->isRequired()->end()->end();
     }
     switch ($filter) {
         case 'choice':
             $children->children()->arrayNode('choices')->useAttributeAsKey('value')->prototype('array')->beforeNormalization()->ifString()->then(function ($v) {
                 return ['name' => $v];
             })->end()->children()->scalarNode('value')->end()->scalarNode('name')->end()->end()->end()->end()->end();
             break;
         case 'sort':
             $children->children()->arrayNode('choices')->useAttributeAsKey('value')->beforeNormalization()->always(function ($a) {
                 foreach ($a as $name => $v) {
                     if (is_string($v)) {
                         $a[$name] = ['field' => $v, 'name' => $name];
                     } elseif (!isset($v['name'])) {
                         $a[$name]['name'] = $name;
                     }
                 }
                 return $a;
             })->end()->prototype('array')->children()->scalarNode('value')->end()->scalarNode('field')->isRequired()->end()->enumNode('order')->values(['asc', 'desc'])->defaultValue('asc')->end()->scalarNode('name')->end()->end()->end()->end()->end();
             break;
         case 'pager':
             $children->children()->integerNode('items_per_page')->defaultValue(10)->end()->end();
             break;
     }
     return $node;
 }
 /**
  * @dataProvider providePrototypedArrayNodeDefaults
  */
 public function testPrototypedArrayNodeDefault($args, $shouldThrowWhenUsingAttrAsKey, $shouldThrowWhenNotUsingAttrAsKey, $defaults)
 {
     $node = new ArrayNodeDefinition('root');
     $node->addDefaultChildrenIfNoneSet($args)->prototype('array');
     try {
         $tree = $node->getNode();
         $this->assertFalse($shouldThrowWhenNotUsingAttrAsKey);
         $this->assertEquals($defaults, $tree->getDefaultValue());
     } catch (InvalidDefinitionException $e) {
         $this->assertTrue($shouldThrowWhenNotUsingAttrAsKey);
     }
     $node = new ArrayNodeDefinition('root');
     $node->useAttributeAsKey('attr')->addDefaultChildrenIfNoneSet($args)->prototype('array');
     try {
         $tree = $node->getNode();
         $this->assertFalse($shouldThrowWhenUsingAttrAsKey);
         $this->assertEquals($defaults, $tree->getDefaultValue());
     } catch (InvalidDefinitionException $e) {
         $this->assertTrue($shouldThrowWhenUsingAttrAsKey);
     }
 }
Exemplo n.º 6
0
 public function createWorkflowDefinition()
 {
     $processesNode = new ArrayNodeDefinition('workflows');
     $processesNode->useAttributeAsKey('name')->prototype('array')->children()->scalarNode('class')->defaultNull()->end()->scalarNode('entity')->defaultNull()->end()->end()->end();
     return $processesNode;
 }
Exemplo n.º 7
0
 /**
  * Setups configuration for current extension.
  *
  * @param ArrayNodeDefinition $builder
  */
 public function getConfig(ArrayNodeDefinition $builder)
 {
     $builder->useAttributeAsKey('name')->prototype('variable');
 }
 /**
  * @return NodeDefinition
  */
 private function getQueueArgumentsConfiguration()
 {
     $node = new ArrayNodeDefinition('arguments');
     return $node->useAttributeAsKey('identifier')->beforeNormalization()->ifTrue(function ($arguments) {
         foreach ($arguments as $k => $v) {
             //Un-normalize keys
             if (false !== strpos($k, '_')) {
                 return true;
             }
         }
     })->then(function ($arguments) {
         foreach ($arguments as $k => $v) {
             if (false !== strpos($k, '_')) {
                 $arguments[str_replace('_', '-', $k)] = $v;
                 unset($arguments[$k]);
             }
         }
         return $arguments;
     })->end()->prototype('scalar')->end();
 }