Просмотр исходного кода

Merge remote branch 'schmittjoh/parameterBagEnhancement'

* schmittjoh/parameterBagEnhancement:
  [HttpFoundation] added some unit tests
  [HttpFoundation] allow to retrieve paths of arbitrary depths
Fabien Potencier 14 лет назад
Родитель
Сommit
94eed37a93

+ 55 - 0
src/Symfony/Component/HttpFoundation/ParameterBag.php

@@ -81,6 +81,61 @@ class ParameterBag
         return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
     }
 
+    /**
+     * Returns a parameter by name allowing to specify a path with arbitrary depth.
+     *
+     * @param string $path   The path, e.g. foo[bar]
+     * @param mixed $default The default value
+     */
+    public function getDeep($path, $default = null)
+    {
+        if (false === $pos = strpos($path, '[')) {
+            return $this->get($path, $default);
+        }
+
+        $root = substr($path, 0, $pos);
+        if (!array_key_exists($root, $this->parameters)) {
+            return $default;
+        }
+
+        $value = $this->parameters[$root];
+        $currentKey = null;
+        for ($i=$pos,$c=strlen($path); $i<$c; $i++) {
+            $char = $path[$i];
+
+            if ('[' === $char) {
+                if (null !== $currentKey) {
+                    throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
+                }
+
+                $currentKey = '';
+            } else if (']' === $char) {
+                if (null === $currentKey) {
+                    throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
+                }
+
+                if (!is_array($value) || !array_key_exists($currentKey, $value)) {
+                    return $default;
+                }
+
+                $value = $value[$currentKey];
+                $currentKey = null;
+            } else {
+                if (null === $currentKey) {
+                    throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
+                }
+
+                $currentKey .= $char;
+            }
+        }
+
+        if (null !== $currentKey) {
+            throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
+        }
+
+        return $value;
+    }
+
     /**
      * Sets a parameter by name.
      *

+ 35 - 0
tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php

@@ -56,6 +56,41 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
         $this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
     }
 
+    /**
+     * @dataProvider getInvalidPaths
+     * @expectedException \InvalidArgumentException
+     * @covers Symfony\Component\HttpFoundation\ParameterBag::getDeep
+     */
+    public function testGetDeepWithInvalidPaths($path)
+    {
+        $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
+
+        $bag->getDeep($path);
+    }
+
+    public function getInvalidPaths()
+    {
+        return array(
+            array('foo[['),
+            array('foo[d'),
+            array('foo[bar]]'),
+            array('foo[bar]d'),
+        );
+    }
+
+    /**
+	 * @covers Symfony\Component\HttpFoundation\ParameterBag::getDeep
+     */
+    public function testGetDeep()
+    {
+        $bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));
+
+        $this->assertEquals(array('moo' => 'boo'), $bag->getDeep('foo[bar]'));
+        $this->assertEquals('boo', $bag->getDeep('foo[bar][moo]'));
+        $this->assertEquals('default', $bag->getDeep('foo[bar][foo]', 'default'));
+        $this->assertEquals('default', $bag->getDeep('bar[moo][foo]', 'default'));
+    }
+
     /**
      * @covers Symfony\Component\HttpFoundation\ParameterBag::set
      */