Index: library/Zend/Controller/Router/Route/Regex.php =================================================================== --- library/Zend/Controller/Router/Route/Regex.php (revision 8431) +++ library/Zend/Controller/Router/Route/Regex.php (working copy) @@ -132,7 +132,7 @@ return $return; } - + /** * Assembles a URL path defined by this route * @@ -146,13 +146,13 @@ throw new Zend_Controller_Router_Exception('Cannot assemble. Reversed route is not specified.'); } - $data = $this->_getMappedValues($data, true, false); - $data += $this->_getMappedValues($this->_defaults, true, false); - $data += $this->_values; + $mergedData = $this->_getMappedValues($this->_defaults, true, false); + $mergedData = $this->_arrayMergeNumericKeys($mergedData, $this->_getMappedValues($this->_values, true, false)); + $mergedData = $this->_arrayMergeNumericKeys($mergedData, $this->_getMappedValues($data, true, false)); - ksort($data); + ksort($mergedData); - $return = @vsprintf($this->_reverse, $data); + $return = @vsprintf($this->_reverse, $mergedData); if ($return === false) { require_once 'Zend/Controller/Router/Exception.php'; @@ -183,5 +183,23 @@ public function getDefaults() { return $this->_defaults; } + + /** + * _arrayMergeNumericKeys() - allows for a strict key (numeric's included) array_merge. + * php's array_merge() lacks the ability to merge with numeric keys. + * + * @param array $array1 + * @param array $array2 + * @return array + */ + protected function _arrayMergeNumericKeys(Array $array1, Array $array2) + { + $returnArray = $array1; + foreach ($array2 as $array2Index => $array2Value) { + $returnArray[$array2Index] = $array2Value; + } + return $returnArray; + } + } Index: tests/Zend/Controller/Router/Route/RegexTest.php =================================================================== --- tests/Zend/Controller/Router/Route/RegexTest.php (revision 8431) +++ tests/Zend/Controller/Router/Route/RegexTest.php (working copy) @@ -5,12 +5,11 @@ * @subpackage UnitTests */ +require_once dirname(__FILE__) . '/../../../../TestHelper.php'; + /** Zend_Controller_Router_Route_Regex */ require_once 'Zend/Controller/Router/Route/Regex.php'; -/** PHPUnit test case */ -require_once 'PHPUnit/Framework/TestCase.php'; - /** * @category Zend * @package Zend_Controller @@ -391,7 +390,10 @@ } - public function testAssembleZF2301() + /** + * @issue ZF-2301 + */ + public function testAssemblyOfRouteWithMergedMatchedParts() { $route = new Zend_Controller_Router_Route_Regex( "itemlist(?:/(\d+))?", @@ -400,15 +402,20 @@ 'itemlist/%d' ); - $values = $route->match('/itemlist/2'); + // make sure defaults work + $this->assertEquals(array('page' => 1), $route->match('/itemlist/')); - $this->assertEquals(array('page' => 2), $values); - - $url = $route->assemble(); - $this->assertEquals('itemlist/2', $url); + // make sure default assembly work + $this->assertEquals('itemlist/1', $route->assemble()); - $url = $route->assemble(array('page' => 2)); - $this->assertEquals('itemlist/2', $url); + // make sure the route is parsed correctly + $this->assertEquals(array('page' => 2), $route->match('/itemlist/2')); + + // check to make sure that the default assembly will return with default 1 (previously defined) + $this->assertEquals('itemlist/2', $route->assemble()); + + // check to make sure that the assembly will return with provided page=2 in the correct place + $this->assertEquals('itemlist/2', $route->assemble(array('page' => 2))); } }