Skip to content

Commit 5b5afae

Browse files
committed
UrlScript: added withPath()
1 parent 5791551 commit 5b5afae

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Http/UrlScript.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ public function __construct($url = '/', string $scriptPath = '')
4949
}
5050

5151

52+
/**
53+
* @return static
54+
*/
55+
public function withPath(string $path, string $scriptPath = '')
56+
{
57+
$dolly = clone $this;
58+
$dolly->scriptPath = $scriptPath;
59+
return call_user_func([$dolly, 'parent::withPath'], $path);
60+
}
61+
62+
5263
public function getScriptPath(): string
5364
{
5465
return $this->scriptPath;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Http\UrlScript;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
test(function () {
13+
$url = new UrlScript('http://nette.org:8080/test/?q=search', '/test/index.php');
14+
Assert::same('/test/', $url->basePath);
15+
Assert::same('/test/index.php', $url->scriptPath);
16+
17+
$url = $url->withPath('/index.php');
18+
Assert::same('/', $url->basePath);
19+
Assert::same('/index.php', $url->scriptPath);
20+
21+
$url = $url->withPath('/test/', '/test/index.php');
22+
Assert::same('/test/', $url->basePath);
23+
Assert::same('/test/index.php', $url->scriptPath);
24+
});
25+
26+
27+
Assert::exception(function () {
28+
$url = new UrlScript('http://nette.org:8080/test/?q=search', '/test/index.php');
29+
$url->withPath('/test/', '/test/index/');
30+
}, Nette\InvalidArgumentException::class);

0 commit comments

Comments
 (0)