Skip to content
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

[5.4] Let JsonResponse also provide the original data. #17137

Merged
merged 2 commits into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/Illuminate/Http/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class JsonResponse extends BaseJsonResponse
*/
public function __construct($data = null, $status = 200, $headers = [], $options = 0)
{
$this->original = $data;

$this->encodingOptions = $options;

parent::__construct($data, $status, $headers);
Expand Down Expand Up @@ -55,6 +57,8 @@ public function getData($assoc = false, $depth = 512)
*/
public function setData($data = [])
{
$this->original = $data;

if ($data instanceof Arrayable) {
$this->data = json_encode($data->toArray(), $this->encodingOptions);
} elseif ($data instanceof Jsonable) {
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Http/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class RedirectResponse extends BaseRedirectResponse
*/
protected $session;

/**
* Get the original response content.
*
* @return null
*/
public function getOriginalContent()
{
return;
}

/**
* Flash a piece of data to the session.
*
Expand Down
17 changes: 0 additions & 17 deletions src/Illuminate/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ class Response extends BaseResponse
{
use ResponseTrait;

/**
* The original content of the response.
*
* @var mixed
*/
public $original;

/**
* Set the content on the response.
*
Expand Down Expand Up @@ -76,14 +69,4 @@ protected function morphToJson($content)

return json_encode($content);
}

/**
* Get the original response content.
*
* @return mixed
*/
public function getOriginalContent()
{
return $this->original;
}
}
17 changes: 17 additions & 0 deletions src/Illuminate/Http/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

trait ResponseTrait
{
/**
* The original content of the response.
*
* @var mixed
*/
public $original;

/**
* The exception that triggered the error response (if applicable).
*
Expand Down Expand Up @@ -34,6 +41,16 @@ public function content()
return $this->getContent();
}

/**
* Get the original response content.
*
* @return mixed
*/
public function getOriginalContent()
{
return $this->original;
}

/**
* Set a header on the Response.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Http/HttpJsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public function testSetAndRetrieveData()
$this->assertEquals('bar', $data->foo);
}

public function testGetOriginalContent()
{
$response = new Illuminate\Http\JsonResponse(new JsonResponseTestArrayableObject);
$this->assertInstanceOf(JsonResponseTestArrayableObject::class, $response->getOriginalContent());

$response = new Illuminate\Http\JsonResponse;
$response->setData(new JsonResponseTestArrayableObject);
$this->assertInstanceOf(JsonResponseTestArrayableObject::class, $response->getOriginalContent());
}

public function testSetAndRetrieveOptions()
{
$response = new Illuminate\Http\JsonResponse(['foo' => 'bar']);
Expand Down