Skip to content

Commit

Permalink
JsonApiResponseFactory::createResponse() meta and links arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
samizdam committed May 9, 2021
1 parent 07f70b8 commit 21712f5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Data Transfer Object classes generation from swagger spec
- JsonApiResponseFactory::createResponse() `meta` and `links` arguments

## [0.0.14] - 2021-02-02
### Added
Expand Down
17 changes: 11 additions & 6 deletions src/FreeElephants/JsonApiToolkit/Psr/JsonApiResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,31 @@ public function __construct(EncoderInterface $encoder, ResponseFactoryInterface
$this->errorFactory = $errorFactory;
}

public function createResponse($data, ServerRequestInterface $request = null): ResponseInterface
public function createResponse($data, ServerRequestInterface $request = null, $meta = null, array $links = []): ResponseInterface
{
$includedPath = [];
$fieldSets = [];
if ($request) {
$queryParams = $request->getQueryParams();

if (array_key_exists('include', $queryParams)) {
$includedPath = explode(',', $queryParams['include']);
}
}

$fieldSets = [];
if ($request) {
$queryParams = $request->getQueryParams();
if (array_key_exists('fields', $queryParams)) {
foreach ($queryParams['fields'] as $type => $typeFieldsString) {
$fieldSets[$type] = explode(',', $typeFieldsString);
}
}
}
$content = $this->encoder->withIncludedPaths($includedPath)->withFieldSets($fieldSets)->encodeData($data);

$encoder = $this->encoder
->withIncludedPaths($includedPath)
->withFieldSets($fieldSets)
->withMeta($meta)
->withLinks($links);

$content = $encoder->encodeData($data);

$response = $this->createPsrResponse();
$response->getBody()->write($content);
Expand Down

0 comments on commit 21712f5

Please sign in to comment.