Skip to content

Commit 72f76a6

Browse files
committed
fix: remove unneeded methods
getSegment() accepts n+1.
1 parent d919930 commit 72f76a6

File tree

2 files changed

+10
-56
lines changed

2 files changed

+10
-56
lines changed

system/HTTP/SiteURI.php

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,25 @@ public function getRoutePath(): string
192192
}
193193

194194
/**
195-
* Returns the URI segments of the path as an array.
196-
*/
197-
public function getSegments(): array
198-
{
199-
return $this->segments;
200-
}
201-
202-
/**
203-
* Returns the value of a specific segment of the URI path relative to baseURL.
195+
* Returns the value of a specific segment of the URI path.
196+
* Allows to get only existing segments or the next one.
204197
*
205-
* @param int $number Segment number
198+
* @param int $number Segment number starting at 1
206199
* @param string $default Default value
207200
*
208-
* @return string The value of the segment. If no segment is found,
209-
* throws HTTPException
201+
* @return string The value of the segment. If you specify the last +1
202+
* segment, the $default value. If you specify the last +2
203+
* or more throws HTTPException.
204+
*
205+
* @TODO remove this method after merging #7267
210206
*/
211207
public function getSegment(int $number, string $default = ''): string
212208
{
213209
if ($number < 1) {
214210
throw HTTPException::forURISegmentOutOfRange($number);
215211
}
216212

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

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

228-
/**
229-
* Set the value of a specific segment of the URI path relative to baseURL.
230-
* Allows to set only existing segments or add new one.
231-
*
232-
* @param int $number The segment number. Starting with 1.
233-
* @param string $value The segment value.
234-
*
235-
* @return $this
236-
*/
237-
public function setSegment(int $number, $value)
238-
{
239-
if ($number < 1) {
240-
throw HTTPException::forURISegmentOutOfRange($number);
241-
}
242-
243-
if ($number > count($this->segments) + 1) {
244-
if ($this->silent) {
245-
return $this;
246-
}
247-
248-
throw HTTPException::forURISegmentOutOfRange($number);
249-
}
250-
251-
// The segment should treat the array as 1-based for the user,
252-
// but we still have to deal with a zero-based array.
253-
$number--;
254-
255-
$this->segments[$number] = $value;
256-
257-
$this->refreshPath();
258-
259-
return $this;
260-
}
261-
262-
/**
263-
* Returns the total number of segments.
264-
*/
265-
public function getTotalSegments(): int
266-
{
267-
return count($this->segments);
268-
}
269-
270224
/**
271225
* Formats the URI as a string.
272226
*/

tests/system/HTTP/SiteURITest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public function testGetSegmentOutOfRange()
285285
$uri = new SiteURI($config);
286286
$uri->setPath('test/method');
287287

288-
$uri->getSegment(3);
288+
$uri->getSegment(4);
289289
}
290290

291291
public function testGetTotalSegments()

0 commit comments

Comments
 (0)