Skip to content

Commit

Permalink
Adding custom headers using the JsonApiResponseTrait is now possible
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Oct 20, 2015
1 parent 501cf62 commit f46dc54
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Serializer/JsonApiResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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)));
}

/**
Expand All @@ -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)));
}

/**
Expand All @@ -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)));
}

/**
Expand All @@ -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)));
}

/**
Expand All @@ -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)));
}

/**
Expand All @@ -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)));
}

/**
Expand All @@ -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)));
}

/**
Expand All @@ -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)));
}

/**
Expand All @@ -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)));
}

/**
Expand All @@ -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)));
}
}

0 comments on commit f46dc54

Please sign in to comment.