Skip to content

Commit

Permalink
Fixing relationships: one element vs many
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Jun 22, 2016
1 parent b1f251f commit 14d15ef
Showing 1 changed file with 71 additions and 32 deletions.
103 changes: 71 additions & 32 deletions src/Helpers/DataIncludedHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,7 @@ public static function setResponseDataIncluded(array &$mappings, array $array, a
if (\is_array($value)) {
foreach ($value as $inArrayValue) {
if (\is_array($inArrayValue)) {

//Remove those resources that do not to appear in the getIncludedResources array.
foreach ($inArrayValue as $position => $includableValue) {
if (!empty($mappings[$parentType])
&& count($mappings[$parentType]->getIncludedResources()) > 0
&& false === in_array($includableValue[Serializer::CLASS_IDENTIFIER_KEY], $mappings[$parentType]->getIncludedResources(), true)
) {
unset($inArrayValue[$position]);
}
}

$inArrayValue = self::removeResourcesNotIncluded($mappings, $parentType, $inArrayValue);
self::setResponseDataIncluded($mappings, $inArrayValue, $data, $parentType);
}
}
Expand Down Expand Up @@ -109,10 +99,7 @@ protected static function addToRelationshipsArray(
DataLinksHelper::setResponseDataLinks($mappings, $attribute),
[
JsonApiTransformer::DATA_KEY => [
$propertyName => PropertyHelper::setResponseDataTypeAndId(
$mappings,
$attribute
),
$propertyName => PropertyHelper::setResponseDataTypeAndId($mappings, $attribute),
],
],
$mappings[$type]->getRelationships()
Expand Down Expand Up @@ -152,8 +139,7 @@ protected static function addToIncludedArray(array &$mappings, array &$data, arr
$mappings,
$relationshipData,
$value,
$value[Serializer::CLASS_IDENTIFIER_KEY],
$attributes
$value[Serializer::CLASS_IDENTIFIER_KEY]
);

if ($relationshipData) {
Expand All @@ -162,17 +148,7 @@ protected static function addToIncludedArray(array &$mappings, array &$data, arr
$relationshipData
);

//Enforce with this check that each property leads to a data element.
$relationships = [];
foreach ($arrayData[JsonApiTransformer::RELATIONSHIPS_KEY] as $attribute => $value) {
//if $value[data] is not found, get next level where [data] should exist.
if (!array_key_exists(JsonApiTransformer::DATA_KEY, $value)) {
array_shift($value);
$relationships[$attribute] = $value;
} else {
$relationships[$attribute] = $value;
}
}
$relationships = self::normalizeRelationshipData($value, $arrayData);
$arrayData[JsonApiTransformer::RELATIONSHIPS_KEY] = $relationships;
}

Expand All @@ -187,6 +163,17 @@ protected static function addToIncludedArray(array &$mappings, array &$data, arr
}
}

/**
* @param array $includedData
*
* @return bool
*/
protected static function hasIdKey(array &$includedData)
{
return \array_key_exists(JsonApiTransformer::ID_KEY, $includedData)
&& !empty($includedData[JsonApiTransformer::ID_KEY]);
}

/**
* @param array $mappings
* @param array $data
Expand Down Expand Up @@ -216,13 +203,65 @@ protected static function addRelationshipsToIncludedResources(
}

/**
* @param array $includedData
* Enforce with this check that each property leads to a data element.
*
* @param array $value
* @param array $arrayData
*
* @return array
*/
protected static function normalizeRelationshipData(array &$value, array $arrayData)
{
$relationships = [];
foreach ($arrayData[JsonApiTransformer::RELATIONSHIPS_KEY] as $attribute => $value) {

//if $value[data] is not found, get next level where [data] should exist.
if (!array_key_exists(JsonApiTransformer::DATA_KEY, $value)) {
array_shift($value);
}

//If one value in $value[data], remove the array and make it object only.
if (1 === count($value[JsonApiTransformer::DATA_KEY])) {
$value = reset($value[JsonApiTransformer::DATA_KEY]);
}

$relationships[$attribute] = $value;
}

return $relationships;
}

/**
* Remove those resources that do not to appear in the getIncludedResources array.
*
* @param array $mappings
* @param string $parentType
* @param array $inArrayValue
*
* @return array
*/
protected static function removeResourcesNotIncluded(array &$mappings, $parentType, array $inArrayValue)
{
foreach ($inArrayValue as $position => $includeValue) {
if (self::isDeleteableIncludedResource($mappings, $parentType, $includeValue)) {
unset($inArrayValue[$position]);
}
}

return $inArrayValue;
}

/**
* @param array $mappings
* @param $parentType
* @param $includeValue
*
* @return bool
*/
protected static function hasIdKey(array &$includedData)
protected static function isDeleteableIncludedResource(array &$mappings, $parentType, $includeValue)
{
return \array_key_exists(JsonApiTransformer::ID_KEY, $includedData)
&& !empty($includedData[JsonApiTransformer::ID_KEY]);
return !empty($mappings[$parentType])
&& count($mappings[$parentType]->getIncludedResources()) > 0
&& false === in_array($includeValue[Serializer::CLASS_IDENTIFIER_KEY], $mappings[$parentType]->getIncludedResources(), true);
}
}

0 comments on commit 14d15ef

Please sign in to comment.