2017-09-15 14:36:37 +03:00
|
|
|
<?php
|
2016-07-08 22:35:48 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2017-01-31 09:35:34 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2017, Alex Tselegidis
|
2016-07-08 22:35:48 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.2.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
namespace EA\Engine\Api\V1;
|
2016-07-08 22:35:48 +03:00
|
|
|
|
2016-11-09 21:56:24 +03:00
|
|
|
use EA\Engine\Types\NonEmptyText;
|
2016-07-09 22:53:28 +03:00
|
|
|
|
2016-07-09 14:14:08 +03:00
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* API v1 Response
|
2016-07-09 14:14:08 +03:00
|
|
|
*
|
|
|
|
* 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:
|
2016-11-09 22:10:27 +03:00
|
|
|
* $parser = new \EA\Engine\Api\V1\Parsers\Appointments;
|
2016-07-09 14:14:08 +03:00
|
|
|
* $response = new \EA\Engine\Api\V1\Response($data);
|
2016-11-09 22:10:27 +03:00
|
|
|
* $response->format($parser)->search()->sort()->paginate()->minimize()->output();
|
2016-07-09 14:14:08 +03:00
|
|
|
*/
|
2016-07-08 22:35:48 +03:00
|
|
|
class Response {
|
2016-07-09 14:14:08 +03:00
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Contains the response information.
|
|
|
|
*
|
2016-07-09 14:14:08 +03:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $response;
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Class Constructor
|
|
|
|
*
|
|
|
|
* @param array $response Provide unfiltered data to be processed as an array.
|
2016-07-09 14:14:08 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function __construct(array $response)
|
|
|
|
{
|
2016-07-09 14:14:08 +03:00
|
|
|
$this->response = $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Encode the response entries to the API compatible structure.
|
|
|
|
*
|
2016-11-09 22:10:27 +03:00
|
|
|
* @param Parsers\ParsersInterface $parser Provide the corresponding parser class.
|
2016-07-09 14:14:08 +03:00
|
|
|
*
|
|
|
|
* @return \EA\Engine\Api\V1\Response
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function encode(Parsers\ParsersInterface $parser)
|
|
|
|
{
|
|
|
|
foreach ($this->response as &$entry)
|
|
|
|
{
|
2016-07-09 22:53:28 +03:00
|
|
|
$parser->encode($entry);
|
|
|
|
}
|
2016-07-09 14:14:08 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Perform a response search.
|
|
|
|
*
|
2016-07-09 14:14:08 +03:00
|
|
|
* @return \EA\Engine\Api\V1\Response
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function search()
|
|
|
|
{
|
|
|
|
Processors\Search::process($this->response);
|
2016-07-09 14:14:08 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Perform a response sort.
|
2016-07-09 14:14:08 +03:00
|
|
|
*
|
|
|
|
* @return \EA\Engine\Api\V1\Response
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function sort()
|
|
|
|
{
|
|
|
|
Processors\Sort::process($this->response);
|
2016-07-09 14:14:08 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Perform a response paginate.
|
2016-07-09 14:14:08 +03:00
|
|
|
*
|
|
|
|
* @return \EA\Engine\Api\V1\Response
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function paginate()
|
|
|
|
{
|
|
|
|
Processors\Paginate::process($this->response);
|
2016-07-09 14:14:08 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Perform a response minimize.
|
2016-07-09 14:14:08 +03:00
|
|
|
*
|
|
|
|
* @return \EA\Engine\Api\V1\Response
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function minimize()
|
|
|
|
{
|
2016-07-09 14:14:08 +03:00
|
|
|
Processors\Minimize::process($this->response);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-07-09 22:11:33 +03:00
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Return a single entry instead of an array of entries.
|
2016-07-09 22:11:33 +03:00
|
|
|
*
|
2016-07-10 12:38:22 +03:00
|
|
|
* This is useful whenever the client requests only a single entry. Make sure that you call this method
|
2017-09-15 14:36:37 +03:00
|
|
|
* right before the output() in order to avoid side-effects with the other processor methods of this class.
|
2016-07-09 22:11:33 +03:00
|
|
|
*
|
2017-09-15 14:36:37 +03:00
|
|
|
* @param int $id Provide the ID value of the request UI and if is not null the response will be
|
2016-07-10 12:38:22 +03:00
|
|
|
* converted into an associative array.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2016-07-09 22:11:33 +03:00
|
|
|
* @return \EA\Engine\Api\V1\Response
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function singleEntry($id)
|
|
|
|
{
|
|
|
|
if ($id !== NULL)
|
|
|
|
{
|
2016-07-10 12:38:22 +03:00
|
|
|
$this->response = array_shift($this->response);
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2016-07-09 22:11:33 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-07-09 14:14:08 +03:00
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Output the response as a JSON with the provided status header.
|
2016-07-09 14:14:08 +03:00
|
|
|
*
|
2016-11-09 21:56:24 +03:00
|
|
|
* @param \EA\Engine\Types\NonEmptyText $status Optional (null), if provided it must contain the status
|
2016-07-09 14:14:08 +03:00
|
|
|
* header value. Default string: '200 OK'.
|
|
|
|
*
|
|
|
|
* @return \EA\Engine\Api\V1\Response
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function output(NonEmptyText $status = NULL)
|
|
|
|
{
|
|
|
|
$header = $status ? $status->get() : '200 OK';
|
2016-07-09 14:14:08 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
header('HTTP/1.0 ' . $header);
|
2016-07-09 14:14:08 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
echo json_encode($this->response, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
|
2016-07-09 14:14:08 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2016-07-08 22:35:48 +03:00
|
|
|
}
|