Skip to content

Commit

Permalink
Adding custom headers using the JsonResponseTrait is now possible
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Oct 20, 2015
1 parent 485e351 commit 68a1be9
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/NilPortugues/Laravel5/JsonSerializer/JsonResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@

trait JsonResponseTrait
{
/**
* @param \Psr\Http\Message\ResponseInterface $response
* @return \Psr\Http\Message\ResponseInterface
*/
protected function addHeaders(\Psr\Http\Message\ResponseInterface $response)
{
return $response;
}

/**
* @param string $json
*
* @return \Symfony\Component\HttpFoundation\Response
*/
private function errorResponse($json) {
return (new HttpFoundationFactory())
->createResponse(new \NilPortugues\Api\Json\Http\Message\ErrorResponse($json));
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ErrorResponse($json)));
}

/**
Expand All @@ -32,7 +41,7 @@ private function errorResponse($json) {
private function resourceCreatedResponse($json)
{
return (new HttpFoundationFactory())
->createResponse(new \NilPortugues\Api\Json\Http\Message\ResourceCreatedResponse($json));
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourceCreatedResponse($json)));
}

/**
Expand All @@ -43,7 +52,7 @@ private function resourceCreatedResponse($json)
private function resourceDeletedResponse($json)
{
return (new HttpFoundationFactory())
->createResponse(new \NilPortugues\Api\Json\Http\Message\ResourceDeletedResponse($json));
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourceDeletedResponse($json)));
}

/**
Expand All @@ -54,7 +63,7 @@ private function resourceDeletedResponse($json)
private function resourceNotFoundResponse($json)
{
return (new HttpFoundationFactory())
->createResponse(new \NilPortugues\Api\Json\Http\Message\ResourceNotFoundResponse($json));
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourceNotFoundResponse($json)));
}

/**
Expand All @@ -65,7 +74,7 @@ private function resourceNotFoundResponse($json)
private function resourcePatchErrorResponse($json)
{
return (new HttpFoundationFactory())
->createResponse(new \NilPortugues\Api\Json\Http\Message\ResourcePatchErrorResponse($json));
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourcePatchErrorResponse($json)));
}

/**
Expand All @@ -76,7 +85,7 @@ private function resourcePatchErrorResponse($json)
private function resourcePostErrorResponse($json)
{
return (new HttpFoundationFactory())
->createResponse(new \NilPortugues\Api\Json\Http\Message\ResourcePostErrorResponse($json));
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourcePostErrorResponse($json)));
}

/**
Expand All @@ -87,7 +96,7 @@ private function resourcePostErrorResponse($json)
private function resourceProcessingResponse($json)
{
return (new HttpFoundationFactory())
->createResponse(new \NilPortugues\Api\Json\Http\Message\ResourceProcessingResponse($json));
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourceProcessingResponse($json)));
}

/**
Expand All @@ -98,7 +107,7 @@ private function resourceProcessingResponse($json)
private function resourceUpdatedResponse($json)
{
return (new HttpFoundationFactory())
->createResponse(new \NilPortugues\Api\Json\Http\Message\ResourceUpdatedResponse($json));
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourceUpdatedResponse($json)));
}

/**
Expand All @@ -109,7 +118,7 @@ private function resourceUpdatedResponse($json)
private function response($json)
{
return (new HttpFoundationFactory())
->createResponse(new \NilPortugues\Api\Json\Http\Message\Response($json));
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\Response($json)));
}

/**
Expand All @@ -120,7 +129,7 @@ private function response($json)
private function unsupportedActionResponse($json)
{
return (new HttpFoundationFactory())
->createResponse(new \NilPortugues\Api\Json\Http\Message\UnsupportedActionResponse($json));
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\UnsupportedActionResponse($json)));
}
}

0 comments on commit 68a1be9

Please sign in to comment.