Skip to content

Commit e529291

Browse files
committed
feat: add URI::withScheme() and deprecate URI::setScheme()
1 parent 3e349e1 commit e529291

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

system/HTTP/URI.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use BadMethodCallException;
1515
use CodeIgniter\HTTP\Exceptions\HTTPException;
1616
use Config\App;
17+
use InvalidArgumentException;
1718

1819
/**
1920
* Abstraction for a uniform resource identifier (URI).
@@ -709,7 +710,7 @@ public function setAuthority(string $str)
709710
*
710711
* @return $this
711712
*
712-
* @TODO PSR-7: Should be `withScheme($scheme)`.
713+
* @deprecated Use `withScheme()` instead.
713714
*/
714715
public function setScheme(string $str)
715716
{
@@ -719,6 +720,34 @@ public function setScheme(string $str)
719720
return $this;
720721
}
721722

723+
/**
724+
* Return an instance with the specified scheme.
725+
*
726+
* This method MUST retain the state of the current instance, and return
727+
* an instance that contains the specified scheme.
728+
*
729+
* Implementations MUST support the schemes "http" and "https" case
730+
* insensitively, and MAY accommodate other schemes if required.
731+
*
732+
* An empty scheme is equivalent to removing the scheme.
733+
*
734+
* @param string $scheme The scheme to use with the new instance.
735+
*
736+
* @return static A new instance with the specified scheme.
737+
*
738+
* @throws InvalidArgumentException for invalid or unsupported schemes.
739+
*/
740+
public function withScheme(string $scheme)
741+
{
742+
$uri = clone $this;
743+
744+
$scheme = strtolower($scheme);
745+
746+
$uri->scheme = preg_replace('#:(//)?$#', '', $scheme);
747+
748+
return $uri;
749+
}
750+
722751
/**
723752
* Sets the userInfo/Authority portion of the URI.
724753
*

tests/system/HTTP/URITest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,44 @@ public function testSetSchemeSetsValue()
240240
$this->assertSame($expected, (string) $uri);
241241
}
242242

243+
public function testWithScheme()
244+
{
245+
$url = 'example.com';
246+
$uri = new URI('http://' . $url);
247+
248+
$new = $uri->withScheme('x');
249+
250+
$this->assertSame('x://' . $url, (string) $new);
251+
$this->assertSame('http://' . $url, (string) $uri);
252+
}
253+
254+
public function testWithSchemeSetsHttps()
255+
{
256+
$url = 'http://example.com/path';
257+
$uri = new URI($url);
258+
259+
$new = $uri->withScheme('https');
260+
261+
$this->assertSame('https', $new->getScheme());
262+
$this->assertSame('http', $uri->getScheme());
263+
264+
$expected = 'https://example.com/path';
265+
$this->assertSame($expected, (string) $new);
266+
$expected = 'http://example.com/path';
267+
$this->assertSame($expected, (string) $uri);
268+
}
269+
270+
public function testWithSchemeSetsEmpty()
271+
{
272+
$url = 'example.com';
273+
$uri = new URI('http://' . $url);
274+
275+
$new = $uri->withScheme('');
276+
277+
$this->assertSame($url, (string) $new);
278+
$this->assertSame('http://' . $url, (string) $uri);
279+
}
280+
243281
public function testSetUserInfoSetsValue()
244282
{
245283
$url = 'http://example.com/path';

user_guide_src/source/changelogs/v4.4.0.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ Deprecations
216216
``$tokenName``, ``$headerName``, ``$expires``, ``$regenerate``, and
217217
``$redirect`` in ``Security`` are deprecated, and no longer used. Use
218218
``$config`` instead.
219-
- **URI:** ``URI::setSilent()`` is deprecated.
219+
- **URI:**
220+
- ``URI::setSilent()`` is deprecated.
221+
- ``URI::setScheme()`` is deprecated. Use ``withScheme()`` instead.
220222

221223
Bugs Fixed
222224
**********

0 commit comments

Comments
 (0)