*/ class ResultsPayload implements DataTablePayloadInterface { private $request; /** * @var ResultsExporterInterface */ private $exporter; public function __construct(ResultsExporterInterface $exporter) { $this->request = DatatableRequest::fromGlobals(); $this->exporter = $exporter; } /** * @return array */ public function getPayload() { $data = $this->exporter->getData(); return $this->doPostProcessing($data); } private function doPostProcessing($data) { $countTotal = count($data); $filters = $this->getFilters(); $this->limitRows($data, $filters); $payload = [ 'data' => $data, 'page' => $this->request->getPage(), 'amount' => count($data), 'total' => ceil($countTotal / $filters['limit']) ]; return $payload; } private function limitRows(&$data, $filters) { $data = array_slice($data, $filters['offset'], $filters['limit']); } protected function getFilters() { $page = $this->request->getPage(); $limit = $this->request->getRows(); $filters = [ 'offset' => $limit * ($page - 1), 'limit' => $limit ]; return $filters; } /** * @return array */ public function jsonSerialize() { return $this->getPayload(); } }