Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ecdd581

Browse filesBrowse files
committed
[HttpFoundation] Allow query params to be formated with a preservered order
1 parent b0facfe commit ecdd581
Copy full SHA for ecdd581

File tree

2 files changed

+38
-8
lines changed
Filter options

2 files changed

+38
-8
lines changed

‎src/Symfony/Component/HttpFoundation/Request.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Request.php
+10-6Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -616,14 +616,15 @@ public static function getTrustedHosts()
616616
/**
617617
* Normalizes a query string.
618618
*
619-
* It builds a normalized query string, where keys/value pairs are alphabetized,
619+
* It builds a normalized query string, where keys/value pairs are alphabetized (by default),
620620
* have consistent escaping and unneeded delimiters are removed.
621621
*
622622
* @param string $qs Query string
623+
* @param bool $preserveOrder Flag to preserve the querystring from being alphabetically ordered
623624
*
624625
* @return string A normalized query string for the Request
625626
*/
626-
public static function normalizeQueryString($qs)
627+
public static function normalizeQueryString($qs, $preserveOrder = false)
627628
{
628629
if ('' == $qs) {
629630
return '';
@@ -651,7 +652,9 @@ public static function normalizeQueryString($qs)
651652
$order[] = urldecode($keyValuePair[0]);
652653
}
653654

654-
array_multisort($order, SORT_ASC, $parts);
655+
if ($preserveOrder !== true) {
656+
array_multisort($order, SORT_ASC, $parts);
657+
}
655658

656659
return implode('&', $parts);
657660
}
@@ -1103,13 +1106,14 @@ public function getRelativeUriForPath($path)
11031106
* Generates the normalized query string for the Request.
11041107
*
11051108
* It builds a normalized query string, where keys/value pairs are alphabetized
1106-
* and have consistent escaping.
1109+
* (by default) and have consistent escaping.
11071110
*
1111+
* @param bool $preserveOrder Flag to preserve the querystring from being alphabetically ordered
11081112
* @return string|null A normalized query string for the Request
11091113
*/
1110-
public function getQueryString()
1114+
public function getQueryString($preserveOrder = false)
11111115
{
1112-
$qs = static::normalizeQueryString($this->server->get('QUERY_STRING'));
1116+
$qs = static::normalizeQueryString($this->server->get('QUERY_STRING'), $preserveOrder);
11131117

11141118
return '' === $qs ? null : $qs;
11151119
}

‎src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
+28-2Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,17 +672,43 @@ public function testGetQueryString($query, $expectedQuery, $msg)
672672
$this->assertSame($expectedQuery, $request->getQueryString(), $msg);
673673
}
674674

675+
public function testGetQueryStringSortAlphabeticalyByDefaut()
676+
{
677+
$request = new Request();
678+
679+
$request->server->set('QUERY_STRING', 'foo=bar&bar=');
680+
$this->assertSame('bar=&foo=bar', $request->getQueryString(), 'sorts keys alphabetically');
681+
}
682+
683+
/**
684+
* @dataProvider getQueryStringNormalizationData
685+
*/
686+
public function testGetQueryStringWithoutOrderingDoesNotAffectAnythingElseThanOrder($query, $expectedQuery, $msg)
687+
{
688+
$request = new Request();
689+
690+
$request->server->set('QUERY_STRING', $query);
691+
$this->assertSame($expectedQuery, $request->getQueryString(true), $msg);
692+
}
693+
694+
public function testGetQueryStringPreservesOrder()
695+
{
696+
$request = new Request();
697+
698+
$request->server->set('QUERY_STRING', 'foo=bar&bar=');
699+
$this->assertSame('foo=bar&bar=', $request->getQueryString(true), 'preserves keys order');
700+
}
701+
675702
public function getQueryStringNormalizationData()
676703
{
677704
return array(
678705
array('foo', 'foo', 'works with valueless parameters'),
679706
array('foo=', 'foo=', 'includes a dangling equal sign'),
680707
array('bar=&foo=bar', 'bar=&foo=bar', '->works with empty parameters'),
681-
array('foo=bar&bar=', 'bar=&foo=bar', 'sorts keys alphabetically'),
682708

683709
// GET parameters, that are submitted from a HTML form, encode spaces as "+" by default (as defined in enctype application/x-www-form-urlencoded).
684710
// PHP also converts "+" to spaces when filling the global _GET or when using the function parse_str.
685-
array('him=John%20Doe&her=Jane+Doe', 'her=Jane%20Doe&him=John%20Doe', 'normalizes spaces in both encodings "%20" and "+"'),
711+
array('her=Jane+Doe&him=John%20Doe', 'her=Jane%20Doe&him=John%20Doe', 'normalizes spaces in both encodings "%20" and "+"'),
686712

687713
array('foo[]=1&foo[]=2', 'foo%5B%5D=1&foo%5B%5D=2', 'allows array notation'),
688714
array('foo=1&foo=2', 'foo=1&foo=2', 'allows repeated parameters'),

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.