8000 Added a correct return type for Responses::createResponse by zlodes · Pull Request #536 · cloudcreativity/laravel-json-api · GitHub
[go: up one dir, main page]

Skip to content

Added a correct return type for Responses::createResponse #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 55 additions & 38 deletions src/Http/Responses/Responses.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
use CloudCreativity\LaravelJsonApi\Encoder\Neomerx\Factory;
use CloudCreativity\LaravelJsonApi\Routing\Route;
use CloudCreativity\LaravelJsonApi\Utils\Helpers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use InvalidArgumentException;
use Neomerx\JsonApi\Contracts\Document\DocumentInterface;
use Neomerx\JsonApi\Contracts\Document\ErrorInterface;
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
Expand Down Expand Up @@ -115,7 +117,7 @@ public function withCodec(Codec $codec): self
public function withMediaType(string $mediaType): self
{
if (!$encoding = $this->api->getEncodings()->find($mediaType)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
"Media type {$mediaType} is not valid for API {$this->api->getName()}."
);
}
Expand All @@ -135,13 +137,13 @@ public function withMediaType(string $mediaType): self
* @param int $options
* @param int $depth
* @param string|null $mediaType
* @return Responses
* @return $this
*/
public function withEncoding(
int $options = 0,
int $depth = 512,
string $mediaType = MediaTypeInterface::JSON_API_MEDIA_TYPE
) {
): self {
$encoding = Encoding::create(
$mediaType,
$options,
Expand Down Expand Up @@ -174,18 +176,18 @@ public function withEncodingParameters(?EncodingParametersInterface $parameters)
/**
* @param $statusCode
* @param array $headers
* @return mixed
* @return Response
*/
public function statusCode($statusCode, array $headers = [])
public function statusCode($statusCode, array $headers = []): Response
{
return $this->getCodeResponse($statusCode, $headers);
}

/**
* @param array $headers
* @return mixed
* @return Response
*/
public function noContent(array $headers = [])
public function noContent(array $headers = []): Response
{
return $this->getCodeResponse(204, $headers);
}
Expand All @@ -194,9 +196,9 @@ public function noContent(array $headers = [])
* @param $meta
* @param int $statusCode
* @param array $headers
* @return mixed
* @return Response
*/
public function meta($meta, $statusCode = self::HTTP_OK, array $headers = [])
public function meta($meta, $statusCode = self::HTTP_OK, array $headers = []): Response
{
return $this->getMetaResponse($meta, $statusCode, $headers);
}
Expand All @@ -206,9 +208,9 @@ public function meta($meta, $statusCode = self::HTTP_OK, array $headers = [])
* @param $meta
* @param int $statusCode
* @param array $headers
* @return mixed
* @return Response
*/
public function noData(array $links = [], $meta = null, $statusCode = self::HTTP_OK, array $headers = [])
public function noData(array $links = [], $meta = null, $statusCode = self::HTTP_OK, array $headers = []): Response
{
$encoder = $this->getEncoder();
$content = $encoder->withLinks($links)->encodeMeta($meta ?: []);
Expand All @@ -222,30 +224,38 @@ public function noData(array $links = [], $meta = null, $statusCode = self::HTTP
* @param mixed $meta
* @param int $statusCode
* @param array $headers
* @return mixed
* @return Response
*/
public function content(
$data,
array $links = [],
$meta = null,
$statusCode = self::HTTP_OK,
array $headers = []
) {
): Response {
return $this->getContentResponse($data, $statusCode, $links, $meta, $headers);
}


/**
* @inheritdoc
* Get response with regular JSON API Document in body.
*
* @param array|object $data
* @param int $statusCode
* @param null $links
* @param null $meta
* @param array $headers
* @return Response
*/
public function getContentResponse(
$data,
$statusCode = self::HTTP_OK,
$links = null,
$meta = null,
array $headers = []
) {
): Response {
if ($data instanceof PageInterface) {
list ($data, $meta, $links) = $this->extractPage($data, $meta, $links);
[$data, $meta, $links] = $this->extractPage($data, $meta, $links);
}

return parent::getContentResponse($data, $statusCode, $links, $meta, $headers);
Expand All @@ -256,9 +266,9 @@ public function getContentResponse(
* @param array $links
* @param mixed $meta
* @param array $headers
* @return mixed
* @return Response
*/
public function created($resource = null, array $links = [], $meta = null, array $headers = [])
public function created($resource = null, array $links = [], $meta = null, array $headers = []): Response
{
if ($this->isNoContent($resource, $links, $meta)) {
return $this->noContent();
Expand All @@ -282,14 +292,14 @@ public function created($resource = null, array $links = [], $meta = null, array
* @param array $links
* @param mixed $meta
* @param array $headers
* @return mixed
* @return Response
*/
public function updated(
$resource = null,
array $links = [],
$meta = null,
array $headers = []
) {
): Response {
return $this->getResourceResponse($resource, $links, $meta, $headers);
}

Expand All @@ -300,14 +310,14 @@ public function updated(
* @param array $links
* @param mixed|null $meta
* @param array $headers
* @return mixed
* @return Response
*/
public function deleted(
$resource = null,
array $links = [],
$meta = null,
array $headers = []
) {
): Response {
return $this->getResourceResponse($resource, $links, $meta, $headers);
}

Expand All @@ -316,9 +326,9 @@ public function deleted(
* @param array $links
* @param null $meta
* @param array $headers
* @return mixed
* @return Response
*/
public function accepted(AsynchronousProcess $job, array $links = [], $meta = null, array $headers = [])
public function accepted(AsynchronousProcess $job, array $links = [], $meta = null, array $headers = []): Response
{
$headers['Content-Location'] = $this->getResourceLocationUrl($job);

Expand All @@ -330,7 +340,7 @@ public function accepted(AsynchronousProcess $job, array $links = [], $meta = nu
* @param array $links
* @param null $meta
* @param array $headers
* @return \Illuminate\Http\RedirectResponse|mixed
* @return RedirectResponse|mixed
*/
public function process(AsynchronousProcess $job, array $links = [], $meta = null, array $headers = [])
{
Expand All @@ -348,15 +358,15 @@ public function process(AsynchronousProcess $job, array $links = [], $meta = nul
* @param mixed $meta
* @param int $statusCode
* @param array $headers
* @return mixed
* @return Response
*/
public function relationship(
$data,
array $links = [],
$meta = null,
$statusCode = 200,
array $headers = []
) {
): Response {
return $this->getIdentifiersResponse($data, $statusCode, $links, $meta, $headers);
}

Expand All @@ -366,17 +376,17 @@ public function relationship(
* @param $links
* @param $meta
* @param array $headers
* @return mixed
* @return Response
*/
public function getIdentifiersResponse(
$data,
$statusCode = self::HTTP_OK,
$links = null,
$meta = null,
array $headers = []
) {
): Response {
if ($data instanceof PageInterface) {
list ($data, $meta, $links) = $this->extractPage($data, $meta, $links);
[$data, $meta, $links] = $this->extractPage($data, $meta, $links);
}

return parent::getIdentifiersResponse($data, $statusCode, $links, $meta, $headers);
8000 Expand All @@ -388,9 +398,9 @@ public function getIdentifiersResponse(
* @param Error|ErrorInterface|array $error
* @param int|null $defaultStatusCode
* @param array $headers
* @return mixed
* @return Response
*/
public function error($error, int $defaultStatusCode = null, array $headers = [])
public function error($error, int $defaultStatusCode = null, array $headers = []): Response
{
if (!$error instanceof ErrorInterface) {
$error = $this->factory->createError(
Expand All @@ -411,9 +421,10 @@ public function error($error, int $defaultStatusCode = null, array $headers = []
* @param iterable $errors
* @param int|null $defaultStatusCode
* @param array $headers
* @return mixed
*
* @return Response
*/
public function errors(iterable $errors, int $defaultStatusCode = null, array $headers = [])
public function errors(iterable $errors, int $defaultStatusCode = null, array $headers = []): Response
{
$errors = $this->factory->createErrors($errors);
$statusCode = Helpers::httpErrorStatus($errors, $defaultStatusCode);
Expand All @@ -426,9 +437,9 @@ public function errors(iterable $errors, int $defaultStatusCode = null, array $h
* @param array $links
* @param null $meta
* @param array $headers
* @return mixed
* @return Response
*/
protected function getResourceResponse($resource, array $links = [], $meta = null, array $headers = [])
protected function getResourceResponse($resource, array $links = [], $meta = null, array $headers = []): Response
{
if ($this->isNoContent($resource, $links, $meta)) {
return $this->noContent();
Expand Down Expand Up @@ -518,9 +529,15 @@ protected function getSupportedExtensions()
}

/**
* @inheritdoc
* Create HTTP response.
*
* @param string|null $content
* @param int $statusCode
* @param array $headers
*
* @return Response
*/
protected function createResponse($content, $statusCode, array $headers)
protected function createResponse($content, $statusCode, array $headers): Response
{
return response($content, $statusCode, $headers);
}
Expand Down
0