JsonResponse: Allow to output the "data" key in case of an error

As stated by the JSend specification this key is optional for error responses.
This commit is contained in:
Johannes Meyer 2016-09-21 15:58:59 +02:00
parent c547f4c17f
commit 5b875f3749
1 changed files with 5 additions and 3 deletions

View File

@ -121,7 +121,7 @@ class JsonResponse extends Response
*/ */
public function getFailData() public function getFailData()
{ {
return $this->failData; return (! is_array($this->failData) || empty($this->failData)) ? null : $this->failData;
} }
/** /**
@ -173,9 +173,11 @@ class JsonResponse extends Response
switch ($this->status) { switch ($this->status) {
case static::STATUS_ERROR: case static::STATUS_ERROR:
$body['message'] = $this->getErrorMessage(); $body['message'] = $this->getErrorMessage();
break;
case static::STATUS_FAIL: case static::STATUS_FAIL:
$body['data'] = $this->getFailData(); $failData = $this->getFailData();
if ($failData !== null || $this->status === static::STATUS_FAIL) {
$body['data'] = $failData;
}
break; break;
case static::STATUS_SUCCESS: case static::STATUS_SUCCESS:
$body['data'] = $this->getSuccessData(); $body['data'] = $this->getSuccessData();