From ae4a20cc905f63c27864533175a77f7e6a89ac55 Mon Sep 17 00:00:00 2001 From: Zlatoslav Desyatnikov Date: Wed, 29 Jul 2020 17:32:25 +0300 Subject: [PATCH 1/2] Added correct return type hints for Http\Responses --- src/Http/Responses/Responses.php | 60 +++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/src/Http/Responses/Responses.php b/src/Http/Responses/Responses.php index 66420f01..3f8fdfe3 100644 --- a/src/Http/Responses/Responses.php +++ b/src/Http/Responses/Responses.php @@ -28,7 +28,10 @@ use CloudCreativity\LaravelJsonApi\Encoder\Neomerx\Factory; use CloudCreativity\LaravelJsonApi\Routing\Route; use CloudCreativity\LaravelJsonApi\Utils\Helpers; +use Illuminate\Contracts\Routing\ResponseFactory; +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; @@ -115,7 +118,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()}." ); } @@ -135,7 +138,7 @@ 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, @@ -174,7 +177,7 @@ public function withEncodingParameters(?EncodingParametersInterface $parameters) /** * @param $statusCode * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function statusCode($statusCode, array $headers = []) { @@ -183,7 +186,7 @@ public function statusCode($statusCode, array $headers = []) /** * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function noContent(array $headers = []) { @@ -194,7 +197,7 @@ public function noContent(array $headers = []) * @param $meta * @param int $statusCode * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function meta($meta, $statusCode = self::HTTP_OK, array $headers = []) { @@ -206,7 +209,7 @@ public function meta($meta, $statusCode = self::HTTP_OK, array $headers = []) * @param $meta * @param int $statusCode * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function noData(array $links = [], $meta = null, $statusCode = self::HTTP_OK, array $headers = []) { @@ -222,7 +225,7 @@ public function noData(array $links = [], $meta = null, $statusCode = self::HTTP * @param mixed $meta * @param int $statusCode * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function content( $data, @@ -234,8 +237,16 @@ public function content( 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|ResponseFactory */ public function getContentResponse( $data, @@ -245,7 +256,7 @@ public function getContentResponse( array $headers = [] ) { 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); @@ -256,7 +267,7 @@ public function getContentResponse( * @param array $links * @param mixed $meta * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function created($resource = null, array $links = [], $meta = null, array $headers = []) { @@ -282,7 +293,7 @@ public function created($resource = null, array $links = [], $meta = null, array * @param array $links * @param mixed $meta * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function updated( $resource = null, @@ -300,7 +311,7 @@ public function updated( * @param array $links * @param mixed|null $meta * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function deleted( $resource = null, @@ -316,7 +327,7 @@ public function deleted( * @param array $links * @param null $meta * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function accepted(AsynchronousProcess $job, array $links = [], $meta = null, array $headers = []) { @@ -330,7 +341,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 = []) { @@ -348,7 +359,7 @@ public function process(AsynchronousProcess $job, array $links = [], $meta = nul * @param mixed $meta * @param int $statusCode * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function relationship( $data, @@ -366,7 +377,7 @@ public function relationship( * @param $links * @param $meta * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function getIdentifiersResponse( $data, @@ -376,7 +387,7 @@ public function getIdentifiersResponse( array $headers = [] ) { 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); @@ -388,7 +399,7 @@ public function getIdentifiersResponse( * @param Error|ErrorInterface|array $error * @param int|null $defaultStatusCode * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ public function error($error, int $defaultStatusCode = null, array $headers = []) { @@ -411,7 +422,8 @@ public function error($error, int $defaultStatusCode = null, array $headers = [] * @param iterable $errors * @param int|null $defaultStatusCode * @param array $headers - * @return mixed + * + * @return Response|ResponseFactory */ public function errors(iterable $errors, int $defaultStatusCode = null, array $headers = []) { @@ -426,7 +438,7 @@ public function errors(iterable $errors, int $defaultStatusCode = null, array $h * @param array $links * @param null $meta * @param array $headers - * @return mixed + * @return Response|ResponseFactory */ protected function getResourceResponse($resource, array $links = [], $meta = null, array $headers = []) { @@ -518,7 +530,13 @@ protected function getSupportedExtensions() } /** - * @inheritdoc + * Create HTTP response. + * + * @param string|null $content + * @param int $statusCode + * @param array $headers + * + * @return Response|ResponseFactory */ protected function createResponse($content, $statusCode, array $headers) { From b8b22f17ced6a2d7b0c3dd691541fa596bdd91a2 Mon Sep 17 00:00:00 2001 From: Zlatoslav Desyatnikov Date: Thu, 13 Aug 2020 12:09:27 +0300 Subject: [PATCH 2/2] Removed ResponseFactory --- src/Http/Responses/Responses.php | 67 ++++++++++++++++---------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/Http/Responses/Responses.php b/src/Http/Responses/Responses.php index 3f8fdfe3..3c942ed2 100644 --- a/src/Http/Responses/Responses.php +++ b/src/Http/Responses/Responses.php @@ -28,7 +28,6 @@ use CloudCreativity\LaravelJsonApi\Encoder\Neomerx\Factory; use CloudCreativity\LaravelJsonApi\Routing\Route; use CloudCreativity\LaravelJsonApi\Utils\Helpers; -use Illuminate\Contracts\Routing\ResponseFactory; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Response; use InvalidArgumentException; @@ -144,7 +143,7 @@ public function withEncoding( int $options = 0, int $depth = 512, string $mediaType = MediaTypeInterface::JSON_API_MEDIA_TYPE - ) { + ): self { $encoding = Encoding::create( $mediaType, $options, @@ -177,18 +176,18 @@ public function withEncodingParameters(?EncodingParametersInterface $parameters) /** * @param $statusCode * @param array $headers - * @return Response|ResponseFactory + * @return Response */ - public function statusCode($statusCode, array $headers = []) + public function statusCode($statusCode, array $headers = []): Response { return $this->getCodeResponse($statusCode, $headers); } /** * @param array $headers - * @return Response|ResponseFactory + * @return Response */ - public function noContent(array $headers = []) + public function noContent(array $headers = []): Response { return $this->getCodeResponse(204, $headers); } @@ -197,9 +196,9 @@ public function noContent(array $headers = []) * @param $meta * @param int $statusCode * @param array $headers - * @return Response|ResponseFactory + * @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); } @@ -209,9 +208,9 @@ public function meta($meta, $statusCode = self::HTTP_OK, array $headers = []) * @param $meta * @param int $statusCode * @param array $headers - * @return Response|ResponseFactory + * @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 ?: []); @@ -225,7 +224,7 @@ public function noData(array $links = [], $meta = null, $statusCode = self::HTTP * @param mixed $meta * @param int $statusCode * @param array $headers - * @return Response|ResponseFactory + * @return Response */ public function content( $data, @@ -233,7 +232,7 @@ public function content( $meta = null, $statusCode = self::HTTP_OK, array $headers = [] - ) { + ): Response { return $this->getContentResponse($data, $statusCode, $links, $meta, $headers); } @@ -246,7 +245,7 @@ public function content( * @param null $links * @param null $meta * @param array $headers - * @return Response|ResponseFactory + * @return Response */ public function getContentResponse( $data, @@ -254,7 +253,7 @@ public function getContentResponse( $links = null, $meta = null, array $headers = [] - ) { + ): Response { if ($data instanceof PageInterface) { [$data, $meta, $links] = $this->extractPage($data, $meta, $links); } @@ -267,9 +266,9 @@ public function getContentResponse( * @param array $links * @param mixed $meta * @param array $headers - * @return Response|ResponseFactory + * @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(); @@ -293,14 +292,14 @@ public function created($resource = null, array $links = [], $meta = null, array * @param array $links * @param mixed $meta * @param array $headers - * @return Response|ResponseFactory + * @return Response */ public function updated( $resource = null, array $links = [], $meta = null, array $headers = [] - ) { + ): Response { return $this->getResourceResponse($resource, $links, $meta, $headers); } @@ -311,14 +310,14 @@ public function updated( * @param array $links * @param mixed|null $meta * @param array $headers - * @return Response|ResponseFactory + * @return Response */ public function deleted( $resource = null, array $links = [], $meta = null, array $headers = [] - ) { + ): Response { return $this->getResourceResponse($resource, $links, $meta, $headers); } @@ -327,9 +326,9 @@ public function deleted( * @param array $links * @param null $meta * @param array $headers - * @return Response|ResponseFactory + * @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); @@ -359,7 +358,7 @@ public function process(AsynchronousProcess $job, array $links = [], $meta = nul * @param mixed $meta * @param int $statusCode * @param array $headers - * @return Response|ResponseFactory + * @return Response */ public function relationship( $data, @@ -367,7 +366,7 @@ public function relationship( $meta = null, $statusCode = 200, array $headers = [] - ) { + ): Response { return $this->getIdentifiersResponse($data, $statusCode, $links, $meta, $headers); } @@ -377,7 +376,7 @@ public function relationship( * @param $links * @param $meta * @param array $headers - * @return Response|ResponseFactory + * @return Response */ public function getIdentifiersResponse( $data, @@ -385,7 +384,7 @@ public function getIdentifiersResponse( $links = null, $meta = null, array $headers = [] - ) { + ): Response { if ($data instanceof PageInterface) { [$data, $meta, $links] = $this->extractPage($data, $meta, $links); } @@ -399,9 +398,9 @@ public function getIdentifiersResponse( * @param Error|ErrorInterface|array $error * @param int|null $defaultStatusCode * @param array $headers - * @return Response|ResponseFactory + * @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( @@ -423,9 +422,9 @@ public function error($error, int $defaultStatusCode = null, array $headers = [] * @param int|null $defaultStatusCode * @param array $headers * - * @return Response|ResponseFactory + * @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); @@ -438,9 +437,9 @@ public function errors(iterable $errors, int $defaultStatusCode = null, array $h * @param array $links * @param null $meta * @param array $headers - * @return Response|ResponseFactory + * @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(); @@ -536,9 +535,9 @@ protected function getSupportedExtensions() * @param int $statusCode * @param array $headers * - * @return Response|ResponseFactory + * @return Response */ - protected function createResponse($content, $statusCode, array $headers) + protected function createResponse($content, $statusCode, array $headers): Response { return response($content, $statusCode, $headers); }