2017-09-15 14:36:37 +03:00
|
|
|
<?php
|
2016-07-09 10:02:25 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2020-03-11 12:10:59 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
|
2016-07-09 10:02:25 +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\Processors;
|
2016-07-09 10:02:25 +03:00
|
|
|
|
2016-07-10 11:59:39 +03:00
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Paginate Processor
|
2016-07-10 11:59:39 +03:00
|
|
|
*
|
|
|
|
* This class will handle the pagination GET parameters ("page", "length"). The "page" parameter
|
|
|
|
* is required in order for the pagination to work. The "length" is optional and the default value
|
2017-09-15 14:36:37 +03:00
|
|
|
* is 20 entries per page.
|
2016-07-10 11:59:39 +03:00
|
|
|
*
|
2017-09-15 14:36:37 +03:00
|
|
|
* Make sure that the response parameter is a sequential array and not a single entry by the time this
|
2016-07-10 11:59:39 +03:00
|
|
|
* processor is executed.
|
2020-05-12 21:49:15 +03:00
|
|
|
*
|
|
|
|
* @deprecated
|
2016-07-10 11:59:39 +03:00
|
|
|
*/
|
2016-07-09 14:14:08 +03:00
|
|
|
class Paginate implements ProcessorsInterface {
|
2016-07-10 11:59:39 +03:00
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* Process Response Array
|
2016-07-10 11:59:39 +03:00
|
|
|
*
|
2017-09-15 14:36:37 +03:00
|
|
|
* Example:
|
2020-11-16 12:54:59 +03:00
|
|
|
* http://example.org/api/v1/appointments?page=3&length=30
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2016-07-10 11:59:39 +03:00
|
|
|
* @param array &$response The response array to be processed.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public static function process(array &$response)
|
|
|
|
{
|
|
|
|
if ( ! isset($_GET['page']) || empty($response))
|
|
|
|
{
|
|
|
|
return;
|
2016-07-09 23:34:45 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$page = (int)$_GET['page'];
|
|
|
|
$length = isset($_GET['length']) ? (int)$_GET['length'] : 20;
|
2016-07-09 23:34:45 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$chunks = array_chunk($response, $length);
|
2016-07-09 23:34:45 +03:00
|
|
|
|
2016-07-10 13:51:37 +03:00
|
|
|
$response = isset($chunks[$page - 1]) ? $chunks[$page - 1] : [];
|
2016-07-09 14:14:08 +03:00
|
|
|
}
|
2016-07-09 10:02:25 +03:00
|
|
|
}
|