Skip to content

Commit

Permalink
fix: add some changes to fixing self link on related responses
Browse files Browse the repository at this point in the history
  • Loading branch information
lindyhopchris committed Jun 26, 2024
1 parent 9ac2839 commit ad1d591
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/Core/Document/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,23 @@ public function hasRelated(): bool
return $this->has('related');
}

/**
* @return $this
*/
public function relatedAsSelf(): self
{
if($this->forget('self')->hasRelated()){
$this->push(Link::fromArray('self', $this->getRelated()->toArray()));
$related = $this->getRelated();

if ($related) {
$this->push(new Link(
key: 'self',
href: $related->href(),
meta: $related->meta(),
));
return $this->forget('related');
}
return $this->forget('related');

return $this->forget('self');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function toResponse($request)
->withResources($this->related)
->withJsonApi($this->jsonApi())
->withMeta($this->allMeta())
->withLinks($this->allLinks()->relatedAsSelf())
->withLinks($this->allLinks())
->toJson($this->encodeOptions);

return new Response(
Expand All @@ -69,4 +69,17 @@ public function toResponse($request)
$this->headers()
);
}

/**
* Get all links.
*
* @return Links
*/
private function allLinks(): Links
{
return $this
->linksForRelationship()
->relatedAsSelf()
->merge($this->links());
}
}
15 changes: 14 additions & 1 deletion src/Core/Responses/Internal/RelatedResourceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function toResponse($request)
->withResource($this->related)
->withJsonApi($this->jsonApi())
->withMeta($this->allMeta())
->withLinks($this->allLinks()->relatedAsSelf())
->withLinks($this->allLinks())
->toJson($this->encodeOptions);

return new Response(
Expand All @@ -70,4 +70,17 @@ public function toResponse($request)
$this->headers()
);
}

/**
* Get all links.
*
* @return Links
*/
private function allLinks(): Links
{
return $this
->linksForRelationship()
->relatedAsSelf()
->merge($this->links());
}
}

0 comments on commit ad1d591

Please sign in to comment.