Skip to content

Commit c68fd6b

Browse files
committed
Add ability include to resource array attributes with scalar values.
Also include meta to response when serialized target is array.
1 parent 5656809 commit c68fd6b

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

Diff for: src/Helpers/DataAttributesHelper.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static function setResponseDataAttributes(array &$mappings, array &$array
119119
}
120120

121121
if (\is_array($value) && !array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $value)) {
122-
if (self::containsClassIdentifierKey($value)) {
122+
if (self::containsClassIdentifierKey($value) || self::arrayWithScalarValue($value)) {
123123
$attributes[$keyName] = $value;
124124
}
125125
}
@@ -186,4 +186,28 @@ protected static function containsClassIdentifierKey(array $input, $foundIdentif
186186

187187
return !$foundIdentifierKey;
188188
}
189+
190+
/**
191+
* @param array $input
192+
*
193+
* @return bool
194+
*/
195+
protected static function arrayWithScalarValue(array $input)
196+
{
197+
if (!is_array($input)) {
198+
return false;
199+
}
200+
201+
if (!empty($input[Serializer::SCALAR_VALUE])) {
202+
$input = $input[Serializer::SCALAR_VALUE];
203+
204+
if (\is_array($input)) {
205+
foreach ($input as $value) {
206+
return self::isScalarValue($value);
207+
}
208+
}
209+
}
210+
211+
return false;
212+
}
189213
}

Diff for: src/Helpers/DataIncludedHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function setResponseDataIncluded(array &$mappings, array $array, a
4444
//Remove those resources that do not to appear in the getIncludedResources array.
4545
foreach ($inArrayValue as $position => $includableValue) {
4646
if (!empty($mappings[$parentType]) && $mappings[$parentType]->isFilteringIncludedResources()
47-
&& false === in_array(
47+
&& isset($includableValue[Serializer::CLASS_IDENTIFIER_KEY]) && false === in_array(
4848
$includableValue[Serializer::CLASS_IDENTIFIER_KEY],
4949
$mappings[$parentType]->getIncludedResources(),
5050
true

Diff for: src/JsonApiTransformer.php

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ protected function serializedArray(array $value)
213213
$data = array_filter($data);
214214

215215
$this->setResponseLinks($value, $data);
216+
$this->setResponseMeta($data);
216217
$this->setResponseVersion($data);
217218

218219
return (empty($data['data'])) ? array_merge(['data' => []], $data) : $data;

0 commit comments

Comments
 (0)