Skip to content

Commit

Permalink
fix: remove unneeded methods
Browse files Browse the repository at this point in the history
getSegment() accepts n+1.
  • Loading branch information
kenjis committed Feb 17, 2023
1 parent d919930 commit 72f76a6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 56 deletions.
64 changes: 9 additions & 55 deletions system/HTTP/SiteURI.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,25 @@ public function getRoutePath(): string
}

/**
* Returns the URI segments of the path as an array.
*/
public function getSegments(): array
{
return $this->segments;
}

/**
* Returns the value of a specific segment of the URI path relative to baseURL.
* Returns the value of a specific segment of the URI path.
* Allows to get only existing segments or the next one.
*
* @param int $number Segment number
* @param int $number Segment number starting at 1
* @param string $default Default value
*
* @return string The value of the segment. If no segment is found,
* throws HTTPException
* @return string The value of the segment. If you specify the last +1
* segment, the $default value. If you specify the last +2
* or more throws HTTPException.
*
* @TODO remove this method after merging #7267
*/
public function getSegment(int $number, string $default = ''): string
{
if ($number < 1) {
throw HTTPException::forURISegmentOutOfRange($number);
}

if ($number > count($this->segments) && ! $this->silent) {
if ($number > count($this->segments) + 1 && ! $this->silent) {
throw HTTPException::forURISegmentOutOfRange($number);
}

Expand All @@ -225,48 +221,6 @@ public function getSegment(int $number, string $default = ''): string
return $this->segments[$number] ?? $default;
}

/**
* Set the value of a specific segment of the URI path relative to baseURL.
* Allows to set only existing segments or add new one.
*
* @param int $number The segment number. Starting with 1.
* @param string $value The segment value.
*
* @return $this
*/
public function setSegment(int $number, $value)
{
if ($number < 1) {
throw HTTPException::forURISegmentOutOfRange($number);
}

if ($number > count($this->segments) + 1) {
if ($this->silent) {
return $this;
}

throw HTTPException::forURISegmentOutOfRange($number);
}

// The segment should treat the array as 1-based for the user,
// but we still have to deal with a zero-based array.
$number--;

$this->segments[$number] = $value;

$this->refreshPath();

return $this;
}

/**
* Returns the total number of segments.
*/
public function getTotalSegments(): int
{
return count($this->segments);
}

/**
* Formats the URI as a string.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/system/HTTP/SiteURITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public function testGetSegmentOutOfRange()
$uri = new SiteURI($config);
$uri->setPath('test/method');

$uri->getSegment(3);
$uri->getSegment(4);
}

public function testGetTotalSegments()
Expand Down

0 comments on commit 72f76a6

Please sign in to comment.