Skip to content

Commit

Permalink
feature #19552 [HttpFoundation] Add named constructor on JsonResponse…
Browse files Browse the repository at this point in the history
… (tyx)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[HttpFoundation] Add named constructor on JsonResponse

| Q             | A
| ------------- | ---
| Branch?       | "master"
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

To make easier construction with raw json. It is a simple addition but having something like :
```php
return new JsonResponse($content, Response::HTTP_OK, [], true);
```

and
```php
return new JsonResponse($content, Response::HTTP_OK, []);
```

is not very obvious to get the difference without going to read the JsonResponse constructor.

Commits
-------

6d5a65d Add named constructor on JsonResponse
  • Loading branch information
fabpot committed Aug 16, 2016
2 parents f56ba63 + 6d5a65d commit 5ace4fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Expand Up @@ -58,6 +58,14 @@ public static function create($data = null, $status = 200, $headers = array())
return new static($data, $status, $headers);
}

/**
* Make easier the creation of JsonResponse from raw json.
*/
public static function fromJsonString($data = null, $status = 200, $headers = array())
{
return new static($data, $status, $headers, true);
}

/**
* Sets the JSONP callback.
*
Expand Down
Expand Up @@ -198,6 +198,12 @@ public function testSetEncodingOptions()
$this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
}

public function testItAcceptsJsonAsString()
{
$response = JsonResponse::fromJsonString('{"foo":"bar"}');
$this->assertSame('{"foo":"bar"}', $response->getContent());
}

/**
* @expectedException \InvalidArgumentException
*/
Expand Down

0 comments on commit 5ace4fd

Please sign in to comment.