Skip to content

Commit 12888fb

Browse files
authored
feat: add immutable URI query variable helpers (#10242)
1 parent c2dcbbd commit 12888fb

5 files changed

Lines changed: 110 additions & 0 deletions

File tree

‎system/HTTP/URI.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,38 @@ public function addQuery(string $key, $value = null)
875875
return $this;
876876
}
877877

878+
/**
879+
* Returns an instance with one query var added or replaced.
880+
*
881+
* Note: Method not in PSR-7
882+
*/
883+
public function withQueryVar(string $key, int|string|null $value): static
884+
{
885+
$uri = clone $this;
886+
887+
$uri->query[$key] = $value;
888+
889+
return $uri;
890+
}
891+
892+
/**
893+
* Returns an instance with multiple query vars added or replaced.
894+
*
895+
* Note: Method not in PSR-7
896+
*
897+
* @param array<string, int|string|null> $params
898+
*/
899+
public function withQueryVars(array $params): static
900+
{
901+
$uri = clone $this;
902+
903+
foreach ($params as $key => $value) {
904+
$uri->query[$key] = $value;
905+
}
906+
907+
return $uri;
908+
}
909+
878910
/**
879911
* Removes one or more query vars from the URI.
880912
*

‎tests/system/HTTP/URITest.php‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,57 @@ public function testAddQueryVarRespectsExistingQueryVars(): void
835835
$this->assertSame('http://example.com/foo?bar=baz&baz=foz', (string) $uri);
836836
}
837837

838+
public function testWithQueryVarAddsQueryVarWithoutMutatingOriginal(): void
839+
{
840+
$base = 'http://example.com/foo';
841+
$uri = new URI($base);
842+
843+
$new = $uri->withQueryVar('bar', 'baz');
844+
845+
$this->assertNotSame($uri, $new);
846+
$this->assertSame('http://example.com/foo?bar=baz', (string) $new);
847+
$this->assertSame('http://example.com/foo', (string) $uri);
848+
}
849+
850+
public function testWithQueryVarReplacesQueryVarAndPreservesFragment(): void
851+
{
852+
$base = 'http://example.com/foo?bar=baz#section';
853+
$uri = new URI($base);
854+
855+
$new = $uri->withQueryVar('bar', 'foz');
856+
857+
$this->assertNotSame($uri, $new);
858+
$this->assertSame('http://example.com/foo?bar=foz#section', (string) $new);
859+
$this->assertSame('http://example.com/foo?bar=baz#section', (string) $uri);
860+
}
861+
862+
public function testWithQueryVarKeepsEmptyStringQueryVar(): void
863+
{
864+
$base = 'http://example.com/foo?bar=baz';
865+
$uri = new URI($base);
866+
867+
$new = $uri->withQueryVar('bar', '');
868+
869+
$this->assertNotSame($uri, $new);
870+
$this->assertSame('http://example.com/foo?bar=', (string) $new);
871+
$this->assertSame('http://example.com/foo?bar=baz', (string) $uri);
872+
}
873+
874+
public function testWithQueryVarsAddsAndReplacesWithoutMutatingOriginal(): void
875+
{
876+
$base = 'http://example.com/foo?foo=bar&bar=baz&baz=foz#section';
877+
$uri = new URI($base);
878+
879+
$new = $uri->withQueryVars([
880+
'baz' => 'updated',
881+
'new' => 'value',
882+
]);
883+
884+
$this->assertNotSame($uri, $new);
885+
$this->assertSame('http://example.com/foo?foo=bar&bar=baz&baz=updated&new=value#section', (string) $new);
886+
$this->assertSame('http://example.com/foo?foo=bar&bar=baz&baz=foz#section', (string) $uri);
887+
}
888+
838889
public function testStripQueryVars(): void
839890
{
840891
$base = 'http://example.com/foo?foo=bar&bar=baz&baz=foz';

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ HTTP
287287
- ``CLIRequest`` now supports options with values specified using an equals sign (e.g., ``--option=value``) in addition to the existing space-separated syntax (e.g., ``--option value``).
288288
This provides more flexibility in how you can pass options to CLI requests.
289289
- Added ``$enableStyleNonce`` and ``$enableScriptNonce`` options to ``Config\App`` to automatically add nonces to control whether to add nonces to style-* and script-* directives in the Content Security Policy (CSP) header when CSP is enabled. See :ref:`csp-control-nonce-generation` for details.
290+
- Added ``URI::withQueryVar()`` and ``URI::withQueryVars()`` to return a cloned URI with query variables added or replaced.
290291
- ``URI`` now accepts an optional boolean second parameter in the constructor, defaulting to ``false``, to control how the query string is parsed in instantiation.
291292
This is the behavior of ``->useRawQueryString()`` brought into the constructor for convenience. Previously, you need to call ``$uri->useRawQueryString(true)->setURI($uri)`` to get this behavior.
292293
Now you can simply do ``new URI($uri, true)``.

‎user_guide_src/source/libraries/uri.rst‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,19 @@ parameter is the name of the variable, and the second parameter is the value:
188188

189189
.. literalinclude:: uri/019.php
190190

191+
Changing Query Values Without Mutation
192+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
193+
194+
.. versionadded:: 4.8.0
195+
196+
You can return a new URI instance with one or more query variables added or replaced by using the
197+
``withQueryVar()`` and ``withQueryVars()`` methods. Existing query variables are preserved unless they
198+
are replaced:
199+
200+
.. literalinclude:: uri/028.php
201+
202+
The original URI instance is not modified.
203+
191204
Filtering Query Values
192205
^^^^^^^^^^^^^^^^^^^^^^
193206

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$uri = new \CodeIgniter\HTTP\URI('https://example.com/users?q=bob&page=1');
4+
5+
$nextPage = $uri->withQueryVar('page', 2);
6+
// https://example.com/users?q=bob&page=2
7+
8+
$filtered = $uri->withQueryVars([
9+
'q' => 'alice',
10+
'page' => 1,
11+
'role' => 'admin',
12+
]);
13+
// https://example.com/users?q=alice&page=1&role=admin

0 commit comments

Comments
 (0)