Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4a14137

Browse files
committed
Add named constructor on JsonResponse
To make easier construction with raw json
1 parent 64ace10 commit 4a14137

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public static function create($data = null, $status = 200, $headers = array())
5858
return new static($data, $status, $headers);
5959
}
6060

61+
/**
62+
* Make easier the creation of JsonResponse from raw json
63+
*/
64+
public static function fromJsonString($data = null, $status = 200, $headers = array())
65+
{
66+
return new static($data, $status, $headers, true);
67+
}
68+
6169
/**
6270
* Sets the JSONP callback.
6371
*

src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ public function testSetEncodingOptions()
198198
$this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
199199
}
200200

201+
public function testItAcceptsJsonAsString()
202+
{
203+
$response = JsonResponse::fromJsonString('{"foo":"bar"}');
204+
$this->assertSame('{"foo":"bar"}', $response->getContent());
205+
}
206+
201207
/**
202208
* @expectedException \InvalidArgumentException
203209
*/

0 commit comments

Comments
 (0)