Closed nyamsprod closed 11 months ago
@kelunik @shadowhand what do you think about the issue and more importantly about the proposed solutions ?
The corresponding PR is currently a draft as the Traversable
objects are still supported.
@nyamsprod I wouldn't support object property iteration at all, but I guess that ship has sailed.
@kelunik http_build_query
is based on the following
$parameters = static fn (object|array $data): array => is_array($data) ? $data : get_object_vars($data);
So it does not know or care about anything Traversable
related at all hence the bug. The main reason being that the function predates the introduction of the interface in PHP 😄
@kelunik to avoid needless BC break I proposed the following:
fromParameters
methods because of their inconsistencies but will let them act as is and recommend in the documentation to no longer use them.fromPhpVariable
which will follow the http_buid_query
aka use get_object_vars
on object.@nyamsprod I'd skip Php
in the method name.
will update the method names then thanks for the feedback
Fix has been merge to master via #122
Bug Report
Summary
Data encoding is inconsistent between classes:
URLSearchParams
is not supported withQuery::fromParameters
application/x-www-form-urlencoded
rules are not enforced byURLSearchParams::fromParameters
Standalone code, or other way to reproduce the problem
Expected result
Actual result
TL;DR: formatting rules from
application/x-www-form-urlencoded
are not respected.Source
The issue is because
fromParameters
internally useshttp_build_query
thus the conversion is done following the function algorithm and not the url standard specification.Side effects
A side effect is how
QueryInterface
,URLSearchParams
and most generallyTraversable
objects are supposed to be treated by both named constructors.If we were to fully follow
http_build_query
then they should never be converted into something and just return the empty string. Currently if those objects are submitted they will be formatted prior to submission tohttp_build_query
BUT if they are nested in an array or in a another object they will get converted to the empty string, which is not consistent.Solution Proposal
For consistency, I propose:
QueryInterface
,URLSearchParams
and most generallyTraversable
from allfromParameters
named constructors.http_build_query
independent method forURLSearchParams
to enforcedapplication/x-www-form-urlencoded
rules.For reference the following PR is an attempt to resolve the issue #122
If removing the support for such class is deemed a BC break then the alternative is to provide yet another named constructor and deprecate the current ones with a warning on their usage in the documentation.