Browse Source

Improved the stripNesting method
Added changes requested by ADmad

lilHermit 8 years ago
parent
commit
a33a03553e
2 changed files with 13 additions and 9 deletions
  1. 7 7
      src/View/Form/ArrayContext.php
  2. 6 2
      tests/TestCase/View/Form/ArrayContextTest.php

+ 7 - 7
src/View/Form/ArrayContext.php

@@ -180,9 +180,9 @@ class ArrayContext implements ContextInterface
         // Using Hash::check here incase the default value is actually null
         if (Hash::check($this->_context['defaults'], $field)) {
             return Hash::get($this->_context['defaults'], $field);
-        } else {
-            return Hash::get($this->_context['defaults'], $this->stripNesting($field));
         }
+
+        return Hash::get($this->_context['defaults'], $this->stripNesting($field));
     }
 
     /**
@@ -290,15 +290,15 @@ class ArrayContext implements ContextInterface
     }
 
     /**
-     * Strips out any numeric nesting like users.0.age
+     * Strips out any numeric nesting
+     *
+     * For example users.0.age will output as users.age
      *
-     * @param string $field A dot separated path to check errors on
+     * @param string $field A dot separated path
      * @return string A string with stripped numeric nesting
      */
     protected function stripNesting($field)
     {
-        return implode('.', array_filter(explode('.', $field), function ($val) {
-            return !is_numeric($val);
-        }));
+        return preg_replace('/\.\d\./', '.', $field);
     }
 }

+ 6 - 2
tests/TestCase/View/Form/ArrayContextTest.php

@@ -169,6 +169,9 @@ class ArrayContextTest extends TestCase
     /**
      * Test getting default value
      *
+     * Tests includes making sure numeric elements are stripped but not keys beginning with numeric
+     * value
+     *
      * @return void
      */
     public function testValDefault()
@@ -176,12 +179,13 @@ class ArrayContextTest extends TestCase
         $context = new ArrayContext($this->request, [
             'defaults' => [
                 'title' => 'Default value',
-                'users' => ['tags' => 'common']
+                'users' => ['tags' => 'common1', '9tags' => 'common2']
             ]
         ]);
 
         $this->assertEquals('Default value', $context->val('title'));
-        $this->assertEquals('common', $context->val('users.0.tags'));
+        $this->assertEquals('common1', $context->val('users.0.tags'));
+        $this->assertEquals('common2', $context->val('users.0.9tags'));
         $result = $context->val('title', ['default' => 'explicit default']);
         $this->assertEquals('explicit default', $result);
     }