Skip to content
Merged
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
35 changes: 35 additions & 0 deletions src/Metadata/Tests/Util/IriHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,41 @@ public function testHelpersWithNetworkPath(): void
$this->assertSame('//foo:bar@localhost:443/hello.json?foo=bar&bar=3&page=2#foo', IriHelper::createIri($parsed['parts'], $parsed['parameters'], 'page', 2., UrlGeneratorInterface::NET_PATH));
}

public function testHelpersPreserveNestedArrayQueryParameters(): void
{
$parts = [
'path' => '/objects',
'query' => '',
];
$parameters = [
'filters' => [
0 => ['a' => 'foo', 'b' => 'bar'],
],
];

$iri = IriHelper::createIri($parts, $parameters, 'page', 2.);

$this->assertSame('/objects?filters%5B0%5D%5Ba%5D=foo&filters%5B0%5D%5Bb%5D=bar&page=2', $iri);

$reparsed = IriHelper::parseIri($iri, 'page');
$this->assertSame($parameters, $reparsed['parameters']);
}

public function testHelpersCollapseSimpleListQueryParameters(): void
{
$parts = [
'path' => '/objects',
'query' => '',
];
$parameters = [
'foo' => ['a', 'b'],
];

$iri = IriHelper::createIri($parts, $parameters, 'page', 2.);

$this->assertSame('/objects?foo%5B%5D=a&foo%5B%5D=b&page=2', $iri);
}

public function testParseIriWithInvalidUrl(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
5 changes: 4 additions & 1 deletion src/Metadata/Util/IriHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public static function createIri(array $parts, array $parameters, ?string $pageP
}

$query = http_build_query($parameters, '', '&', \PHP_QUERY_RFC3986);
$parts['query'] = preg_replace('/%5B\d+%5D/', '%5B%5D', $query);
// Only collapse a numeric index when it is the leaf segment of a bracket chain
// (a simple list element). Collapsing a non-leaf index would merge distinct keys of a
// nested array into separate elements (e.g. filters[0][a] must stay filters[0][a]).
$parts['query'] = preg_replace('/%5B\d+%5D(?!%5B)/', '%5B%5D', $query);

$url = '';
if ((UrlGeneratorInterface::ABS_URL === $urlGenerationStrategy || UrlGeneratorInterface::NET_PATH === $urlGenerationStrategy) && isset($parts['host'])) {
Expand Down
Loading