Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion Factory/PsrHttpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ public function createRequest(Request $symfonyRequest)
$request = $request->withAttribute($key, $value);
}

return $request;
$queryString = urldecode($symfonyRequest->server->get('QUERY_STRING', ''));

return $request
->withUri(
$request
->getUri()
->withQuery($queryString)
)
->withQueryParams(iterator_to_array($this->parseStr($queryString)));
}

/**
Expand Down Expand Up @@ -165,4 +173,35 @@ public function createResponse(Response $symfonyResponse)

return $response;
}

/**
* Custom parse_str() function that doesn't alter the parameters key value.
*
* @return \Generator<string, string>
*/
private function parseStr(string $queryString): \Generator
{
parse_str(
array_reduce(
['urldecode', 'bin2hex'],
static function (string $queryString, callable $callback): string {
return (string) preg_replace_callback(
'/(?<key>[^&=]+?)(?:\[[^&=]*\])?=(?<value>[^&=]+)/',
static function (array $match) use ($callback): string {
return str_replace($match['key'], $callback($match['key']), $match[0]);
},
$queryString
);
},
$queryString
),
$parameters
);

foreach ($parameters as $key => $value) {
yield (string) hex2bin($key) => $value;
}

return yield from [];
}
}
15 changes: 13 additions & 2 deletions Tests/Functional/CovertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function testConvertRequestMultipleTimes($request, $firstFactory, $second
$this->assertEquals($request->getMethod(), $finalRequest->getMethod());
$this->assertEquals($request->getRequestTarget(), $finalRequest->getRequestTarget());
$this->assertEquals((string) $request->getUri(), (string) $finalRequest->getUri());
$this->assertEquals($request->getUri()->getQuery(), $finalRequest->getUri()->getQuery());
$this->assertEquals((string) $request->getBody(), (string) $finalRequest->getBody());
$this->assertEquals($strToLower($request->getHeaders()), $strToLower($finalRequest->getHeaders()));
$this->assertEquals($request->getProtocolVersion(), $finalRequest->getProtocolVersion());
Expand Down Expand Up @@ -152,10 +153,20 @@ public function requestProvider()
$psr17Factory = new PsrHttpFactory($nyholmFactory, $nyholmFactory, $nyholmFactory, $nyholmFactory);
$symfonyFactory = new HttpFoundationFactory();

$psr7ServerRequestWithDots = (new Psr7Request('GET', 'http://localhost/api?foo.bar=foodotbar&foo.led[]=IamNot&foo.led[]=fooled&and another one=please fix me'))
->withQueryParams([
'foo.bar' => 'foodotbar',
'foo.led' => [
'IamNot',
'fooled',
],
'and another one' => 'please fix me',
]);

return array_merge([
[$sfRequest, $psr17Factory, $symfonyFactory],
], array_map(function ($psr7Request) use ($symfonyFactory, $psr17Factory) {
return [$psr7Request, $symfonyFactory, $psr17Factory];
], array_map(function ($psr7Request) use ($symfonyFactory, $psr17Factory, $psr7ServerRequestWithDots) {
return [$psr7Request, $symfonyFactory, $psr17Factory, $psr7ServerRequestWithDots];
}, $psr7Requests));
}

Expand Down