feature #9963 [HttpFoundation] JsonResponse::setEncodingOptions accepts also integer (stloyd)
This PR was merged into the 2.5-dev branch.
Discussion
----------
[HttpFoundation] JsonResponse::setEncodingOptions accepts also integer
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Tests pass? | yes
| License | MIT
Now you can also set encoding options like:
```php
$response->setEncodingOptions(JSON_UNESCAPED_UNICODE | $response->getEncodingOptions());
Commits
-------
f8bc3b2
[HttpFoundation] JsonResponse::setEncodingOptions accepts also integer
This commit is contained in:
commit
ef12af9948
@ -117,18 +117,13 @@ class JsonResponse extends Response
|
|||||||
/**
|
/**
|
||||||
* Sets options used while encoding data to JSON.
|
* Sets options used while encoding data to JSON.
|
||||||
*
|
*
|
||||||
* @param array $encodingOptions
|
* @param integer $encodingOptions
|
||||||
*
|
*
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function setEncodingOptions(array $encodingOptions)
|
public function setEncodingOptions($encodingOptions)
|
||||||
{
|
{
|
||||||
$this->encodingOptions = 0;
|
$this->encodingOptions = (integer) $encodingOptions;
|
||||||
foreach ($encodingOptions as $encodingOption) {
|
|
||||||
if (($this->encodingOptions & $encodingOption) != $encodingOption) {
|
|
||||||
$this->encodingOptions |= $encodingOption;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->setData(json_decode($this->data));
|
return $this->setData(json_decode($this->data));
|
||||||
}
|
}
|
||||||
|
@ -180,7 +180,7 @@ class JsonResponseTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals('[[1,2,3]]', $response->getContent());
|
$this->assertEquals('[[1,2,3]]', $response->getContent());
|
||||||
|
|
||||||
$response->setEncodingOptions(array(JSON_FORCE_OBJECT));
|
$response->setEncodingOptions(JSON_FORCE_OBJECT);
|
||||||
|
|
||||||
$this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
|
$this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user