Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[Config] Numerical keys for prot. arrays if useAttributeAsKey is set

  • Loading branch information...
commit b7fc009316511170b395c75d98b3348fe336a52a 1 parent 46ffbd5
Alexander authored May 14, 2012
3  src/Symfony/Component/Config/Definition/PrototypedArrayNode.php
@@ -242,10 +242,11 @@ protected function normalizeValue($value)
242 242
 
243 243
         $value = $this->remapXml($value);
244 244
 
  245
+        $isAssoc = array_keys($value) === range(0, count($value) -1);
245 246
         $normalized = array();
246 247
         foreach ($value as $k => $v) {
247 248
             if (null !== $this->keyAttribute && is_array($v)) {
248  
-                if (!isset($v[$this->keyAttribute]) && is_int($k)) {
  249
+                if (!isset($v[$this->keyAttribute]) && is_int($k) && $isAssoc) {
249 250
                     $msg = sprintf('The attribute "%s" must be set for path "%s".', $this->keyAttribute, $this->getPath());
250 251
                     $ex = new InvalidConfigurationException($msg);
251 252
                     $ex->setPath($this->getPath());
66  src/Symfony/Component/Config/Tests/Definition/NormalizationTest.php
@@ -137,8 +137,74 @@ public function getAnonymousKeysTests()
137 137
         return array_map(function($v) { return array($v); }, $configs);
138 138
     }
139 139
 
  140
+    /**
  141
+     * @dataProvider getNumericKeysTests
  142
+     */
  143
+    public function testNumericKeysAsAttributes($denormalized)
  144
+    {
  145
+        $normalized = array(
  146
+            'thing' => array(42 => array('foo', 'bar'), 1337 => array('baz', 'qux')),
  147
+        );
  148
+
  149
+        $this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, $normalized);
  150
+    }
  151
+
  152
+    public function getNumericKeysTests()
  153
+    {
  154
+        $configs = array();
  155
+
  156
+        $configs[] = array(
  157
+            'thing' => array(
  158
+                42 => array('foo', 'bar'), 1337 => array('baz', 'qux'),
  159
+            ),
  160
+        );
  161
+
  162
+        $configs[] = array(
  163
+            'thing' => array(
  164
+                array('foo', 'bar', 'id' => 42), array('baz', 'qux', 'id' => 1337),
  165
+            ),
  166
+        );
  167
+
  168
+        return array_map(function($v) { return array($v); }, $configs);
  169
+    }
  170
+
  171
+    /**
  172
+     * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  173
+     * @expectedExceptionMessage The attribute "id" must be set for path "root.thing".
  174
+     */
  175
+    public function testNonAssociativeArrayThrowsExceptionIfAttributeNotSet()
  176
+    {
  177
+        $denormalized = array(
  178
+            'thing' => array(
  179
+                array('foo', 'bar'), array('baz', 'qux')
  180
+            )
  181
+        );
  182
+
  183
+        $this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, array());
  184
+    }
  185
+
140 186
     public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
141 187
     {
142 188
         self::assertSame($normalized, $tree->normalize($denormalized));
143 189
     }
  190
+
  191
+    private function getNumericKeysTestTree()
  192
+    {
  193
+        $tb = new TreeBuilder();
  194
+        $tree = $tb
  195
+            ->root('root', 'array')
  196
+                ->children()
  197
+                    ->node('thing', 'array')
  198
+                        ->useAttributeAsKey('id')
  199
+                        ->prototype('array')
  200
+                            ->prototype('scalar')->end()
  201
+                        ->end()
  202
+                    ->end()
  203
+                ->end()
  204
+            ->end()
  205
+            ->buildTree()
  206
+        ;
  207
+
  208
+        return $tree;
  209
+    }
144 210
 }

0 notes on commit b7fc009

Please sign in to comment.
Something went wrong with that request. Please try again.