Prechádzať zdrojové kódy

[HttpFoundation] removed getDeep(), added a boolean flag to get() instead

Johannes Schmitt 14 rokov pred
rodič
commit
89f60e04d1

+ 26 - 32
src/Symfony/Component/HttpFoundation/ParameterBag.php

@@ -73,24 +73,14 @@ class ParameterBag
     /**
      * Returns a parameter by name.
      *
-     * @param string $key     The key
-     * @param mixed  $default The default value
+     * @param string  $key     The key
+     * @param mixed   $default The default value
+     * @param boolean $deep
      */
-    public function get($key, $default = null)
+    public function get($path, $default = null, $deep = false)
     {
-        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);
+        if (!$deep || false === $pos = strpos($path, '[')) {
+            return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
         }
 
         $root = substr($path, 0, $pos);
@@ -172,52 +162,56 @@ class ParameterBag
     /**
      * Returns the alphabetic characters of the parameter value.
      *
-     * @param string $key     The parameter key
-     * @param mixed  $default The default value
+     * @param string  $key     The parameter key
+     * @param mixed   $default The default value
+     * @param boolean $deep
      *
      * @return string The filtered value
      */
-    public function getAlpha($key, $default = '')
+    public function getAlpha($key, $default = '', $deep = false)
     {
-        return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
+        return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
     }
 
     /**
      * Returns the alphabetic characters and digits of the parameter value.
      *
-     * @param string $key     The parameter key
-     * @param mixed  $default The default value
+     * @param string  $key     The parameter key
+     * @param mixed   $default The default value
+     * @param boolean $deep
      *
      * @return string The filtered value
      */
-    public function getAlnum($key, $default = '')
+    public function getAlnum($key, $default = '', $deep = false)
     {
-        return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
+        return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
     }
 
     /**
      * Returns the digits of the parameter value.
      *
-     * @param string $key     The parameter key
-     * @param mixed  $default The default value
+     * @param string  $key     The parameter key
+     * @param mixed   $default The default value
+     * @param boolean $deep
      *
      * @return string The filtered value
      */
-    public function getDigits($key, $default = '')
+    public function getDigits($key, $default = '', $deep = false)
     {
-        return preg_replace('/[^[:digit:]]/', '', $this->get($key, $default));
+        return preg_replace('/[^[:digit:]]/', '', $this->get($key, $default, $deep));
     }
 
     /**
      * Returns the parameter value converted to integer.
      *
-     * @param string $key     The parameter key
-     * @param mixed  $default The default value
+     * @param string  $key     The parameter key
+     * @param mixed   $default The default value
+     * @param boolean $deep
      *
      * @return string The filtered value
      */
-    public function getInt($key, $default = 0)
+    public function getInt($key, $default = 0, $deep = false)
     {
-        return (int) $this->get($key, $default);
+        return (int) $this->get($key, $default, $deep);
     }
 }

+ 12 - 5
tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php

@@ -56,6 +56,13 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
         $this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
     }
 
+    public function testGetDoesNotUseDeepByDefault()
+    {
+        $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
+
+        $this->assertNull($bag->get('foo[bar]'));
+    }
+
     /**
      * @dataProvider getInvalidPaths
      * @expectedException \InvalidArgumentException
@@ -65,7 +72,7 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
     {
         $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
 
-        $bag->getDeep($path);
+        $bag->get($path, null, true);
     }
 
     public function getInvalidPaths()
@@ -85,10 +92,10 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
     {
         $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'));
+        $this->assertEquals(array('moo' => 'boo'), $bag->get('foo[bar]', null, true));
+        $this->assertEquals('boo', $bag->get('foo[bar][moo]', null, true));
+        $this->assertEquals('default', $bag->get('foo[bar][foo]', 'default', true));
+        $this->assertEquals('default', $bag->get('bar[moo][foo]', 'default', true));
     }
 
     /**