setKeyAttribute() public méthode

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.
public setKeyAttribute ( string $attribute, boolean $remove = true )
$attribute string The name of the attribute which value is to be used as a key
$remove boolean Whether or not to remove the key
 /**
  * Tests the opposite of the testMappedAttributeKeyIsRemoved because
  * the removal can be toggled with an option.
  */
 public function testMappedAttributeKeyNotRemoved()
 {
     $node = new PrototypedArrayNode('root');
     $node->setKeyAttribute('id', false);
     // each item under the root is an array, with two scalar items
     $prototype = new ArrayNode(null, $node);
     $prototype->addChild(new ScalarNode('foo'));
     $prototype->addChild(new ScalarNode('id'));
     // the key attribute will remain
     $node->setPrototype($prototype);
     $children = array();
     $children[] = array('id' => 'item_name', 'foo' => 'bar');
     $normalized = $node->normalize($children);
     $expected = array();
     $expected['item_name'] = array('id' => 'item_name', 'foo' => 'bar');
     $this->assertEquals($expected, $normalized);
 }
Exemple #2
0
 /**
  * {@inheritDoc}
  */
 protected function createNode()
 {
     if (null == $this->prototype) {
         $node = new ArrayNode($this->name, $this->parent);
     } else {
         $node = new PrototypedArrayNode($this->name, $this->parent);
     }
     $node->setAddIfNotSet($this->addDefaults);
     $node->setAllowNewKeys($this->allowNewKeys);
     $node->addEquivalentValue(null, $this->nullEquivalent);
     $node->addEquivalentValue(true, $this->trueEquivalent);
     $node->addEquivalentValue(false, $this->falseEquivalent);
     $node->setPerformDeepMerging($this->performDeepMerging);
     $node->setRequired($this->required);
     $node->setIgnoreExtraKeys($this->ignoreExtraKeys);
     if (null !== $this->normalization) {
         $node->setNormalizationClosures($this->normalization->before);
         $node->setXmlRemappings($this->normalization->remappings);
     }
     if (null !== $this->merge) {
         $node->setAllowOverwrite($this->merge->allowOverwrite);
         $node->setAllowFalse($this->merge->allowFalse);
     }
     if (null !== $this->validation) {
         $node->setFinalValidationClosures($this->validation->rules);
     }
     if (null == $this->prototype) {
         foreach ($this->children as $child) {
             $child->parent = $node;
             $node->addChild($child->getNode());
         }
     } else {
         if (null !== $this->key) {
             $node->setKeyAttribute($this->key, $this->removeKeyItem);
         }
         if (true === $this->atLeastOne) {
             $node->setMinNumberOfElements(1);
         }
         if (null !== $this->defaultValue) {
             $node->setDefaultValue($this->defaultValue);
         }
         $this->prototype->parent = $node;
         $node->setPrototype($this->prototype->getNode());
     }
     return $node;
 }
 /**
  * Tests that when a key attribute is mapped, that key is removed from the array.
  * And if only 'value' element is left in the array, it will replace its wrapper array.
  *
  *     <things>
  *         <option id="option1" value="value1">
  *     </things>
  *
  * The above should finally be mapped to an array that looks like this
  * (because "id" is the key attribute).
  *
  *     array(
  *         'things' => array(
  *             'option1' => 'value1'
  *         )
  *     )
  *
  * It's also possible to mix 'value-only' and 'non-value-only' elements in the array.
  *
  * <things>
  *     <option id="option1" value="value1">
  *     <option id="option2" value="value2" foo="foo2">
  * </things>
  *
  * The above should finally be mapped to an array as follows
  *
  * array(
  *     'things' => array(
  *         'option1' => 'value1',
  *         'option2' => array(
  *             'value' => 'value2',
  *             'foo' => 'foo2'
  *         )
  *     )
  * )
  *
  * The 'value' element can also be ArrayNode:
  *
  * <things>
  *     <option id="option1">
  *         <value>
  *            <foo>foo1</foo>
  *            <bar>bar1</bar>
  *         </value>
  *     </option>
  * </things>
  *
  * The above should be finally be mapped to an array as follows
  *
  * array(
  *     'things' => array(
  *         'option1' => array(
  *             'foo' => 'foo1',
  *             'bar' => 'bar1'
  *         )
  *     )
  * )
  *
  * If using VariableNode for value node, it's also possible to mix different types of value nodes:
  *
  * <things>
  *     <option id="option1">
  *         <value>
  *            <foo>foo1</foo>
  *            <bar>bar1</bar>
  *         </value>
  *     </option>
  *     <option id="option2" value="value2">
  * </things>
  *
  * The above should be finally mapped to an array as follows
  *
  * array(
  *     'things' => array(
  *         'option1' => array(
  *             'foo' => 'foo1',
  *             'bar' => 'bar1'
  *         ),
  *         'option2' => 'value2'
  *     )
  * )
  *
  *
  * @dataProvider getDataForKeyRemovedLeftValueOnly
  */
 public function testMappedAttributeKeyIsRemovedLeftValueOnly($value, $children, $expected)
 {
     $node = new PrototypedArrayNode('root');
     $node->setKeyAttribute('id', true);
     // each item under the root is an array, with one scalar item
     $prototype = new ArrayNode(null, $node);
     $prototype->addChild(new ScalarNode('id'));
     $prototype->addChild(new ScalarNode('foo'));
     $prototype->addChild($value);
     $node->setPrototype($prototype);
     $normalized = $node->normalize($children);
     $this->assertEquals($expected, $normalized);
 }