Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Widgets\DataTable\Request;
|
||||
|
||||
use Xentral\Components\Http\Request;
|
||||
use Xentral\Widgets\DataTable\Exception\InvalidArgumentException;
|
||||
|
||||
final class DataTableRequest
|
||||
{
|
||||
/** @var Request $request */
|
||||
private $request;
|
||||
|
||||
/** @var DataTableRequestParameter $params */
|
||||
private $params;
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param DataTableRequestParameter $parameter
|
||||
*/
|
||||
public function __construct(Request $request, DataTableRequestParameter $parameter)
|
||||
{
|
||||
$method = $request->getMethod();
|
||||
if (!in_array($method, ['GET', 'POST'], true)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Can not create DataTableRequest instance. HTTP method "%s" is invalid.', $method
|
||||
));
|
||||
}
|
||||
|
||||
$this->request = $request;
|
||||
$this->params = $parameter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function fromRequest(Request $request)
|
||||
{
|
||||
$parameters = DataTableRequestParameter::fromRequest($request);
|
||||
|
||||
return new self($request, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isDataRequest()
|
||||
{
|
||||
if (!$this->isValidDataTableRequest()) {
|
||||
return false;
|
||||
}
|
||||
if (!$this->isAjax()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function isValidDataTableRequest()
|
||||
{
|
||||
if (empty($this->getParams()->getTableName())) {
|
||||
return false;
|
||||
}
|
||||
if ($this->params->getDraw() < 1) {
|
||||
return false;
|
||||
}
|
||||
if (empty($this->params->getColumnsValues()) ||
|
||||
empty($this->params->getOrderValues()) ||
|
||||
empty($this->params->getSearchValues())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isAjax()
|
||||
{
|
||||
return $this->request->isAjax();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod()
|
||||
{
|
||||
return $this->request->getMethod();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DataTableRequestParameter
|
||||
*/
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Request
|
||||
*/
|
||||
public function getOriginalRequest()
|
||||
{
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isExportRequest()
|
||||
{
|
||||
if (!$this->isValidDataTableRequest()) {
|
||||
return false;
|
||||
}
|
||||
if ($this->isAjax()) {
|
||||
return false;
|
||||
}
|
||||
if (empty($this->params->getExportValues())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Widgets\DataTable\Request;
|
||||
|
||||
use Xentral\Components\Http\Request;
|
||||
|
||||
/**
|
||||
* @see https://datatables.net/manual/server-side
|
||||
*/
|
||||
final class DataTableRequestParameter
|
||||
{
|
||||
/** @var string|null $tableName */
|
||||
private $tableName;
|
||||
|
||||
/** @var int $draw Draw counter */
|
||||
private $draw;
|
||||
|
||||
/** @var int $start Paging first record offset */
|
||||
private $start;
|
||||
|
||||
/** @var int $length Number of records returned */
|
||||
private $length;
|
||||
|
||||
/** @var array $columns Column settings and search queries */
|
||||
private $columns;
|
||||
|
||||
/** @var array $search Global search query */
|
||||
private $search;
|
||||
|
||||
/** @var array $order Ordering settings */
|
||||
private $order;
|
||||
|
||||
/** @var array $filter Custom parameter for filter feature */
|
||||
private $filter;
|
||||
|
||||
/** @var array $export Custom parameter for export feature */
|
||||
private $export;
|
||||
|
||||
/**
|
||||
* @param string $tableName
|
||||
* @param int $draw
|
||||
* @param int $start
|
||||
* @param int $length
|
||||
* @param array $columns
|
||||
* @param array $search
|
||||
* @param array $order
|
||||
* @param array $filter
|
||||
* @param array $export
|
||||
*/
|
||||
public function __construct(
|
||||
$tableName = null,
|
||||
$draw = 1,
|
||||
$start = 0,
|
||||
$length = 10,
|
||||
$columns = [],
|
||||
$search = [],
|
||||
$order = [],
|
||||
$filter = [],
|
||||
$export = []
|
||||
) {
|
||||
$this->tableName = $tableName;
|
||||
$this->draw = (int)$draw;
|
||||
$this->start = (int)$start;
|
||||
$this->length = (int)$length;
|
||||
$this->columns = (array)$columns;
|
||||
$this->search = (array)$search;
|
||||
$this->order = (array)$order;
|
||||
$this->filter = (array)$filter;
|
||||
$this->export = (array)$export;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function fromRequest(Request $request)
|
||||
{
|
||||
$params = $request->getMethod() === 'GET' ? $request->get : $request->post;
|
||||
|
||||
$tableName = $params->getAlphaNumWithDashes('tablename', null);
|
||||
$draw = $params->getInt('draw', 1);
|
||||
$start = $params->getInt('start', 0);
|
||||
$length = $params->getInt('length', 10);
|
||||
$columns = (array)$params->get('columns', []);
|
||||
$search = (array)$params->get('search', []);
|
||||
$order = (array)$params->get('order', []);
|
||||
$filter = (array)$params->get('filter', []);
|
||||
$export = (array)$params->get('export', []);
|
||||
|
||||
return new self($tableName, $draw, $start, $length, $columns, $search, $order, $filter, $export);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getTableName()
|
||||
{
|
||||
return $this->tableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getDraw()
|
||||
{
|
||||
return $this->draw;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStart()
|
||||
{
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getLength()
|
||||
{
|
||||
return $this->length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSearchValues()
|
||||
{
|
||||
return $this->search;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderValues()
|
||||
{
|
||||
return $this->order;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getColumnsValues()
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFilterValues()
|
||||
{
|
||||
return $this->filter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getExportValues()
|
||||
{
|
||||
return $this->export;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user