Skip to content

Commit

Permalink
UrlScript: added getRelativePath() [Closes #45]
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik authored and dg committed Feb 11, 2019
1 parent 4c4738c commit 863d24f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Http/Url.php
Expand Up @@ -43,6 +43,7 @@
* @property-read string $authority
* @property-read string $hostUrl
* @property-read string $basePath
* @property-read string $relativePath
* @property-read string $baseUrl
* @property-read string $relativeUrl
* @property-read array $queryParameters
Expand Down Expand Up @@ -381,6 +382,16 @@ public function getBasePath(): string
}


/**
* Returns part of path after base path.
* @return string
*/
public function getRelativePath()
{
return substr($this->path, strlen($this->getBasePath()));
}


/**
* Returns the base-URI.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Http/UrlScript.php
Expand Up @@ -59,6 +59,16 @@ public function getScriptPath(): string
}


/**
* Returns part of path after base path.
* @return string
*/
public function getRelativePath()
{
return substr($this->getPath(), strrpos($this->scriptPath, '/') + 1);
}


/**
* Returns the base-path.
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Http/Url.httpScheme.phpt
Expand Up @@ -24,6 +24,7 @@ Assert::same('hostname', $url->host);
Assert::same(60, $url->port);
Assert::same('/p%61th/script.php', $url->path);
Assert::same('/p%61th/', $url->basePath);
Assert::same('script.php', $url->relativePath);
Assert::same('arg=value', $url->query);
Assert::same('anchor', $url->fragment);
Assert::same('hostname:60', $url->authority);
Expand Down
18 changes: 18 additions & 0 deletions tests/Http/UrlScript.modify.phpt
Expand Up @@ -23,3 +23,21 @@ Assert::same('/test/', $url->basePath);
Assert::same('?q=search', $url->relativeUrl);
Assert::same('', $url->pathInfo);
Assert::same('http://nette.org:8080/test/?q=search', $url->absoluteUrl);


$url = new UrlScript('http://nette.org:8080/www/about');
$url->scriptPath = '/www/';

Assert::same('/www/about', $url->path);
Assert::same('/www/', $url->scriptPath);
Assert::same('about', $url->relativePath);
Assert::same('about', $url->pathInfo);


$url = new UrlScript('http://nette.org:8080/www/index.php');
$url->scriptPath = '/www/index.php';

Assert::same('/www/index.php', $url->path);
Assert::same('/www/index.php', $url->scriptPath);
Assert::same('index.php', $url->relativePath);
Assert::same('', $url->pathInfo);

0 comments on commit 863d24f

Please sign in to comment.