Moved the response operations into their own namespace 'Processors' and bundled up the appointments GET request.

This commit is contained in:
Alex Tselegidis 2016-07-09 13:14:08 +02:00
parent 013e8ca4fe
commit 523f4a3e76
11 changed files with 223 additions and 29 deletions

View file

@ -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) {

View file

@ -0,0 +1,37 @@
<?php
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @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;
}
}

View file

@ -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);
}

View file

@ -0,0 +1,21 @@
<?php
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @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) {
}
}

View file

@ -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) {
}
}

View file

@ -0,0 +1,20 @@
<?php
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @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) {
}
}

View file

@ -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);
}

View file

@ -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) {
}
}

View file

@ -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) {
}
}

View file

@ -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;
}
}

View file

@ -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,