fix: keep valueless query parameters when casting to a string

Valueless query parameters (for example ?foo) are stored as null, and has()
reports them as present, but __toString() passed those null values to
http_build_query() which skips them entirely, dropping the parameters from the
output. Render null values as empty-valued parameters so they survive a
round-trip.
This commit is contained in:
dualfroz committed 2026-09-06 00:59:19 +02:00
1 parent 53eebaebee
commit ef1f80eb18
2 files changed
+20 -1

No files matched your search

+5 -1
View File
@@ -65,6 +65,10 @@ class QueryParameterBag implements \Stringable
public function __toString(): string
{
return http_build_query($this->parameters, '', '&', PHP_QUERY_RFC3986);
// Valueless parameters are stored as null, which http_build_query() skips;
// render them with an empty value so they survive.
$parameters = array_map(fn ($value) => $value ?? '', $this->parameters);
return http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);
}
}