diff --git a/Uri/Argument.php b/Uri/Argument.php index 8963b0033..669d164ea 100644 --- a/Uri/Argument.php +++ b/Uri/Argument.php @@ -108,6 +108,14 @@ final class Argument implements UriInterface */ private string $path = ''; + /** + * Uri path with offset. + * + * @var string + * @since 1.0.0 + */ + private string $offsetPath = ''; + /** * Uri query. * @@ -164,6 +172,9 @@ final class Argument implements UriInterface $this->path = \array_shift($uriParts); $this->pathElements = \explode('/', \ltrim($this->path, '/')); + $path = \array_slice($this->pathElements, $this->pathOffset); + $this->offsetPath = '/' . \implode('/', $path); + $this->setQuery(\implode(' ', $uriParts)); } @@ -235,6 +246,9 @@ final class Argument implements UriInterface public function setPathOffset(int $offset = 0) : void { $this->pathOffset = $offset; + + $path = \array_slice($this->pathElements, $this->pathOffset); + $this->offsetPath = '/' . \implode('/', $path); } /** @@ -252,6 +266,9 @@ final class Argument implements UriInterface { $this->path = $path; $this->pathElements = \explode('/', \ltrim($this->path, '/')); + + $path = \array_slice($this->pathElements, $this->pathOffset); + $this->offsetPath = '/' . \implode('/', $path); } /** @@ -269,10 +286,12 @@ final class Argument implements UriInterface /** * {@inheritdoc} */ - public function getRoute() : string + public function getRoute(bool $ignoreOffset = false) : string { + $path = $ignoreOffset ? $this->path : $this->offsetPath; + $query = $this->getQuery(); - return $this->path . (!empty($query) ? ' ' . $this->getQuery() : ''); + return $path . (!empty($query) ? ' ' . $this->getQuery() : ''); } /** diff --git a/Uri/HttpUri.php b/Uri/HttpUri.php index 39a12bd6c..cba9dd6f0 100644 --- a/Uri/HttpUri.php +++ b/Uri/HttpUri.php @@ -204,6 +204,10 @@ final class HttpUri implements UriInterface } $this->pathElements = \explode('/', \trim($this->path, '/')); + + $path = \array_slice($this->pathElements, $this->pathOffset); + $this->offsetPath = '/' . \implode('/', $path); + $this->queryString = $url['query'] ?? ''; if (!empty($this->queryString)) { @@ -278,6 +282,9 @@ final class HttpUri implements UriInterface public function setPathOffset(int $offset = 0) : void { $this->pathOffset = $offset; + + $path = \array_slice($this->pathElements, $this->pathOffset); + $this->offsetPath = '/' . \implode('/', $path); } /** @@ -302,7 +309,7 @@ final class HttpUri implements UriInterface /** * {@inheritdoc} */ - public function getPath() : string + public function getPath(int $offset = 0) : string { return $this->path; } @@ -314,6 +321,9 @@ final class HttpUri implements UriInterface { $this->path = $path; $this->pathElements = \explode('/', \ltrim($this->path, '/')); + + $path = \array_slice($this->pathElements, $this->pathOffset); + $this->offsetPath = '/' . \implode('/', $path); } /** @@ -331,10 +341,12 @@ final class HttpUri implements UriInterface /** * {@inheritdoc} */ - public function getRoute() : string + public function getRoute(bool $ignoreOffset = false) : string { + $path = $ignoreOffset ? $this->path : $this->offsetPath; + $query = $this->getQuery(); - return $this->path . (!empty($query) ? '?' . $this->getQuery() : ''); + return $path . (!empty($query) ? '?' . $this->getQuery() : ''); } /** diff --git a/Uri/UriInterface.php b/Uri/UriInterface.php index 0731adb58..f2fc2b470 100644 --- a/Uri/UriInterface.php +++ b/Uri/UriInterface.php @@ -175,11 +175,13 @@ interface UriInterface /** * Get route representation of uri. * + * @param bool $ignoreOffset Ignore internal offset + * * @return string * * @since 1.0.0 */ - public function getRoute() : string; + public function getRoute(bool $ignoreOffset = false) : string; /** * Set uri.