Jelajahi Sumber

[Config] Moving the removal of the key attribute on ArrayNode onto the setKeyAttribute() method per Johannes.

This is more consistent with how this was handled elsewhere, and it really is an "option" on the keyAttribute idea.
Ryan Weaver 14 tahun lalu
induk
melakukan
6d24d37b16

+ 8 - 20
src/Symfony/Component/Config/Definition/ArrayNode.php

@@ -87,22 +87,7 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
      * This is only relevant for XML configurations, and only in combination
      * with a prototype based node.
      *
-     * @param string $attribute
-     * @return void
-     */
-    public function setKeyAttribute($attribute)
-    {
-        $this->keyAttribute = $attribute;
-    }
-
-    /**
-     * Sets whether or not the key attribute should be removed from child items.
-     *
-     * If true (the default) and keyAttribute is set, then when a child item
-     * is remapped based off of the key attribute, the key attribute is removed
-     * from the item's value.
-     *
-     * In other words, if "id" is the keyAttribute, then:
+     * For example, if "id" is the keyAttribute, then:
      *
      *     array('id' => 'my_name', 'foo' => 'bar')
      *
@@ -110,13 +95,16 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
      *
      *     'id' => array('foo' => 'bar')
      *
-     * If false, the resulting array will still have the "'id' => 'my_name'"
-     * item in it.
+     * If $remove is false, the resulting array will still have the
+     * "'id' => 'my_name'" item in it.
      *
-     * @param Boolean $remove Whether or not the key attribute should be removed.
+     * @param string $attribute The name of the attribute to use as a key
+     * @param Boolean $remove Whether or not to remove the key
+     * @return void
      */
-    public function setRemoveKeyAttribute($remove)
+    public function setKeyAttribute($attribute, $remove = true)
     {
+        $this->keyAttribute = $attribute;
         $this->removeKeyAttribute = $remove;
     }
 

+ 1 - 2
src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php

@@ -166,8 +166,7 @@ class TreeBuilder
         $configNode->setPreventExtraKeys($node->preventExtraKeys);
 
         if (null !== $node->key) {
-            $configNode->setKeyAttribute($node->key);
-            $configNode->setRemoveKeyAttribute($node->removeKeyItem);
+            $configNode->setKeyAttribute($node->key, $node->removeKeyItem);
         }
 
         if (true === $node->atLeastOne) {

+ 2 - 3
tests/Symfony/Tests/Component/Config/Definition/ArrayNodeTest.php

@@ -106,7 +106,7 @@ class ArrayNodeTest extends \PHPUnit_Framework_TestCase
     public function testMappedAttributeKeyIsRemoved()
     {
         $node = new ArrayNode('root');
-        $node->setKeyAttribute('id');
+        $node->setKeyAttribute('id', true);
 
         $prototype = new ArrayNode(null);
         $prototype->setPreventExtraKeys(false); // just so it allows anything
@@ -128,8 +128,7 @@ class ArrayNodeTest extends \PHPUnit_Framework_TestCase
     public function testMappedAttributeKeyNotRemoved()
     {
         $node = new ArrayNode('root');
-        $node->setKeyAttribute('id');
-        $node->setRemoveKeyAttribute(false);
+        $node->setKeyAttribute('id', false);
 
         $prototype = new ArrayNode(null);
         $prototype->setPreventExtraKeys(false); // just so it allows anything