Skip to content

Commit

Permalink
Add new property, and functions, to have a default error return for a…
Browse files Browse the repository at this point in the history
… response
  • Loading branch information
rmrhz committed Jul 19, 2016
1 parent 1813f94 commit 903568c
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/Prjkt/Component/Reqfuck/Reqfuck.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ abstract class Reqfuck extends FormRequest
*/
protected $rules = [];

/**
* Response data
*
* @var array
*/
protected $responseData = [ // We are being pessimestic here
'code' => 500,
'message' => 'Something bad went wrong'
];

/**
* Get the parsed method
*
Expand Down Expand Up @@ -89,16 +99,39 @@ public function getCommand(string $command) : string
return $command;
}

/**
* Set response data
*
* @param array $data
* @return $this
*/
public function setResponseData(array $data) : \Prjkt\Component\Reqfuck\Reqfuck
{
$this->responseData = $data;

return $this;
}

/**
* Get response data
*
* @return array
*/
public function getResponseData() : array
{
return $this->responseData;
}

/**
* Default response
*
* --
* @param array $errors
* @return \Illuminate\Http\JsonResponse
*/
public function response(array $errors) : \Illuminate\Http\JsonResponse
public function response(array $errors = []) : \Illuminate\Http\JsonResponse
{
$errors = ['code' => 422, 'data' => $errors];
$errors = ! count($errors) ? $this->getResponseData() : $errors;

return response()->json($errors);
}
Expand Down

0 comments on commit 903568c

Please sign in to comment.