From f46dc546bf073f99095d7a129aae23f6b03977db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20Portugu=C3=A9s=20Calder=C3=B3?= Date: Tue, 20 Oct 2015 17:37:37 +0200 Subject: [PATCH] Adding custom headers using the JsonApiResponseTrait is now possible --- src/Serializer/JsonApiResponseTrait.php | 29 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Serializer/JsonApiResponseTrait.php b/src/Serializer/JsonApiResponseTrait.php index 1e332d7..bb32c18 100644 --- a/src/Serializer/JsonApiResponseTrait.php +++ b/src/Serializer/JsonApiResponseTrait.php @@ -6,6 +6,15 @@ trait JsonApiResponseTrait { + /** + * @param \Psr\Http\Message\ResponseInterface $response + * @return \Psr\Http\Message\ResponseInterface + */ + protected function addHeaders(\Psr\Http\Message\ResponseInterface $response) + { + return $response; + } + /** * @param string $json * @@ -14,7 +23,7 @@ trait JsonApiResponseTrait private function errorResponse($json) { return (new HttpFoundationFactory()) - ->createResponse(new \NilPortugues\Api\JsonApi\Http\Message\ErrorResponse($json)); + ->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\ErrorResponse($json))); } /** @@ -25,7 +34,7 @@ private function errorResponse($json) private function resourceCreatedResponse($json) { return (new HttpFoundationFactory()) - ->createResponse(new \NilPortugues\Api\JsonApi\Http\Message\ResourceCreatedResponse($json)); + ->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\ResourceCreatedResponse($json))); } /** @@ -36,7 +45,7 @@ private function resourceCreatedResponse($json) private function resourceDeletedResponse($json) { return (new HttpFoundationFactory()) - ->createResponse(new \NilPortugues\Api\JsonApi\Http\Message\ResourceDeletedResponse($json)); + ->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\ResourceDeletedResponse($json))); } /** @@ -47,7 +56,7 @@ private function resourceDeletedResponse($json) private function resourceNotFoundResponse($json) { return (new HttpFoundationFactory()) - ->createResponse(new \NilPortugues\Api\JsonApi\Http\Message\ResourceNotFoundResponse($json)); + ->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\ResourceNotFoundResponse($json))); } /** @@ -58,7 +67,7 @@ private function resourceNotFoundResponse($json) private function resourcePatchErrorResponse($json) { return (new HttpFoundationFactory()) - ->createResponse(new \NilPortugues\Api\JsonApi\Http\Message\ResourcePatchErrorResponse($json)); + ->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\ResourcePatchErrorResponse($json))); } /** @@ -69,7 +78,7 @@ private function resourcePatchErrorResponse($json) private function resourcePostErrorResponse($json) { return (new HttpFoundationFactory()) - ->createResponse(new \NilPortugues\Api\JsonApi\Http\Message\ResourcePostErrorResponse($json)); + ->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\ResourcePostErrorResponse($json))); } /** @@ -80,7 +89,7 @@ private function resourcePostErrorResponse($json) private function resourceProcessingResponse($json) { return (new HttpFoundationFactory()) - ->createResponse(new \NilPortugues\Api\JsonApi\Http\Message\ResourceProcessingResponse($json)); + ->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\ResourceProcessingResponse($json))); } /** @@ -91,7 +100,7 @@ private function resourceProcessingResponse($json) private function resourceUpdatedResponse($json) { return (new HttpFoundationFactory()) - ->createResponse(new \NilPortugues\Api\JsonApi\Http\Message\ResourceUpdatedResponse($json)); + ->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\ResourceUpdatedResponse($json))); } /** @@ -102,7 +111,7 @@ private function resourceUpdatedResponse($json) private function response($json) { return (new HttpFoundationFactory()) - ->createResponse(new \NilPortugues\Api\JsonApi\Http\Message\Response($json)); + ->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\Response($json))); } /** @@ -113,6 +122,6 @@ private function response($json) private function unsupportedActionResponse($json) { return (new HttpFoundationFactory()) - ->createResponse(new \NilPortugues\Api\JsonApi\Http\Message\UnsupportedActionResponse($json)); + ->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\UnsupportedActionResponse($json))); } }