diff --git a/.travis.yml b/.travis.yml index 924bf42..e625349 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,8 @@ language: php php: - 7.1 - 7.2 + - 7.3 + - 7.4 before_script: - travis_retry composer update --prefer-source --no-interaction diff --git a/src/Column.php b/src/Column.php index 032496b..31295fb 100644 --- a/src/Column.php +++ b/src/Column.php @@ -40,7 +40,7 @@ class Column /** * @var array */ - public $attr; + public $attr = []; /** * @var bool @@ -61,6 +61,9 @@ class Column public function __construct($name) { $this->name = $name; + $this->attr['searchable'] = false; + $this->attr['orderable'] = false; + $this->attr['search'] = ['value' => '']; } /** diff --git a/src/DB/DBAdapter.php b/src/DB/DBAdapter.php index e59cc85..6f7f7ab 100644 --- a/src/DB/DBAdapter.php +++ b/src/DB/DBAdapter.php @@ -70,7 +70,7 @@ public function makeWhereString(array $filter) * @param $word * @return string */ - public function makeLikeString(Query $query, Column $column,string $word) + public function makeLikeString(Query $query, Column $column, string $word) { return $column->name.' LIKE '.$this->escape('%'.$word.'%', $query); } diff --git a/src/Datatables.php b/src/Datatables.php index 5865d14..7569b97 100644 --- a/src/Datatables.php +++ b/src/Datatables.php @@ -29,9 +29,9 @@ class Datatables protected $builder; /** - * @var Request + * @var Option */ - protected $request; + public $options; /** * @var array @@ -57,7 +57,7 @@ class Datatables public function __construct(DatabaseInterface $db, Request $request = null) { $this->db = $db->connect(); - $this->request = $request ?: Request::createFromGlobals(); + $this->options = new Option($request ?: Request::createFromGlobals()); } /** @@ -146,6 +146,7 @@ public function getQuery(): Query public function hide(string $column, $searchable = false): Datatables { $this->columns->getByName($column)->hide($searchable); + return $this; } @@ -155,7 +156,7 @@ public function hide(string $column, $searchable = false): Datatables */ public function query($query): Datatables { - $this->builder = new QueryBuilder($query, $this->request, $this->db); + $this->builder = new QueryBuilder($query, $this->options, $this->db); $this->columns = $this->builder->columns(); return $this; @@ -216,7 +217,7 @@ private function getDistinctData(): array */ public function setResponseData(): void { - $this->response['draw'] = (integer)$this->request->get('draw'); + $this->response['draw'] = $this->options->draw(); $this->response['recordsTotal'] = $this->db->count($this->builder->query); $this->response['recordsFiltered'] = $this->db->count($this->builder->filtered); $this->response['data'] = $this->getData(); diff --git a/src/Explode.php b/src/Explode.php index 67e124e..c824027 100644 --- a/src/Explode.php +++ b/src/Explode.php @@ -4,7 +4,6 @@ /** * Trait Explode - * @package Ozdemir\Datatables */ trait Explode diff --git a/src/Http/Request.php b/src/Http/Request.php index a2b65f3..6affeb7 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -8,7 +8,6 @@ */ class Request { - /** * @var ParameterBag */ diff --git a/src/Option.php b/src/Option.php new file mode 100644 index 0000000..6898c9e --- /dev/null +++ b/src/Option.php @@ -0,0 +1,77 @@ +request = $request; + } + + /** + * @return int + */ + public function draw(): int + { + return $this->request->get('draw') ?? 0; + } + + /** + * @return int + */ + public function start(): int + { + return $this->request->get('start') ?? 0; + } + + /** + * @return int + */ + public function length(): int + { + return $this->request->get('length') ?? 0; + } + + /** + * @return string + */ + public function searchValue(): string + { + $search = $this->request->get('search') ?? []; + + return array_key_exists('value', $search) ? $search['value'] : ''; + } + + /** + * @return array + */ + public function order(): array + { + return $this->request->get('order') ?? []; + } + + /** + * @return array + */ + public function columns(): array + { + return $this->request->get('columns') ?? []; + } +} diff --git a/src/Query.php b/src/Query.php index e32b523..27ba07d 100644 --- a/src/Query.php +++ b/src/Query.php @@ -4,7 +4,6 @@ /** * Class Query - * @package Ozdemir\Datatables */ class Query diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index fe229d8..ca24156 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -4,7 +4,6 @@ use Ozdemir\Datatables\DB\DatabaseInterface; use Ozdemir\Datatables\Iterators\ColumnCollection; -use Ozdemir\Datatables\Http\Request; /** * Class Query Builder @@ -44,9 +43,9 @@ class QueryBuilder private $columns; /** - * @var Request + * @var Option */ - private $request; + private $options; /** * @var DatabaseInterface @@ -61,12 +60,12 @@ class QueryBuilder /** * * @param string $query - * @param Request $request + * @param Option $options * @param DatabaseInterface $db */ - public function __construct($query, Request $request, DatabaseInterface $db) + public function __construct($query, Option $options, DatabaseInterface $db) { - $this->request = $request; + $this->options = $options; $this->db = $db; $columnNames = ColumnNameList::from($query); @@ -88,8 +87,7 @@ public function __construct($query, Request $request, DatabaseInterface $db) */ public function setColumnAttributes(): void { - $columns = $this->request->get('columns'); - + $columns = $this->options->columns(); if ($columns) { $attributes = array_column($columns, null, 'data'); @@ -114,11 +112,11 @@ public function isDataObject(): bool */ public function checkAssoc(): bool { - if (!$this->request->get('columns')) { + if (!$this->options->columns()) { return false; } - $data = array_column($this->request->get('columns'), 'data'); + $data = array_column($this->options->columns(), 'data'); $rangeSet = array_map('strval', array_keys($data)); return array_intersect($data, $rangeSet) !== $data; @@ -211,7 +209,7 @@ protected function filter(Query $query): string */ protected function filterGlobal(Query $query): string { - $searchinput = preg_replace("/\W+/u", ' ', $this->request->get('search')['value']); + $searchinput = preg_replace("/\W+/u", ' ', $this->options->searchValue()); $columns = $this->columns->searchable(); if ($searchinput === null || $searchinput === '' || \count($columns) === 0) { @@ -254,14 +252,10 @@ protected function filterIndividual(Query $query): string */ protected function limit(): string { - $take = 10; - $skip = (integer)$this->request->get('start'); - - if ($this->request->get('length')) { - $take = (integer)$this->request->get('length'); - } + $skip = $this->options->start(); + $take = $this->options->length() ?: 10; - if ($take === -1 || !$this->request->get('draw')) { + if ($take === -1 || !$this->options->draw()) { return ''; } @@ -273,7 +267,7 @@ protected function limit(): string */ protected function orderBy(): string { - $orders = $this->request->get('order') ?: []; + $orders = $this->options->order(); $orders = array_filter($orders, function ($order) { return \in_array($order['dir'], ['asc', 'desc'], @@ -283,7 +277,7 @@ protected function orderBy(): string $o = []; foreach ($orders as $order) { - $id = $this->request->get('columns')[$order['column']]['data']; + $id = $this->options->columns()[$order['column']]['data']; if ($this->columns->visible()->isExists($id)) { $o[] = $this->columns->visible()->get($id)->name.' '.$order['dir'];