Skip to content

Commit a8664e0

Browse files
committed
refactor(http): align URI query helpers with add semantics
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 79cbf3d commit a8664e0

5 files changed

Lines changed: 23 additions & 50 deletions

File tree

system/HTTP/URI.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -876,49 +876,37 @@ public function addQuery(string $key, $value = null)
876876
}
877877

878878
/**
879-
* Return an instance with one query var added, replaced, or removed.
879+
* Return an instance with one query var added or replaced.
880880
*
881881
* Note: Method not in PSR-7
882882
*
883-
* @param int|string|null $value Null removes the query var.
883+
* @param int|string|null $value
884884
*
885885
* @return static
886886
*/
887-
public function withQueryVar(string $key, $value)
887+
public function withAddedQueryVar(string $key, $value)
888888
{
889889
$uri = clone $this;
890890

891-
if ($value === null) {
892-
unset($uri->query[$key]);
893-
894-
return $uri;
895-
}
896-
897891
$uri->query[$key] = $value;
898892

899893
return $uri;
900894
}
901895

902896
/**
903-
* Return an instance with multiple query vars added, replaced, or removed.
897+
* Return an instance with multiple query vars added or replaced.
904898
*
905899
* Note: Method not in PSR-7
906900
*
907-
* @param array<string, int|string|null> $params Null values remove query vars.
901+
* @param array<string, int|string|null> $params
908902
*
909903
* @return static
910904
*/
911-
public function withQueryVars(array $params)
905+
public function withAddedQueryVars(array $params)
912906
{
913907
$uri = clone $this;
914908

915909
foreach ($params as $key => $value) {
916-
if ($value === null) {
917-
unset($uri->query[$key]);
918-
919-
continue;
920-
}
921-
922910
$uri->query[$key] = $value;
923911
}
924912

tests/system/HTTP/URITest.php

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

838-
public function testWithQueryVarAddsQueryVarWithoutMutatingOriginal(): void
838+
public function testWithAddedQueryVarAddsQueryVarWithoutMutatingOriginal(): void
839839
{
840840
$base = 'http://example.com/foo';
841841
$uri = new URI($base);
842842

843-
$new = $uri->withQueryVar('bar', 'baz');
843+
$new = $uri->withAddedQueryVar('bar', 'baz');
844844

845845
$this->assertSame('http://example.com/foo?bar=baz', (string) $new);
846846
$this->assertSame('http://example.com/foo', (string) $uri);
847847
}
848848

849-
public function testWithQueryVarReplacesQueryVarAndPreservesFragment(): void
849+
public function testWithAddedQueryVarReplacesQueryVarAndPreservesFragment(): void
850850
{
851851
$base = 'http://example.com/foo?bar=baz#section';
852852
$uri = new URI($base);
853853

854-
$new = $uri->withQueryVar('bar', 'foz');
854+
$new = $uri->withAddedQueryVar('bar', 'foz');
855855

856856
$this->assertSame('http://example.com/foo?bar=foz#section', (string) $new);
857857
$this->assertSame('http://example.com/foo?bar=baz#section', (string) $uri);
858858
}
859859

860-
public function testWithQueryVarRemovesQueryVarWhenValueIsNull(): void
861-
{
862-
$base = 'http://example.com/foo?foo=bar&bar=baz&baz=foz';
863-
$uri = new URI($base);
864-
865-
$new = $uri->withQueryVar('bar', null);
866-
867-
$this->assertSame('http://example.com/foo?foo=bar&baz=foz', (string) $new);
868-
$this->assertSame('http://example.com/foo?foo=bar&bar=baz&baz=foz', (string) $uri);
869-
}
870-
871-
public function testWithQueryVarKeepsEmptyStringQueryVar(): void
860+
public function testWithAddedQueryVarKeepsEmptyStringQueryVar(): void
872861
{
873862
$base = 'http://example.com/foo?bar=baz';
874863
$uri = new URI($base);
875864

876-
$new = $uri->withQueryVar('bar', '');
865+
$new = $uri->withAddedQueryVar('bar', '');
877866

878867
$this->assertSame('http://example.com/foo?bar=', (string) $new);
879868
$this->assertSame('http://example.com/foo?bar=baz', (string) $uri);
880869
}
881870

882-
public function testWithQueryVarsAddsReplacesAndRemovesWithoutMutatingOriginal(): void
871+
public function testWithAddedQueryVarsAddsAndReplacesWithoutMutatingOriginal(): void
883872
{
884873
$base = 'http://example.com/foo?foo=bar&bar=baz&baz=foz#section';
885874
$uri = new URI($base);
886875

887-
$new = $uri->withQueryVars([
888-
'bar' => null,
876+
$new = $uri->withAddedQueryVars([
889877
'baz' => 'updated',
890878
'new' => 'value',
891879
]);
892880

893-
$this->assertSame('http://example.com/foo?foo=bar&baz=updated&new=value#section', (string) $new);
881+
$this->assertSame('http://example.com/foo?foo=bar&bar=baz&baz=updated&new=value#section', (string) $new);
894882
$this->assertSame('http://example.com/foo?foo=bar&bar=baz&baz=foz#section', (string) $uri);
895883
}
896884

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ HTTP
276276
- ``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``).
277277
This provides more flexibility in how you can pass options to CLI requests.
278278
- 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.
279-
- Added ``URI::withQueryVar()`` and ``URI::withQueryVars()`` to return a cloned URI with query variables added, replaced, or removed.
279+
- Added ``URI::withAddedQueryVar()`` and ``URI::withAddedQueryVars()`` to return a cloned URI with query variables added or replaced.
280280
- ``URI`` now accepts an optional boolean second parameter in the constructor, defaulting to ``false``, to control how the query string is parsed in instantiation.
281281
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.
282282
Now you can simply do ``new URI($uri, true)``.

user_guide_src/source/libraries/uri.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ Changing Query Values Without Mutation
193193

194194
.. versionadded:: 4.8.0
195195

196-
You can return a new URI instance with one or more query variables changed by using the ``withQueryVar()``
197-
and ``withQueryVars()`` methods. Existing query variables are preserved unless they are replaced or removed.
198-
Passing ``null`` removes a query variable:
196+
You can return a new URI instance with one or more query variables added or replaced by using the
197+
``withAddedQueryVar()`` and ``withAddedQueryVars()`` methods. Existing query variables are preserved unless they
198+
are replaced:
199199

200200
.. literalinclude:: uri/028.php
201201

user_guide_src/source/libraries/uri/028.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
$uri = new \CodeIgniter\HTTP\URI('https://example.com/users?q=bob&page=1');
44

5-
$nextPage = $uri->withQueryVar('page', 2);
5+
$nextPage = $uri->withAddedQueryVar('page', 2);
66
// https://example.com/users?q=bob&page=2
77

8-
$withoutSearch = $uri->withQueryVar('q', null);
9-
// https://example.com/users?page=1
10-
11-
$filtered = $uri->withQueryVars([
12-
'q' => null,
8+
$filtered = $uri->withAddedQueryVars([
9+
'q' => 'alice',
1310
'page' => 1,
1411
'role' => 'admin',
1512
]);
16-
// https://example.com/users?page=1&role=admin
13+
// https://example.com/users?q=alice&page=1&role=admin

0 commit comments

Comments
 (0)