Issue

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

Issue Type: Improvement Created: 2010-11-22T17:28:15.000+0000 Last Updated: 2012-06-02T01:33:37.000+0000 Status: Resolved Fix version(s): - 1.12.0 (27/Aug/12)

Reporter: Eveha (eveha) Assignee: Frank Brückner (frosch) Tags: - Zend_Navigation

  • FixForZF1.12
  • state:patch-ready-for-review
  • zf-crteam-padraic
  • zf-crteam-priority
  • zf-crteam-review

Related issues: Attachments: - Mvc.php.patch

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:

<pre class="highlight">
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:

<pre class="highlight">
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

Posted by Kai Uwe (kaiuwe) on 2011-04-06T08:29:41.000+0000

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

Posted by Frank Brückner (frosch) on 2011-09-22T08:57:47.000+0000

Patch and unit tests added.

Posted by Adam Lundrigan (adamlundrigan) on 2012-06-02T01:33:37.000+0000

Fixed in trunk (1.12.0): r24867

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2017 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts