From 523f4a3e76a1e2d33049661de701777c75f671be Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 9 Jul 2016 13:14:08 +0200 Subject: [PATCH] Moved the response operations into their own namespace 'Processors' and bundled up the appointments GET request. --- .../controllers/api/v1/Appointments.php | 28 +++-- src/engine/Api/V1/Formatters/Appointments.php | 37 +++++++ .../FormattersInterface.php} | 8 +- src/engine/Api/V1/Processors/Filter.php | 21 ++++ .../Minimize.php} | 8 +- src/engine/Api/V1/Processors/Paginate.php | 20 ++++ .../ProcessorsInterface.php} | 8 +- src/engine/Api/V1/{ => Processors}/Search.php | 8 +- src/engine/Api/V1/{ => Processors}/Sort.php | 8 +- src/engine/Api/V1/Response.php | 104 +++++++++++++++++- .../engine/Api/V1/fixtures/appointment.json | 2 +- 11 files changed, 223 insertions(+), 29 deletions(-) create mode 100644 src/engine/Api/V1/Formatters/Appointments.php rename src/engine/Api/V1/{Filter.php => Formatters/FormattersInterface.php} (80%) create mode 100644 src/engine/Api/V1/Processors/Filter.php rename src/engine/Api/V1/{Pagination.php => Processors/Minimize.php} (76%) create mode 100644 src/engine/Api/V1/Processors/Paginate.php rename src/engine/Api/V1/{Minimize.php => Processors/ProcessorsInterface.php} (79%) rename src/engine/Api/V1/{ => Processors}/Search.php (76%) rename src/engine/Api/V1/{ => Processors}/Sort.php (76%) diff --git a/src/application/controllers/api/v1/Appointments.php b/src/application/controllers/api/v1/Appointments.php index 2d8b99a2..a3de0a93 100644 --- a/src/application/controllers/api/v1/Appointments.php +++ b/src/application/controllers/api/v1/Appointments.php @@ -13,6 +13,9 @@ require_once __DIR__ . '/API_V1_Controller.php'; +use \EA\Engine\Api\V1\Response; +use \EA\Engine\Types\NonEmptyString; + /** * Appointments Controller * @@ -20,28 +23,39 @@ require_once __DIR__ . '/API_V1_Controller.php'; * @subpackage API */ class Appointments extends API_V1_Controller { + /** + * Appointments Resource Formatter + * + * @var \EA\Engine\Api\V1\Formatters\Appointments + */ + protected $formatter; + /** * Class Constructor */ public function __construct() { parent::__construct(); + $this->load->model('appointments_model'); + $this->formatter = new \EA\Engine\Api\V1\Formatters\Appointments; } /** * GET API Method * * @param int $id Optional (null), the record ID to be returned. - * - * @return \EA\Engine\Api\V1\Response Returns data response. */ public function get($id = null) { - + $appointments = $this->appointments_model->get_batch(); + + $response = new Response($appointments); + + $status = new NonEmptyString('200 OK'); + + $response->format($this->formatter)->search()->sort()->paginate()->minimize()->output(); } /** * POST API Method - * - * @return @return \EA\Engine\Api\V1\Response Returns data response. */ public function post() { @@ -51,8 +65,6 @@ class Appointments extends API_V1_Controller { * PUT API Method * * @param int $id The record ID to be updated. - * - * @return @return \EA\Engine\Api\V1\Response Returns data response. */ public function put($id) { @@ -62,8 +74,6 @@ class Appointments extends API_V1_Controller { * DELETE API Method * * @param int $id The record ID to be deleted. - * - * @return @return \EA\Engine\Api\V1\Response Returns data response. */ public function delete($id) { diff --git a/src/engine/Api/V1/Formatters/Appointments.php b/src/engine/Api/V1/Formatters/Appointments.php new file mode 100644 index 00000000..28469046 --- /dev/null +++ b/src/engine/Api/V1/Formatters/Appointments.php @@ -0,0 +1,37 @@ + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.2.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Api\V1\Formatters; + +class Appointments implements FormattersInterface { + public function format(array &$response) { + $temporaryResponse = []; + + foreach ($response as $entry) { + $temporaryResponse[] = [ + 'id' => $entry['id'], + 'book' => $entry['book_datetime'], + 'start' => $entry['start_datetime'], + 'end' => $entry['end_datetime'], + 'hash' => $entry['hash'], + 'notes' => $entry['notes'], + 'customerId' => $entry['id_users_customer'], + 'providerId' => $entry['id_users_provider'], + 'serviceId' => $entry['id_services'], + 'googleCalendarId' => $entry['id_google_calendar'] + ]; + } + + $response = $temporaryResponse; + } +} diff --git a/src/engine/Api/V1/Filter.php b/src/engine/Api/V1/Formatters/FormattersInterface.php similarity index 80% rename from src/engine/Api/V1/Filter.php rename to src/engine/Api/V1/Formatters/FormattersInterface.php index d2611b7d..80d58e6b 100644 --- a/src/engine/Api/V1/Filter.php +++ b/src/engine/Api/V1/Formatters/FormattersInterface.php @@ -11,8 +11,8 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Api\V1; +namespace EA\Engine\Api\V1\Formatters; -class Response { - -} +interface FormattersInterface { + public function format(array &$response); +} diff --git a/src/engine/Api/V1/Processors/Filter.php b/src/engine/Api/V1/Processors/Filter.php new file mode 100644 index 00000000..d1e40aac --- /dev/null +++ b/src/engine/Api/V1/Processors/Filter.php @@ -0,0 +1,21 @@ + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.2.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Api\V1\Processors; + +class Filter implements ProcessorsInterface { + { + public static function process(array $response) { + + } +} diff --git a/src/engine/Api/V1/Pagination.php b/src/engine/Api/V1/Processors/Minimize.php similarity index 76% rename from src/engine/Api/V1/Pagination.php rename to src/engine/Api/V1/Processors/Minimize.php index 3f1e0a3a..71d931bb 100644 --- a/src/engine/Api/V1/Pagination.php +++ b/src/engine/Api/V1/Processors/Minimize.php @@ -11,8 +11,10 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Api\V1; +namespace EA\Engine\Api\V1\Processors; -class Pagination { - +class Minimize implements ProcessorsInterface { + public static function process(array $response) { + + } } diff --git a/src/engine/Api/V1/Processors/Paginate.php b/src/engine/Api/V1/Processors/Paginate.php new file mode 100644 index 00000000..56e89992 --- /dev/null +++ b/src/engine/Api/V1/Processors/Paginate.php @@ -0,0 +1,20 @@ + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.2.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Api\V1\Processors; + +class Paginate implements ProcessorsInterface { + public static function process(array $response) { + + } +} diff --git a/src/engine/Api/V1/Minimize.php b/src/engine/Api/V1/Processors/ProcessorsInterface.php similarity index 79% rename from src/engine/Api/V1/Minimize.php rename to src/engine/Api/V1/Processors/ProcessorsInterface.php index d2611b7d..898ccee8 100644 --- a/src/engine/Api/V1/Minimize.php +++ b/src/engine/Api/V1/Processors/ProcessorsInterface.php @@ -11,8 +11,8 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Api\V1; +namespace EA\Engine\Api\V1\Processors; -class Response { - -} +interface ProcessorsInterface { + public static function process(array $response); +} diff --git a/src/engine/Api/V1/Search.php b/src/engine/Api/V1/Processors/Search.php similarity index 76% rename from src/engine/Api/V1/Search.php rename to src/engine/Api/V1/Processors/Search.php index d2611b7d..c9cd25c7 100644 --- a/src/engine/Api/V1/Search.php +++ b/src/engine/Api/V1/Processors/Search.php @@ -11,8 +11,10 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Api\V1; +namespace EA\Engine\Api\V1\Processors; -class Response { - +class Search implements ProcessorsInterface { + public static function process(array $response) { + + } } diff --git a/src/engine/Api/V1/Sort.php b/src/engine/Api/V1/Processors/Sort.php similarity index 76% rename from src/engine/Api/V1/Sort.php rename to src/engine/Api/V1/Processors/Sort.php index d2611b7d..fde57f12 100644 --- a/src/engine/Api/V1/Sort.php +++ b/src/engine/Api/V1/Processors/Sort.php @@ -11,8 +11,10 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Api\V1; +namespace EA\Engine\Api\V1\Processors; -class Response { - +class Sort implements ProcessorsInterface { + public static function process(array $response) { + + } } diff --git a/src/engine/Api/V1/Response.php b/src/engine/Api/V1/Response.php index d2611b7d..5dee0d7e 100644 --- a/src/engine/Api/V1/Response.php +++ b/src/engine/Api/V1/Response.php @@ -11,8 +11,108 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Api\V1; +namespace EA\Engine\Api\V1; +/** + * API v1 Response + * + * Use chain-calls of the class methods to easily manipulate the provided response array. This class will + * use directly the provided GET parameters for easier manipulation. + * + * Example: + * $formatter = new \EA\Engine\Api\V1\Formatters\Appointments; + * $response = new \EA\Engine\Api\V1\Response($data); + * $response->format($formatter)->search()->sort()->paginate()->minimize()->output(); + */ class Response { - + /** + * Contains the response information. + * + * @var array + */ + protected $response; + + /** + * Class Constructor + * + * @param array $response Provide unfiltered data to be processed as an array. + */ + public function __construct(array $response) { + $this->response = $response; + } + + /** + * Format the response entries to the API compatible structure. + * + * @param \Formatters\FormattersInterface $formatter Provide the corresponding formatter class. + * + * @return \EA\Engine\Api\V1\Response + */ + public function format(Formatters\FormattersInterface $formatter) { + $formatter->format($this->response); + + return $this; + } + + /** + * Perform a response search. + * + * @return \EA\Engine\Api\V1\Response + */ + public function search() { + Processors\Search::process($this->response); + + return $this; + } + + /** + * Perform a response sort. + * + * @return \EA\Engine\Api\V1\Response + */ + public function sort() { + Processors\Sort::process($this->response); + + return $this; + } + + /** + * Perform a response paginate. + * + * @return \EA\Engine\Api\V1\Response + */ + public function paginate() { + Processors\Paginate::process($this->response); + + return $this; + } + + /** + * Perform a response minimize. + * + * @return \EA\Engine\Api\V1\Response + */ + public function minimize() { + Processors\Minimize::process($this->response); + + return $this; + } + + /** + * Output the response as a JSON with the provided status header. + * + * @param \EA\Engine\Types\NonEmptyString $status Optional (null), if provided it must contain the status + * header value. Default string: '200 OK'. + * + * @return \EA\Engine\Api\V1\Response + */ + public function output(NonEmptyString $status = null) { + $header = $status ? $status->get() : '200 OK'; + + header('HTTP/1.0 ' . $header); + + echo json_encode($this->response, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT); + + return $this; + } } diff --git a/test/php/engine/Api/V1/fixtures/appointment.json b/test/php/engine/Api/V1/fixtures/appointment.json index bb220cba..c8855cc4 100644 --- a/test/php/engine/Api/V1/fixtures/appointment.json +++ b/test/php/engine/Api/V1/fixtures/appointment.json @@ -1,9 +1,9 @@ { "id": 1, - "hash": "asdf809a8sdf987a9d8f7", "book": "2016-07-08 12:57:00", "start": "2016-07-08 18:00:00", "end": "2016-07-08 18:30:00", + "hash": "asdf809a8sdf987a9d8f7", "notes": "These are some test notes.", "customerId": 56, "providerId": 4,