public function testToQueryString() { $this->assertEmpty(ArrayUtils::toQueryString(array())); $this->assertEquals('key=val&key1=val1', ArrayUtils::toQueryString(array('key' => 'val', 'key1' => 'val1'))); $this->assertEquals('key=1', ArrayUtils::toQueryString(array('key' => true))); $this->assertEquals('key=0', ArrayUtils::toQueryString(array('key' => false))); $this->assertEquals('', ArrayUtils::toQueryString(array('key' => null))); $this->assertEquals('key=', ArrayUtils::toQueryString(array('key' => ''))); $this->assertEquals('key=+++', ArrayUtils::toQueryString(array('key' => ' '))); $this->assertEquals('key=lorem+ipsum+dolor', ArrayUtils::toQueryString(array('key' => 'lorem ipsum dolor'))); $this->assertEquals('key=Lorem+ipsum+DOLOR', ArrayUtils::toQueryString(array('key' => 'Lorem ipsum DOLOR'))); $this->assertEquals('0=val', ArrayUtils::toQueryString(array('val'))); $nested = array('key' => 'val', 'key1' => array('subkey' => 'subval', 'subkey1' => 'subval1'), 'key2' => array('a', 'b', 'c', 'd'), '2D_collection_5' => $this->_getArrayPreset('2D_collection_5'), '2D_collection_5_named' => $this->_getArrayPreset('2D_collection_5_named')); $nestedResult = 'key=val&key1%5Bsubkey%5D=subval&key1%5Bsubkey1%5D=subval1&key2%5B0%5D=a&key2%5B1%5D=b&key2%5B2%5D=c&key2%5B3%5D=d&2D_collection_5%5B0%5D%5Bid%5D=1&2D_collection_5%5B0%5D%5Bname%5D=lorem&2D_collection_5%5B0%5D%5BcategoryId%5D=5&2D_collection_5%5B0%5D%5Bdate%5D=2013.07.08&2D_collection_5%5B1%5D%5Bid%5D=2&2D_collection_5%5B1%5D%5Bname%5D=ipsum&2D_collection_5%5B1%5D%5BcategoryId%5D=3&2D_collection_5%5B1%5D%5Bdate%5D=2013.07.07&2D_collection_5%5B2%5D%5Bid%5D=5&2D_collection_5%5B2%5D%5Bname%5D=dolor&2D_collection_5%5B2%5D%5BcategoryId%5D=1&2D_collection_5%5B2%5D%5Bdate%5D=2012.07.08&2D_collection_5%5B3%5D%5Bid%5D=6&2D_collection_5%5B3%5D%5Bname%5D=sit&2D_collection_5%5B3%5D%5BcategoryId%5D=3&2D_collection_5%5B3%5D%5Bdate%5D=2013.12.08&2D_collection_5%5B4%5D%5Bid%5D=9&2D_collection_5%5B4%5D%5Bname%5D=amet&2D_collection_5%5B4%5D%5BcategoryId%5D=5&2D_collection_5%5B4%5D%5Bdate%5D=2013.10.14&2D_collection_5_named%5Blorem%5D%5Bid%5D=1&2D_collection_5_named%5Blorem%5D%5Bname%5D=lorem&2D_collection_5_named%5Blorem%5D%5BcategoryId%5D=5&2D_collection_5_named%5Blorem%5D%5Bdate%5D=2013.07.08&2D_collection_5_named%5Bipsum%5D%5Bid%5D=2&2D_collection_5_named%5Bipsum%5D%5Bname%5D=ipsum&2D_collection_5_named%5Bipsum%5D%5BcategoryId%5D=3&2D_collection_5_named%5Bipsum%5D%5Bdate%5D=2013.07.07&2D_collection_5_named%5Bdolor%5D%5Bid%5D=5&2D_collection_5_named%5Bdolor%5D%5Bname%5D=dolor&2D_collection_5_named%5Bdolor%5D%5BcategoryId%5D=1&2D_collection_5_named%5Bdolor%5D%5Bdate%5D=2012.07.08&2D_collection_5_named%5Bsit%5D%5Bid%5D=6&2D_collection_5_named%5Bsit%5D%5Bname%5D=sit&2D_collection_5_named%5Bsit%5D%5BcategoryId%5D=3&2D_collection_5_named%5Bsit%5D%5Bdate%5D=2013.12.08&2D_collection_5_named%5Bamet%5D%5Bid%5D=9&2D_collection_5_named%5Bamet%5D%5Bname%5D=amet&2D_collection_5_named%5Bamet%5D%5BcategoryId%5D=5&2D_collection_5_named%5Bamet%5D%5Bdate%5D=2013.10.14'; $this->assertEquals($nestedResult, ArrayUtils::toQueryString($nested)); }
/** * Generates a URL for this route, using the given parameters. * * If some parameters aren't in the pattern, then they will be attached as a query string. * * @param array $params Route parameters. * @param string $host [optional] Host to prefix the URL with. Should also include the protocol, e.g. 'http://domain.com'. * Default: null - no host included. * @return string */ public function generateUrl(array $params = array(), $host = null) { if ($this->isPrivate()) { throw new \RuntimeException('Route "' . $this->getName() . '" is set to private, so it is not reachable via URL, therefore it cannot have a URL generated.'); } $routeName = $this->getName(); $url = preg_replace_callback('/(\\{([\\w:]+)\\})(\\?)?/is', function ($matches) use(&$params, $routeName) { $constraints = explode(':', $matches[2]); $name = array_shift($constraints); $optional = isset($matches[3]) && $matches[3] === '?'; if (!isset($params[$name])) { if (!$optional) { throw new RouteParameterNotFoundException('Could not find parameter "' . $name . '" for route "' . $routeName . '".'); } else { return ''; } } $param = urlencode($params[$name]); unset($params[$name]); /** @todo Should also check the constraints before injecting the item. */ return $param; }, $this->getUrlPattern()); // remove all optional characters from the route $url = preg_replace('/(\\/\\/\\?)/is', '/', $url); // two slashes followed by a ? (ie. //? ) change to a single slash (ie. /). $url = str_replace('?', '', $url); // remove any other ? marks if (!empty($params)) { $url .= '?' . ArrayUtils::toQueryString($params); } if ($host && !empty($host)) { $url = rtrim($host, '/') . '/' . ltrim($url, '/'); } return $url; }