Commit 2ad1b457 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Support.php: Add function mergeUrlComponents().

parent d1a6907f
......@@ -586,13 +586,60 @@ class Support {
* @return string
*/
public static function concatUrlParam($url, $param) {
if ($param == '')
if ($param == '') {
return $url;
}
$token = (strpos($url, '?') === false) ? '?' : '&';
return $url . $token . $param;
}
/**
* Concatenate $host, $path and $query. A given $host will always append a '/' if none is given.
* End of $path, there is no assumption to append '/' - the user needs full control.
* $query: trailing 'index.php' will be skipped.
*
* If HOST and PATH is combined in a var, pass the value as $hostOrPath, leave $host empty.
*
* @param string $host
* @param string $hostOrPath
* @param string $query
* @return string
* @throws CodeException
*/
public static function mergeUrlComponents($host, $hostOrPath, $query) {
$url = '';
$pre = '';
if ($host != '' && substr($host, -1, 1) != '/') {
$host .= '/';
}
if ($host != '' && substr($hostOrPath, 0, 1) == '/') {
$hostOrPath = substr($hostOrPath, 1);
}
if ($host != '' || $hostOrPath != '') {
$url = $host . $hostOrPath;
}
if (substr($query, 0, 9) == 'index.php') {
$query = substr($query, 9);
}
if (false !== strpos(substr($query, 1), '?')) {
throw new CodeException('Found a "?" after the beginning of the query - this is forbidden', ERROR_BROKEN_PARAMETER);
}
if ($query != '') {
$url = $url . '?' . $query;
$url = str_replace('??', '?', $url);
}
return $url;
}
/**
* Set Defaults for the current formElement.
*
......
......@@ -447,6 +447,73 @@ class SupportTest extends \PHPUnit_Framework_TestCase {
Support::insertAttribute('<>', 'class', 'qfq');
}
/**
* @expectedException \qfq\CodeException
*/
public function testMergeUrlComponentsException() {
Support::mergeUrlComponents('', '', 'sub?id=1');
}
public function testMergeUrlComponents() {
$this->assertEquals('', Support::mergeUrlComponents('', '', ''));
$this->assertEquals('example.com/', Support::mergeUrlComponents('example.com', '', ''));
$this->assertEquals('example.com/', Support::mergeUrlComponents('example.com/', '', ''));
$this->assertEquals('http://example.com/', Support::mergeUrlComponents('http://example.com', '', ''));
$this->assertEquals('example.com/sub', Support::mergeUrlComponents('example.com', 'sub', ''));
$this->assertEquals('example.com/sub', Support::mergeUrlComponents('example.com/', 'sub', ''));
$this->assertEquals('example.com/sub', Support::mergeUrlComponents('example.com', '/sub', ''));
$this->assertEquals('example.com/sub', Support::mergeUrlComponents('example.com/', '/sub', ''));
$this->assertEquals('example.com/', Support::mergeUrlComponents('example.com/', '/', ''));
$this->assertEquals('example.com/sub/sub2', Support::mergeUrlComponents('example.com/sub', 'sub2', ''));
$this->assertEquals('example.com/sub/sub2', Support::mergeUrlComponents('example.com/sub/', 'sub2', ''));
$this->assertEquals('example.com/sub/sub2', Support::mergeUrlComponents('example.com/sub', '/sub2', ''));
$this->assertEquals('example.com/sub/sub2', Support::mergeUrlComponents('example.com/sub/', '/sub2', ''));
$this->assertEquals('http://example.com/sub', Support::mergeUrlComponents('http://example.com', 'sub', ''));
$this->assertEquals('http://example.com/sub', Support::mergeUrlComponents('http://example.com/', 'sub', ''));
$this->assertEquals('http://example.com/sub', Support::mergeUrlComponents('http://example.com/', '/sub', ''));
$this->assertEquals('http://example.com/sub', Support::mergeUrlComponents('http://example.com', '/sub', ''));
$this->assertEquals('sub', Support::mergeUrlComponents('', 'sub', ''));
$this->assertEquals('/sub', Support::mergeUrlComponents('', '/sub', ''));
$this->assertEquals('/sub/', Support::mergeUrlComponents('', '/sub/', ''));
$this->assertEquals('/', Support::mergeUrlComponents('', '/', ''));
$this->assertEquals('example.com/sub?id=1', Support::mergeUrlComponents('example.com', 'sub', 'id=1'));
$this->assertEquals('example.com/sub?id=1', Support::mergeUrlComponents('example.com', 'sub', '?id=1'));
$this->assertEquals('example.com/sub/?id=1', Support::mergeUrlComponents('example.com', 'sub/', 'id=1'));
$this->assertEquals('example.com/sub/?id=1', Support::mergeUrlComponents('example.com', 'sub/', '?id=1'));
$this->assertEquals('sub?id=1', Support::mergeUrlComponents('', 'sub', 'id=1'));
$this->assertEquals('/sub?id=1', Support::mergeUrlComponents('', '/sub', '?id=1'));
$this->assertEquals('sub/?id=1', Support::mergeUrlComponents('', 'sub/', 'id=1'));
$this->assertEquals('/sub/?id=1', Support::mergeUrlComponents('', '/sub/', '?id=1'));
$this->assertEquals('example.com/sub/index.php?id=1', Support::mergeUrlComponents('example.com', 'sub/index.php', 'id=1'));
$this->assertEquals('example.com/sub/index.php?id=1', Support::mergeUrlComponents('example.com', 'sub/index.php', '?id=1'));
$this->assertEquals('example.com/?id=1', Support::mergeUrlComponents('example.com', '', 'id=1'));
$this->assertEquals('example.com/?id=1', Support::mergeUrlComponents('example.com', '', '?id=1'));
$this->assertEquals('example.com/?id=1', Support::mergeUrlComponents('example.com/', '', 'id=1'));
$this->assertEquals('example.com/?id=1', Support::mergeUrlComponents('example.com/', '', '?id=1'));
$this->assertEquals('example.com/?id=1', Support::mergeUrlComponents('example.com', '/', 'id=1'));
$this->assertEquals('example.com/?id=1', Support::mergeUrlComponents('example.com', '/', '?id=1'));
$this->assertEquals('?', Support::mergeUrlComponents('', '', '?'));
$this->assertEquals('?id=1', Support::mergeUrlComponents('', '', 'id=1'));
$this->assertEquals('?id=1', Support::mergeUrlComponents('', '', '?id=1'));
$this->assertEquals('', Support::mergeUrlComponents('', '', 'index.php'));
$this->assertEquals('?', Support::mergeUrlComponents('', '', 'index.php?'));
$this->assertEquals('?id=1', Support::mergeUrlComponents('', '', 'index.php?id=1'));
}
protected function setUp() {
parent::setUp();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment