Issues

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

Description

I'm giving some static parameters to a page through a navigation xml file ("params" markup) and i'd like to add dynamically others parameter to the same page. In order to do that,i'm using the setParams() method of Zend_Navigation_Page_Mvc class.

Problem: this method erase previous parameters given via xml.

I suggest to merge parameters instead of erase previous ones:

Current listing:


public function setParams(array $params = null)
{
    if (null === $params) {
        $this->_params = array();
    } else {
        // TODO: do this more intelligently?
        $this->_params = $params;
    }

    $this->_hrefCache = null;
    return $this;
}

with merging:


public function setParams(array $params = null)
{
    if (null === $params) {
        $this->_params = array();
    } else {
        // TODO: do this more intelligently?
        $this->_params = array_merge($this->_params, $params);
    }

    $this->_hrefCache = null;
    return $this;
}

Workaround: using getParams in my listing to merge parameters with others then using setParams($merged_array)

Comments

My suggestion:

  • setParam
  • addParams
  • setParams
  • getParam
  • getParams
  • removeParam
  • clearParams

/**
 * 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->_param[$name] = $value;
    
    return $this;
}

/**
 * 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;
}

/**
 * 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 $params                paramters as array ('name' => 'value')
 * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
 */
public function setParams(array $params)
{
    $this->clearParams();
    $this->addParams($params);
    
    return $this;
}

/**
 * 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];
}

/**
 * Retrieve all parameters (to use when assembling URL)
 * 
 * @see getHref()
 *
 * @return array                       parameters as array ('name' => 'value')
 */
public function getParams()
{
    return $this->_params;
}

/**
 * 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]);
        
        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();
    
    return $this;
}

Patch and unit tests added.

Fixed in trunk (1.12.0): r24867