Browse Source

Merge pull request #9353 from cakephp/3.x-query

Allow Request::query() to return all query strings.
Mark Story 9 years ago
parent
commit
bfd81176ef
2 changed files with 17 additions and 9 deletions
  1. 11 7
      src/Network/Request.php
  2. 6 2
      tests/TestCase/Network/RequestTest.php

+ 11 - 7
src/Network/Request.php

@@ -584,9 +584,9 @@ class Request implements ArrayAccess
     {
         if (strpos($name, 'is') === 0) {
             $type = strtolower(substr($name, 2));
-            
+
             array_unshift($params, $type);
-            
+
             return call_user_func_array([$this, 'is'], $params);
         }
         throw new BadMethodCallException(sprintf('Method %s does not exist', $name));
@@ -639,7 +639,7 @@ class Request implements ArrayAccess
 
             return count(array_filter($result)) > 0;
         }
-        
+
         $args = func_get_args();
         array_shift($args);
 
@@ -651,7 +651,7 @@ class Request implements ArrayAccess
         if ($args) {
             return $this->_is($type, $args);
         }
-        
+
         if (!isset($this->_detectorCache[$type])) {
             $this->_detectorCache[$type] = $this->_is($type, $args);
         }
@@ -1161,11 +1161,15 @@ class Request implements ArrayAccess
      * Provides a read accessor for `$this->query`. Allows you
      * to use a syntax similar to `CakeSession` for reading URL query data.
      *
-     * @param string $name Query string variable name
-     * @return mixed The value being read
+     * @param string|null $name Query string variable name or null to read all.
+     * @return string|array|null The value being read
      */
-    public function query($name)
+    public function query($name = null)
     {
+        if ($name === null) {
+            return $this->query;
+        }
+
         return Hash::get($this->query, $name);
     }
 

+ 6 - 2
tests/TestCase/Network/RequestTest.php

@@ -2000,9 +2000,10 @@ class RequestTest extends TestCase
      */
     public function testQuery()
     {
-        $request = new Request([
+        $array = [
             'query' => ['foo' => 'bar', 'zero' => '0']
-        ]);
+        ];
+        $request = new Request($array);
 
         $result = $request->query('foo');
         $this->assertSame('bar', $result);
@@ -2012,6 +2013,9 @@ class RequestTest extends TestCase
 
         $result = $request->query('imaginary');
         $this->assertNull($result);
+
+        $result = $request->query();
+        $this->assertSame($array['query'], $result);
     }
 
     /**