Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect segment number in URI::getSegment() exception message #7267

Merged
merged 3 commits into from
Feb 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions system/HTTP/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,31 +532,38 @@ public function getSegments(): array

/**
* 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 InvalidArgumentError
* @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.
*/
public function getSegment(int $number, string $default = ''): string
{
// The segment should treat the array as 1-based for the user
// but we still have to deal with a zero-based array.
$number--;
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);
}

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

return $this->segments[$number] ?? $default;
}

/**
* Set the value of a specific segment of the URI path.
* Allows to set only existing segments or add new one.
*
* @param mixed $value (string or int)
* @param int $number Segment number starting at 1
* @param int|string $value
*
* @return $this
*/
Expand Down