diff --git a/src/Http/UrlScript.php b/src/Http/UrlScript.php index 3b2edd1f..adebcd02 100644 --- a/src/Http/UrlScript.php +++ b/src/Http/UrlScript.php @@ -49,6 +49,17 @@ public function __construct($url = '/', string $scriptPath = '') } + /** + * @return static + */ + public function withPath(string $path, string $scriptPath = '') + { + $dolly = clone $this; + $dolly->scriptPath = $scriptPath; + return call_user_func([$dolly, 'parent::withPath'], $path); + } + + public function getScriptPath(): string { return $this->scriptPath; diff --git a/tests/Http/UrlScript.manipulation.phpt b/tests/Http/UrlScript.manipulation.phpt new file mode 100644 index 00000000..7faf41f6 --- /dev/null +++ b/tests/Http/UrlScript.manipulation.phpt @@ -0,0 +1,30 @@ +basePath); + Assert::same('/test/index.php', $url->scriptPath); + + $url = $url->withPath('/index.php'); + Assert::same('/', $url->basePath); + Assert::same('/index.php', $url->scriptPath); + + $url = $url->withPath('/test/', '/test/index.php'); + Assert::same('/test/', $url->basePath); + Assert::same('/test/index.php', $url->scriptPath); +}); + + +Assert::exception(function () { + $url = new UrlScript('http://nette.org:8080/test/?q=search', '/test/index.php'); + $url->withPath('/test/', '/test/index/'); +}, Nette\InvalidArgumentException::class);