Skip to content

Commit 263bbd6

Browse files
committed
Merge pull request #571 from soyuka/fix-composite-identifier-order
Allow random composite identifier order
2 parents f002cf5 + 018165a commit 263bbd6

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

features/composite.feature

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Feature: Retrieve data with Composite identifiers
22
In order to retrieve relations with composite identifiers
33
As a client software developer
4-
I need to retrieve all collections
4+
I need to retrieve all collections
55

66
@createSchema
77
@dropSchema
@@ -77,15 +77,38 @@ Feature: Retrieve data with Composite identifiers
7777
"""
7878

7979
@createSchema
80-
@dropSchema
8180
Scenario: Get the first composite relation
8281
Given there are Composite identifier objects
8382
When I send a "GET" request to "/composite_relations/compositeItem=1;compositeLabel=1"
8483
Then the response status code should be 200
8584
And the response should be in JSON
8685
And the header "Content-Type" should be equal to "application/ld+json"
86+
And the JSON should be equal to:
87+
"""
88+
{
89+
"@context": "\/contexts\/CompositeRelation",
90+
"@id": "\/composite_relations\/compositeItem=1;compositeLabel=1",
91+
"@type": "CompositeRelation",
92+
"value": "somefoobardummy"
93+
}
94+
"""
95+
96+
Scenario: Get the first composite relation with a reverse identifiers order
97+
Given there are Composite identifier objects
98+
When I send a "GET" request to "/composite_relations/compositeLabel=1;compositeItem=1"
99+
Then the response status code should be 200
100+
And the response should be in JSON
101+
And the header "Content-Type" should be equal to "application/ld+json"
102+
And the JSON should be equal to:
103+
"""
104+
{
105+
"@context": "\/contexts\/CompositeRelation",
106+
"@id": "\/composite_relations\/compositeItem=1;compositeLabel=1",
107+
"@type": "CompositeRelation",
108+
"value": "somefoobardummy"
109+
}
110+
"""
87111

88-
@createSchema
89112
@dropSchema
90113
Scenario: Get first composite item
91114
Given there are Composite identifier objects

src/Bridge/Doctrine/Orm/ItemDataProvider.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,24 @@ private function normalizeIdentifiers($id, $manager, $resourceClass) : array
125125
{
126126
$doctrineMetadataIdentifier = $manager->getClassMetadata($resourceClass)->getIdentifier();
127127
$identifierValues = [$id];
128-
$identifierValuesArray = [];
129128

130129
if (count($doctrineMetadataIdentifier) >= 2) {
131-
$identifierValues = explode(';', $id);
132-
foreach ($identifierValues as $key => $value) {
133-
$identifierValueArray = explode('=', $value);
134-
if ($doctrineMetadataIdentifier[$key] === $identifierValueArray[0]) {
135-
$identifierValuesArray[] = $identifierValueArray[1];
136-
}
130+
$identifiers = explode(';', $id);
131+
$identifiersMap = [];
132+
133+
// first transform identifiers to a proper key/value array
134+
foreach ($identifiers as $identifier) {
135+
$keyValue = explode('=', $identifier);
136+
$identifiersMap[$keyValue[0]] = $keyValue[1];
137137
}
138+
139+
$identifierValuesArray = [];
140+
141+
// then, loop through doctrine metadata identifiers to keep the same identifiers order
142+
foreach ($doctrineMetadataIdentifier as $metadataIdentifierKey) {
143+
$identifierValuesArray[] = $identifiersMap[$metadataIdentifierKey];
144+
}
145+
138146
$identifierValues = $identifierValuesArray;
139147
}
140148

0 commit comments

Comments
 (0)