-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3316 from mhsdesign/feature/neosFlowPsrUriWrapper…
…Utility FEATURE: introduce `Neos\Flow\Http\UriHelper` to work with query parameters
- Loading branch information
Showing
6 changed files
with
97 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Flow\Http; | ||
|
||
use Neos\Utility\Arrays; | ||
use Psr\Http\Message\UriInterface; | ||
|
||
final class UriHelper | ||
{ | ||
// this class only has static helpers | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Merges recursively into the current {@see UriInterface::getQuery} these additional query parameters. | ||
* | ||
* @param array $queryParameters | ||
* @return UriInterface A new instance with the additional query. | ||
*/ | ||
public static function withAdditionalQueryParameters(UriInterface $uri, array $queryParameters): UriInterface | ||
{ | ||
if ($queryParameters === []) { | ||
return $uri; | ||
} | ||
if ($uri->getQuery() === '') { | ||
$mergedQuery = $queryParameters; | ||
} else { | ||
$queryParametersFromUri = []; | ||
parse_str($uri->getQuery(), $queryParametersFromUri); | ||
$mergedQuery = Arrays::arrayMergeRecursiveOverrule($queryParametersFromUri, $queryParameters); | ||
} | ||
return $uri->withQuery(http_build_query($mergedQuery, '', '&')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
namespace Neos\Flow\Tests\Unit\Http; | ||
|
||
/* | ||
* This file is part of the Neos.Flow package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
|
||
use GuzzleHttp\Psr7\Uri; | ||
use Neos\Flow\Http\UriHelper; | ||
use Neos\Flow\Tests\UnitTestCase; | ||
|
||
class UriHelperTest extends UnitTestCase | ||
{ | ||
/** @test */ | ||
public function specificationWithoutQueryParametersDontModifyTheUri() | ||
{ | ||
self::assertEquals( | ||
new Uri('http://localhost/index?param1=foo¶m2[0]=bar'), | ||
UriHelper::withAdditionalQueryParameters(new Uri('http://localhost/index?param1=foo¶m2[0]=bar'), []) | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function queryParametersAddedToUriWithoutQueryParameters() | ||
{ | ||
self::assertEquals( | ||
new Uri('http://localhost/index?param=123'), | ||
UriHelper::withAdditionalQueryParameters(new Uri('http://localhost/index'), ['param' => 123]) | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function nestedQueryParametersAreMergedCorrectly() | ||
{ | ||
self::assertEquals( | ||
new Uri('http://localhost/index?param1=foo¶m2[a]=bar¶m2[b]=huhu¶m3=123'), | ||
UriHelper::withAdditionalQueryParameters( | ||
new Uri('http://localhost/index?param1=foo¶m2[a]=bar'), | ||
[ | ||
'param2' => [ | ||
'b' => 'huhu' | ||
], | ||
'param3' => 123, | ||
] | ||
) | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters