Skip to content

Commit

Permalink
Merge 2c4ca3b into 746a07f
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt A committed Sep 22, 2016
2 parents 746a07f + 2c4ca3b commit 03daeb1
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 30 deletions.
4 changes: 4 additions & 0 deletions src/Scope.php
Expand Up @@ -258,6 +258,10 @@ public function toArray()
$data = array_merge($data, $includedData);
}

if (!empty($this->availableIncludes)) {
$data = $serializer->injectAvailableIncludeData($data, $this->availableIncludes);
}

if ($this->resource instanceof Collection) {
if ($this->resource->hasCursor()) {
$pagination = $serializer->cursor($this->resource->getCursor());
Expand Down
69 changes: 50 additions & 19 deletions src/Serializer/JsonApiSerializer.php
Expand Up @@ -464,15 +464,6 @@ private function fillRelationshipAsCollection($data, $relationship, $key)
{
foreach ($relationship as $index => $relationshipData) {
$data['data'][$index]['relationships'][$key] = $relationshipData;

if ($this->shouldIncludeLinks()) {
$data['data'][$index]['relationships'][$key] = array_merge([
'links' => [
'self' => "{$this->baseUrl}/{$data['data'][$index]['type']}/{$data['data'][$index]['id']}/relationships/$key",
'related' => "{$this->baseUrl}/{$data['data'][$index]['type']}/{$data['data'][$index]['id']}/$key",
],
], $data['data'][$index]['relationships'][$key]);
}
}

return $data;
Expand All @@ -490,16 +481,6 @@ private function fillRelationshipAsSingleResource($data, $relationship, $key)
{
$data['data']['relationships'][$key] = $relationship[0];

if ($this->shouldIncludeLinks()) {
$data['data']['relationships'][$key] = array_merge([
'links' => [
'self' => "{$this->baseUrl}/{$data['data']['type']}/{$data['data']['id']}/relationships/$key",
'related' => "{$this->baseUrl}/{$data['data']['type']}/{$data['data']['id']}/$key",
],
], $data['data']['relationships'][$key]);

return $data;
}
return $data;
}

Expand Down Expand Up @@ -572,4 +553,54 @@ private function addIncludedDataToRelationship($includeObject, $relationship)

return $relationship;
}

/**
* {@inheritdoc}
*/
public function injectAvailableIncludeData($data, $availableIncludes)
{
if (!$this->shouldIncludeLinks()) {
return $data;
}

if ($this->isCollection($data)) {
$data['data'] = array_map(function ($resource) use ($availableIncludes) {
foreach ($availableIncludes as $relationshipKey) {
$resource = $this->addRelationshipLinks($resource, $relationshipKey);
}
return $resource;
}, $data['data']);
} else {
foreach ($availableIncludes as $relationshipKey) {
$data['data'] = $this->addRelationshipLinks($data['data'], $relationshipKey);
}
}

return $data;
}

/**
* Adds links for all available includes to a single resource.
*
* @param array $resource The resource to add relationship links to
* @param string $relationshipKey The resource key of the relationship
*/
private function addRelationshipLinks($resource, $relationshipKey)
{
if (!isset($resource['relationships']) || !isset($resource['relationships'][$relationshipKey])) {
$resource['relationships'][$relationshipKey] = [];
}

$resource['relationships'][$relationshipKey] = array_merge(
[
'links' => [
'self' => "{$this->baseUrl}/{$resource['type']}/{$resource['id']}/relationships/{$relationshipKey}",
'related' => "{$this->baseUrl}/{$resource['type']}/{$resource['id']}/{$relationshipKey}",
]
],
$resource['relationships'][$relationshipKey]
);

return $resource;
}
}
13 changes: 13 additions & 0 deletions src/Serializer/SerializerAbstract.php
Expand Up @@ -115,6 +115,19 @@ public function injectData($data, $rawIncludedData)
return $data;
}

/**
* Hook for the serializer to inject custom data based on the available includes of the resource.
*
* @param array $data
* @param array $availableIncludes
*
* @return array
*/
public function injectAvailableIncludeData($data, $availableIncludes)
{
return $data;
}

/**
* Hook for the serializer to modify the final list of includes.
*
Expand Down

0 comments on commit 03daeb1

Please sign in to comment.