Skip to content

Commit dcf3e4a

Browse files
committed
JSONAPI Included resources now include the relationships member too when needed
1 parent 44ce1b5 commit dcf3e4a

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

src/Helpers/DataIncludedHelper.php

+51-9
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,58 @@ private static function addToIncludedArray(array &$mappings, array &$data, array
100100
$includedData = PropertyHelper::setResponseDataTypeAndId($mappings, $value);
101101

102102
if (self::hasIdKey($includedData)) {
103-
$data[JsonApiTransformer::INCLUDED_KEY][] = array_filter(
104-
array_merge(
105-
[
106-
JsonApiTransformer::TYPE_KEY => $includedData[JsonApiTransformer::TYPE_KEY],
107-
JsonApiTransformer::ID_KEY => $includedData[JsonApiTransformer::ID_KEY],
108-
JsonApiTransformer::ATTRIBUTES_KEY => $attributes,
109-
],
110-
DataLinksHelper::setResponseDataLinks($mappings, $value)
111-
)
103+
$arrayData = array_merge(
104+
[
105+
JsonApiTransformer::TYPE_KEY => $includedData[JsonApiTransformer::TYPE_KEY],
106+
JsonApiTransformer::ID_KEY => $includedData[JsonApiTransformer::ID_KEY],
107+
JsonApiTransformer::ATTRIBUTES_KEY => $attributes,
108+
JsonApiTransformer::RELATIONSHIPS_KEY => [],
109+
],
110+
DataLinksHelper::setResponseDataLinks($mappings, $value)
111+
);
112+
113+
$relationshipData = [];
114+
self::addRelationshipsToIncludedResources(
115+
$mappings,
116+
$relationshipData,
117+
$value,
118+
$value[Serializer::CLASS_IDENTIFIER_KEY],
119+
$attributes
112120
);
121+
122+
if ($relationshipData) {
123+
$arrayData[JsonApiTransformer::RELATIONSHIPS_KEY] = array_merge(
124+
$arrayData[JsonApiTransformer::RELATIONSHIPS_KEY],
125+
$relationshipData
126+
);
127+
}
128+
129+
$data[JsonApiTransformer::INCLUDED_KEY][] = array_filter($arrayData);
130+
}
131+
}
132+
}
133+
134+
/**
135+
* @param array $mappings
136+
* @param array $data
137+
* @param array $value
138+
* @param string $type
139+
*/
140+
private static function addRelationshipsToIncludedResources(
141+
array &$mappings,
142+
array &$data,
143+
array &$value,
144+
$type
145+
) {
146+
foreach ($value as $propertyName => $attribute) {
147+
if (PropertyHelper::isAttributeProperty($mappings, $propertyName, $type)) {
148+
$propertyName = DataAttributesHelper::transformToValidMemberName($propertyName);
149+
150+
if (is_array($attribute) && array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $attribute)) {
151+
$data[$propertyName][JsonApiTransformer::DATA_KEY] = PropertyHelper::setResponseDataTypeAndId($mappings, $attribute);
152+
153+
continue;
154+
}
113155
}
114156
}
115157
}

tests/JsonApiTransformerTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ public function testItWillSerializeToJsonApiAComplexObject()
210210
},
211211
"comment": "Have no fear, sers, your king is safe."
212212
},
213+
"relationships": {
214+
"user": {
215+
"data": {
216+
"type": "user",
217+
"id": "2"
218+
}
219+
}
220+
},
213221
"links": {
214222
"self": { "href": "http://example.com/comments/1000" }
215223
}

0 commit comments

Comments
 (0)