getRoute now returns the offsetted path by default

This commit is contained in:
Dennis Eichhorn 2022-03-19 08:15:04 +01:00
parent a2f067920c
commit cab2628062
3 changed files with 39 additions and 6 deletions

View File

@ -108,6 +108,14 @@ final class Argument implements UriInterface
*/ */
private string $path = ''; private string $path = '';
/**
* Uri path with offset.
*
* @var string
* @since 1.0.0
*/
private string $offsetPath = '';
/** /**
* Uri query. * Uri query.
* *
@ -164,6 +172,9 @@ final class Argument implements UriInterface
$this->path = \array_shift($uriParts); $this->path = \array_shift($uriParts);
$this->pathElements = \explode('/', \ltrim($this->path, '/')); $this->pathElements = \explode('/', \ltrim($this->path, '/'));
$path = \array_slice($this->pathElements, $this->pathOffset);
$this->offsetPath = '/' . \implode('/', $path);
$this->setQuery(\implode(' ', $uriParts)); $this->setQuery(\implode(' ', $uriParts));
} }
@ -235,6 +246,9 @@ final class Argument implements UriInterface
public function setPathOffset(int $offset = 0) : void public function setPathOffset(int $offset = 0) : void
{ {
$this->pathOffset = $offset; $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->path = $path;
$this->pathElements = \explode('/', \ltrim($this->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} * {@inheritdoc}
*/ */
public function getRoute() : string public function getRoute(bool $ignoreOffset = false) : string
{ {
$path = $ignoreOffset ? $this->path : $this->offsetPath;
$query = $this->getQuery(); $query = $this->getQuery();
return $this->path . (!empty($query) ? ' ' . $this->getQuery() : ''); return $path . (!empty($query) ? ' ' . $this->getQuery() : '');
} }
/** /**

View File

@ -204,6 +204,10 @@ final class HttpUri implements UriInterface
} }
$this->pathElements = \explode('/', \trim($this->path, '/')); $this->pathElements = \explode('/', \trim($this->path, '/'));
$path = \array_slice($this->pathElements, $this->pathOffset);
$this->offsetPath = '/' . \implode('/', $path);
$this->queryString = $url['query'] ?? ''; $this->queryString = $url['query'] ?? '';
if (!empty($this->queryString)) { if (!empty($this->queryString)) {
@ -278,6 +282,9 @@ final class HttpUri implements UriInterface
public function setPathOffset(int $offset = 0) : void public function setPathOffset(int $offset = 0) : void
{ {
$this->pathOffset = $offset; $this->pathOffset = $offset;
$path = \array_slice($this->pathElements, $this->pathOffset);
$this->offsetPath = '/' . \implode('/', $path);
} }
/** /**
@ -302,7 +309,7 @@ final class HttpUri implements UriInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() : string public function getPath(int $offset = 0) : string
{ {
return $this->path; return $this->path;
} }
@ -314,6 +321,9 @@ final class HttpUri implements UriInterface
{ {
$this->path = $path; $this->path = $path;
$this->pathElements = \explode('/', \ltrim($this->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} * {@inheritdoc}
*/ */
public function getRoute() : string public function getRoute(bool $ignoreOffset = false) : string
{ {
$path = $ignoreOffset ? $this->path : $this->offsetPath;
$query = $this->getQuery(); $query = $this->getQuery();
return $this->path . (!empty($query) ? '?' . $this->getQuery() : ''); return $path . (!empty($query) ? '?' . $this->getQuery() : '');
} }
/** /**

View File

@ -175,11 +175,13 @@ interface UriInterface
/** /**
* Get route representation of uri. * Get route representation of uri.
* *
* @param bool $ignoreOffset Ignore internal offset
*
* @return string * @return string
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function getRoute() : string; public function getRoute(bool $ignoreOffset = false) : string;
/** /**
* Set uri. * Set uri.