Procházet zdrojové kódy

ZF-10727: Add additional methods to Zend_Navigation_Page_Mvc for manipulating parameters

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24867 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan před 13 roky
rodič
revize
e58391be96

+ 106 - 11
library/Zend/Navigation/Page/Mvc.php

@@ -349,33 +349,109 @@ class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
     }
 
     /**
-     * Sets params to use when assembling URL
+     * Set multiple parameters (to use when assembling URL) at once
      *
+     * URL options passed to the url action helper for assembling URLs.
+     * Overwrites any previously set parameters!
+     * 
      * @see getHref()
      *
-     * @param  array|null $params        [optional] page params. Default is null
-     *                                   which sets no params.
-     * @return Zend_Navigation_Page_Mvc  fluent interface, returns self
+     * @param  array|null $params           [optional] paramters as array
+     *                                      ('name' => 'value'). Default is null
+     *                                      which clears all params.
+     * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
      */
     public function setParams(array $params = null)
     {
-        if (null === $params) {
-            $this->_params = array();
-        } else {
-            // TODO: do this more intelligently?
-            $this->_params = $params;
+        $this->clearParams();
+        
+        if (is_array($params)) {
+            $this->addParams($params);
         }
+        
+        return $this;
+    }
+    
+    /**
+     * Set parameter (to use when assembling URL)
+     * 
+     * URL option passed to the url action helper for assembling URLs.
+     *
+     * @see getHref()
+     *
+     * @param  string $name                 parameter name
+     * @param  mixed $value                 parameter value
+     * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
+     */
+    public function setParam($name, $value)
+    {
+        $name = (string) $name;
+        $this->_params[$name] = $value;
 
         $this->_hrefCache = null;
         return $this;
     }
 
     /**
-     * Returns params to use when assembling URL
+     * Add multiple parameters (to use when assembling URL) at once
+     * 
+     * URL options passed to the url action helper for assembling URLs.
+     * 
+     * @see getHref()
+     *
+     * @param  array $params                paramters as array ('name' => 'value')
+     * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
+     */
+    public function addParams(array $params)
+    {
+        foreach ($params as $name => $value) {
+            $this->setParam($name, $value);
+        }
+
+        return $this;
+    }
+
+    /**
+     * Remove parameter (to use when assembling URL)
+     * 
+     * @see getHref()
+     *
+     * @param  string $name
+     * @return bool
+     */
+    public function removeParam($name)
+    {             
+        if (array_key_exists($name, $this->_params)) {
+            unset($this->_params[$name]);
+
+            $this->_hrefCache = null;
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Clear all parameters (to use when assembling URL)
+     * 
+     * @see getHref()
      *
+     * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
+     */
+    public function clearParams()
+    {
+        $this->_params = array();
+
+        $this->_hrefCache = null;
+        return $this;
+    }
+    
+    /**
+     * Retrieve all parameters (to use when assembling URL)
+     * 
      * @see getHref()
      *
-     * @return array  page params
+     * @return array                        parameters as array ('name' => 'value')
      */
     public function getParams()
     {
@@ -383,6 +459,25 @@ class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
     }
 
     /**
+     * Retrieve a single parameter (to use when assembling URL)
+     * 
+     * @see getHref()
+     *
+     * @param  string $name                 parameter name
+     * @return mixed
+     */
+    public function getParam($name)
+    {
+        $name = (string) $name;
+
+        if (!array_key_exists($name, $this->_params)) {
+            return null;
+        }
+
+        return $this->_params[$name];
+    }
+
+    /**
      * Sets route name to use when assembling URL
      *
      * @see getHref()

+ 81 - 0
tests/Zend/Navigation/Page/MvcTest.php

@@ -406,6 +406,87 @@ class Zend_Navigation_Page_MvcTest extends PHPUnit_Framework_TestCase
         $page->setParams(array());
         $this->assertEquals(array(), $page->getParams());
     }
+    
+    /**
+     * @group ZF-10727
+     */
+    public function testSetAndGetParam()
+    {
+        $page = new Zend_Navigation_Page_Mvc(array(
+            'label' => 'foo',
+            'action' => 'index',
+            'controller' => 'index'
+        ));
+        
+        $page->setParam('foo', 'bar');
+        $this->assertEquals('bar', $page->getParam('foo'));
+        
+        // Check type conversion
+        $page->setParam(null, null);
+        $this->assertEquals(null, $page->getParam('null'));
+    }
+    
+    /**
+     * @group ZF-10727
+     */
+    public function testAddParams()
+    {
+        $page = new Zend_Navigation_Page_Mvc(array(
+            'label' => 'foo',
+            'action' => 'index',
+            'controller' => 'index'
+        ));
+        
+        $params = array('foo' => 'bar', 'baz' => 'bat');
+        
+        $page->addParams($params);
+        $this->assertEquals($params, $page->getParams());
+        
+        $params2 = array('qux' => 'foobar');
+        
+        $page->addParams($params2);
+        $this->assertEquals(array_merge($params, $params2), $page->getParams());
+    }
+    
+    /**
+     * @group ZF-10727
+     */
+    public function testRemoveParam()
+    {
+        $page = new Zend_Navigation_Page_Mvc(array(
+            'label' => 'foo',
+            'action' => 'index',
+            'controller' => 'index'
+        ));
+        
+        $params = array('foo' => 'bar', 'baz' => 'bat');
+        
+        $page->setParams($params);
+        $page->removeParam('foo');
+        
+        $this->assertEquals(array('baz' => 'bat'), $page->getParams());
+        
+        $this->assertNull($page->getParam('foo'));
+    }
+    
+    /**
+     * @group ZF-10727
+     */
+    public function testClearParams()
+    {
+        $page = new Zend_Navigation_Page_Mvc(array(
+            'label' => 'foo',
+            'action' => 'index',
+            'controller' => 'index'
+        ));
+        
+        $params = array('foo' => 'bar', 'baz' => 'bat');
+        
+        $page->setParams($params);
+        $page->clearParams();
+        
+        $this->assertEquals(array(), $page->getParams());
+    }
 
     /**
      * @group ZF-11664