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-04 09:37:47 +02:00
1 parent 857983643e
commit d71aedae66
3 files changed
+94 -1

No files matched your search

+7 -1
View File
@@ -65,6 +65,12 @@ class QueryParameterBag implements \Stringable
public function __toString(): string
{
return http_build_query($this->parameters, '', '&', PHP_QUERY_RFC3986);
// Parameters without a value are stored as null (for example "?foo"),
// but http_build_query() skips null values entirely, which would drop
// parameters that has() still reports as present. Render them with an
// empty value so they survive a round-trip.
$parameters = array_map(fn ($value) => $value ?? '', $this->parameters);
return http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);
}
}