diff --git a/src/application/controllers/api/v1/Appointments.php b/src/application/controllers/api/v1/Appointments.php index e8ea86c1..a21d2aff 100644 --- a/src/application/controllers/api/v1/Appointments.php +++ b/src/application/controllers/api/v1/Appointments.php @@ -24,11 +24,11 @@ use \EA\Engine\Types\NonEmptyString; */ class Appointments extends API_V1_Controller { /** - * Appointments Resource Formatter + * Appointments Resource Parser * - * @var \EA\Engine\Api\V1\Formatters\Appointments + * @var \EA\Engine\Api\V1\Parsers\Appointments */ - protected $formatter; + protected $parser; /** * Class Constructor @@ -36,7 +36,7 @@ class Appointments extends API_V1_Controller { public function __construct() { parent::__construct(); $this->load->model('appointments_model'); - $this->formatter = new \EA\Engine\Api\V1\Formatters\Appointments; + $this->parser = new \EA\Engine\Api\V1\Parsers\Appointments; } /** @@ -46,12 +46,10 @@ class Appointments extends API_V1_Controller { */ public function get($id = null) { $condition = $id !== null ? 'id = ' . $id : null; - $appointments = $this->appointments_model->get_batch($condition); - - $response = new Response($appointments); - $response->format($this->formatter)->search()->sort()->paginate()->minimize(); + $response = new Response($appointments); + $response->encode($this->parser)->search()->sort()->paginate()->minimize(); if ($id !== null) { $response->singleEntry(); @@ -64,7 +62,13 @@ class Appointments extends API_V1_Controller { * POST API Method */ public function post() { - + $request = json_decode(file_get_contents('php://input'), true); + $this->parser->decode($request); + $id = $this->appointments_model->add($request); + $appointments = $this->appointments_model->get_batch('id = ' . $id); + $response = new Response($appointments); + $status = new NonEmptyString('201 Created'); + $response->encode($this->parser)->singleEntry()->output($status); } /** diff --git a/src/engine/Api/V1/Formatters/Appointments.php b/src/engine/Api/V1/Formatters/Appointments.php deleted file mode 100644 index 28469046..00000000 --- a/src/engine/Api/V1/Formatters/Appointments.php +++ /dev/null @@ -1,37 +0,0 @@ - - * @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/Parsers/Appointments.php b/src/engine/Api/V1/Parsers/Appointments.php new file mode 100644 index 00000000..000edb1e --- /dev/null +++ b/src/engine/Api/V1/Parsers/Appointments.php @@ -0,0 +1,81 @@ + + * @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\Parsers; + +class Appointments implements ParsersInterface { + public function encode(array &$response) { + $encodedResponse = [ + 'id' => $response['id'], + 'book' => $response['book_datetime'], + 'start' => $response['start_datetime'], + 'end' => $response['end_datetime'], + 'hash' => $response['hash'], + 'notes' => $response['notes'], + 'customerId' => $response['id_users_customer'], + 'providerId' => $response['id_users_provider'], + 'serviceId' => $response['id_services'], + 'googleCalendarId' => $response['id_google_calendar'] + ]; + + $response = $encodedResponse; + } + + public function decode(array &$request) { + $decodedRequest = []; + + if (!empty($request['id'])) { + $decodedRequest['id'] = $request['id']; + } + + if (!empty($request['book'])) { + $decodedRequest['book_datetime'] = $request['book']; + } + + if (!empty($request['start'])) { + $decodedRequest['start_datetime'] = $request['start']; + } + + if (!empty($request['end'])) { + $decodedRequest['end_datetime'] = $request['end']; + } + + if (!empty($request['hash'])) { + $decodedRequest['hash'] = $request['hash']; + } + + if (!empty($request['notes'])) { + $decodedRequest['notes'] = $request['notes']; + } + + if (!empty($request['customerId'])) { + $decodedRequest['id_users_customer'] = $request['customerId']; + } + + if (!empty($request['providerId'])) { + $decodedRequest['id_users_provider'] = $request['providerId']; + } + + if (!empty($request['serviceId'])) { + $decodedRequest['id_services'] = $request['serviceId']; + } + + if (!empty($request['googleCalendarId'])) { + $decodedRequest['id_google_calendar'] = $request['googleCalendarId']; + } + + $decodedRequest['is_unavailable'] = false; + + $request = $decodedRequest; + } +} diff --git a/src/engine/Api/V1/Formatters/FormattersInterface.php b/src/engine/Api/V1/Parsers/ParsersInterface.php similarity index 76% rename from src/engine/Api/V1/Formatters/FormattersInterface.php rename to src/engine/Api/V1/Parsers/ParsersInterface.php index 80d58e6b..147615ca 100644 --- a/src/engine/Api/V1/Formatters/FormattersInterface.php +++ b/src/engine/Api/V1/Parsers/ParsersInterface.php @@ -11,8 +11,9 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace EA\Engine\Api\V1\Formatters; +namespace EA\Engine\Api\V1\Parsers; -interface FormattersInterface { - public function format(array &$response); +interface ParsersInterface { + public function encode(array &$response); + public function decode(array &$request); } diff --git a/src/engine/Api/V1/Response.php b/src/engine/Api/V1/Response.php index 1583b124..974a4c75 100644 --- a/src/engine/Api/V1/Response.php +++ b/src/engine/Api/V1/Response.php @@ -13,6 +13,8 @@ namespace EA\Engine\Api\V1; +use EA\Engine\Types\NonEmptyString; + /** * API v1 Response * @@ -42,14 +44,16 @@ class Response { } /** - * Format the response entries to the API compatible structure. + * Encode the response entries to the API compatible structure. * - * @param \Formatters\FormattersInterface $formatter Provide the corresponding formatter class. + * @param \Parsers\ParsersInterface $parser Provide the corresponding parser class. * * @return \EA\Engine\Api\V1\Response */ - public function format(Formatters\FormattersInterface $formatter) { - $formatter->format($this->response); + public function encode(Parsers\ParsersInterface $parser) { + foreach ($this->response as &$entry) { + $parser->encode($entry); + } return $this; }