Skip to content

Commit

Permalink
added php 7.3 and 7.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
n1crack committed Dec 3, 2019
1 parent 0c1696f commit 1427c04
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Column
/**
* @var array
*/
public $attr;
public $attr = [];

/**
* @var bool
Expand All @@ -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' => ''];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/DB/DBAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 6 additions & 5 deletions src/Datatables.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Datatables
protected $builder;

/**
* @var Request
* @var Option
*/
protected $request;
public $options;

/**
* @var array
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -146,6 +146,7 @@ public function getQuery(): Query
public function hide(string $column, $searchable = false): Datatables
{
$this->columns->getByName($column)->hide($searchable);

return $this;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/Explode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

/**
* Trait Explode
* @package Ozdemir\Datatables
*/
trait Explode
Expand Down
1 change: 0 additions & 1 deletion src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
class Request
{

/**
* @var ParameterBag
*/
Expand Down
77 changes: 77 additions & 0 deletions src/Option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php


namespace Ozdemir\Datatables;

use Ozdemir\Datatables\Http\Request;

/**
* Class Option
* @package Ozdemir\Datatables
*/
class Option
{
/**
* @var Request
*/
private $request;

/**
* Option constructor.
* @param Request $request
*/
public function __construct(Request $request)
{
$this->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') ?? [];
}
}
1 change: 0 additions & 1 deletion src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

/**
* Class Query
* @package Ozdemir\Datatables
*/
class Query
Expand Down
34 changes: 14 additions & 20 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Ozdemir\Datatables\DB\DatabaseInterface;
use Ozdemir\Datatables\Iterators\ColumnCollection;
use Ozdemir\Datatables\Http\Request;

/**
* Class Query Builder
Expand Down Expand Up @@ -44,9 +43,9 @@ class QueryBuilder
private $columns;

/**
* @var Request
* @var Option
*/
private $request;
private $options;

/**
* @var DatabaseInterface
Expand All @@ -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);
Expand All @@ -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');

Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 '';
}

Expand All @@ -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'],
Expand All @@ -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'];
Expand Down

0 comments on commit 1427c04

Please sign in to comment.