From eed53d44cd63c38c5cc40e28449f744d0fdff831 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sun, 10 Jul 2016 11:38:22 +0200 Subject: [PATCH] Changed the 'singleEntry' response method in order to avoid writing the ID condition in every API controller. --- .../controllers/api/v1/Appointments.php | 13 +++++++------ src/engine/Api/V1/Response.php | 14 ++++++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/application/controllers/api/v1/Appointments.php b/src/application/controllers/api/v1/Appointments.php index c19e4d1a..b30fddd0 100644 --- a/src/application/controllers/api/v1/Appointments.php +++ b/src/application/controllers/api/v1/Appointments.php @@ -56,13 +56,14 @@ class Appointments extends API_V1_Controller { } $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) { exit($this->_handleException($exception)); } diff --git a/src/engine/Api/V1/Response.php b/src/engine/Api/V1/Response.php index 974a4c75..73e848af 100644 --- a/src/engine/Api/V1/Response.php +++ b/src/engine/Api/V1/Response.php @@ -105,13 +105,19 @@ class Response { /** * 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 */ - public function singleEntry() { - $this->response = array_shift($this->response); - + public function singleEntry($id) { + if ($id !== null) { + $this->response = array_shift($this->response); + } + return $this; }