Procházet zdrojové kódy

ZF-1480
added
Zend_Uri_Http::getQueryAsArray()
Zend_Uri_Http::addReplaceQueryParameters(array $queryParams)
Zend_Uri_Http::removeQueryParameters(array $queryParamKeys)
for easier query modification

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19041 44c647ce-9c0f-0410-b52a-842ac1e357ba

sgehrig před 16 roky
rodič
revize
05d74b61f6
2 změnil soubory, kde provedl 89 přidání a 0 odebrání
  1. 43 0
      library/Zend/Uri/Http.php
  2. 46 0
      tests/Zend/Uri/HttpTest.php

+ 43 - 0
library/Zend/Uri/Http.php

@@ -592,6 +592,23 @@ class Zend_Uri_Http extends Zend_Uri
     }
 
     /**
+     * Returns the query portion of the URL (after ?) as a
+     * key-value-array. If the query is empty an empty array
+     * is returned
+     *
+     * @return array
+     */
+    public function getQueryAsArray()
+    {
+        $query = $this->getQuery();
+        $querryArray = array();
+        if ($query !== false) {
+            parse_str($query, $querryArray);
+        }
+        return $querryArray;
+    }
+
+    /**
      * Returns true if and only if the query string passes validation. If no query is passed,
      * then the query string contained in the instance variable is used.
      *
@@ -623,6 +640,32 @@ class Zend_Uri_Http extends Zend_Uri
     }
 
     /**
+     * Add or replace params in the query string for the current URI, and
+     * return the old query.
+     *
+     * @param  array $queryParams
+     * @return string Old query string
+     */
+    public function addReplaceQueryParameters(array $queryParams)
+    {
+        $queryParams = array_merge($this->getQueryAsArray(), $queryParams);
+        return $this->setQuery($queryParams);
+    }
+
+    /**
+     * Remove params in the query string for the current URI, and
+     * return the old query.
+     *
+     * @param  array $queryParamKeys
+     * @return string Old query string
+     */
+    public function removeQueryParameters(array $queryParamKeys)
+    {
+        $queryParams = array_diff_key($this->getQueryAsArray(), array_fill_keys($queryParamKeys, 0));
+        return $this->setQuery($queryParams);
+    }
+
+    /**
      * Set the query string for the current URI, and return the old query
      * string This method accepts both strings and arrays.
      *

+ 46 - 0
tests/Zend/Uri/HttpTest.php

@@ -379,4 +379,50 @@ class Zend_Uri_HttpTest extends PHPUnit_Framework_TestCase
         $this->setExpectedException('Zend_Uri_Exception');
         $uri->setHost($host);
     }
+
+    /**
+     * @group ZF-1480
+     */
+    public function testGetQueryAsArrayReturnsCorrectArray()
+    {
+        $uri = Zend_Uri_Http::fromString('http://example.com/foo/?test=a&var[]=1&var[]=2&some[thing]=3');
+        $this->assertEquals(array(
+            'test' => 'a',
+            'var'  => array(1, 2),
+            'some' => array('thing' => 3)
+        ), $uri->getQueryAsArray());
+    }
+
+    /**
+     * @group ZF-1480
+     */
+    public function testAddReplaceQueryParametersModifiesQueryAndReturnsOldQuery()
+    {
+        $uri = Zend_Uri_Http::fromString('http://example.com/foo/?a=1&b=2&c=3');
+        $this->assertEquals('a=1&b=2&c=3', $uri->addReplaceQueryParameters(array(
+            'b' => 4,
+            'd' => -1
+        )));
+        $this->assertEquals(array(
+            'a' => 1,
+            'b' => 4,
+            'c' => 3,
+            'd' => -1
+        ), $uri->getQueryAsArray());
+        $this->assertEquals('a=1&b=4&c=3&d=-1', $uri->getQuery());
+    }
+
+    /**
+     * @group ZF-1480
+     */
+    public function testRemoveQueryParametersModifiesQueryAndReturnsOldQuery()
+    {
+        $uri = Zend_Uri_Http::fromString('http://example.com/foo/?a=1&b=2&c=3&d=4');
+        $this->assertEquals('a=1&b=2&c=3&d=4', $uri->removeQueryParameters(array('b', 'd', 'e')));
+        $this->assertEquals(array(
+            'a' => 1,
+            'c' => 3
+        ), $uri->getQueryAsArray());
+        $this->assertEquals('a=1&c=3', $uri->getQuery());
+    }
 }