Added No Content response to API\ResponseTrait

This commit is contained in:
Sandstrom 2019-08-12 18:54:18 +02:00
parent 290c9f6905
commit e20830e1bc
No known key found for this signature in database
GPG Key ID: 5DFDEE8BD4192B67
3 changed files with 40 additions and 0 deletions

View File

@ -65,6 +65,7 @@ trait ResponseTrait
protected $codes = [
'created' => 201,
'deleted' => 200,
'no_content' => 204,
'invalid_request' => 400,
'unsupported_response_type' => 400,
'invalid_scope' => 400,
@ -190,6 +191,21 @@ trait ResponseTrait
//--------------------------------------------------------------------
/**
* Used after a command has been successfully executed but there is no
* meaningful reply to send back to the client.
*
* @param string $message Message.
*
* @return mixed
*/
public function respondNoContent(string $message = 'No Content')
{
return $this->respond(null, $this->codes['no_content'], $message);
}
//--------------------------------------------------------------------
/**
* Used when the client is either didn't send authorization information,
* or had bad authorization credentials. User is encouraged to try again

View File

@ -278,6 +278,15 @@ EOH;
$this->assertEquals($this->formatter->format($expected), $this->response->getBody());
}
public function testNoContent()
{
$controller = $this->makeController();
$controller->respondNoContent('');
$this->assertEquals('No Content', $this->response->getReason());
$this->assertEquals(204, $this->response->getStatusCode());
}
public function testNotFound()
{
$controller = $this->makeController();

View File

@ -48,6 +48,8 @@ exist for the most common use cases::
respondCreated($data);
// Item successfully deleted
respondDeleted($data);
// Command executed by no response required
respondNoContent($message);
// Client isn't authorized
failUnauthorized($description);
// Forbidden action
@ -179,6 +181,19 @@ Class Reference
$user = $userModel->delete($id);
return $this->respondDeleted(['id' => $id]);
.. php:method:: respondNoContent(string $message = 'No Content')
:param string $message: A custom "reason" message to return.
:returns: The value of the Response object's send() method.
Sets the appropriate status code to use when a command was successfully executed by the server but there is no
meaningful reply to send back to the client, typically 204.
::
sleep(1);
return $this->respondNoContent();
.. php:method:: failUnauthorized(string $description = 'Unauthorized'[, string $code=null[, string $message = '']])
:param mixed $description: The error message to show the user.