feat: add immutable URI query variable helpers#10242
Conversation
- Add withQueryVar() and withQueryVars() for cloned query updates - Support adding, replacing, and removing query variables - Document immutable query variable updates - Add tests for single and bulk query changes Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
|
I still have some doubts about the When proposing this, I intended to mirror the PSR-7 My current thinking is that we should evaluate the names in terms of the planned transition to a PSR-7-style immutable
In that full API family, the replace-vs-merge distinction becomes clearer: $uri = $uri->withQueryVars([
'page' => 1,
]);replaces the query variable collection, while: $uri = $uri->withAddedQueryVars([
'sort' => 'name',
]);preserves the existing query variables and merges the provided ones into them. That said, I am not fully convinced that So maybe we should follow this transition path (which is basically the initial PR):
Looking forward to some feedback, because at this point I have been thinking about this for too long and IDK which option is better. |
|
Thanks for laying it out this way. After looking at PSR-7 again, I think the second option feels cleaner to me too. The main thing that makes me hesitate about I also noticed that PSR-7 already uses So my preference would be:
|
Description
This PR proposes adding two small helpers to
URIfor changing query variables without mutating the current instance:Both methods return a cloned URI. Existing query variables are preserved, matching keys are replaced, new keys are added, and
nullremoves a key.This is handy for everyday URL-building work: pagination links, filter links, canonical URLs, and other places where code needs to adjust a URI while preserving the original instance.
Quick note on naming: I originally implemented this as
withQueryParam()/withQueryParams(), but later realizedURI.phpalready hasTODOtags forwithQuery()andwithQueryParams()as future immutable versions ofsetQuery()andsetQueryArray(). UsingwithQueryVar()/withQueryVars()keeps those future names free for full-query replacement semantics.Tests cover single and bulk query updates, replacing existing values, removing values with
null, preserving empty strings, preserving fragments, and keeping the original URI unchanged.Checklist: