Changed the 'singleEntry' response method in order to avoid writing the ID condition in every API controller.

This commit is contained in:
Alex Tselegidis 2016-07-10 11:38:22 +02:00
parent ff558f99c4
commit eed53d44cd
2 changed files with 17 additions and 10 deletions

View file

@ -56,13 +56,14 @@ class Appointments extends API_V1_Controller {
} }
$response = new Response($appointments); $response = new Response($appointments);
$response->encode($this->parser)->search()->sort()->paginate()->minimize(); $response->encode($this->parser)
->search()
->sort()
->paginate()
->minimize()
->singleEntry($id)
->output();
if ($id !== null) {
$response->singleEntry();
}
$response->output();
} catch(\Exception $exception) { } catch(\Exception $exception) {
exit($this->_handleException($exception)); exit($this->_handleException($exception));
} }

View file

@ -105,12 +105,18 @@ class Response {
/** /**
* Return a single entry instead of an array of entries. * Return a single entry instead of an array of entries.
* *
* This is useful whenever the client requests only a single entry. * This is useful whenever the client requests only a single entry. Make sure that you call this method
* right before the output() in order to avoid side-effects with the other processor methods of this class.
*
* @param int $id Provide the ID value of the request UI and if is not null the response will be
* converted into an associative array.
* *
* @return \EA\Engine\Api\V1\Response * @return \EA\Engine\Api\V1\Response
*/ */
public function singleEntry() { public function singleEntry($id) {
if ($id !== null) {
$this->response = array_shift($this->response); $this->response = array_shift($this->response);
}
return $this; return $this;
} }