Skip to content

Commit

Permalink
improve filter method, add forceExactMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
n1crack committed Jun 8, 2024
1 parent 35e4a64 commit 0cd4211
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 17 deletions.
28 changes: 24 additions & 4 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,19 @@ class Column
* Custom filter
* @var \Closure
*/
public $customFilter;
public $customIndividualFilter;

/**
* Custom filter
* @var \Closure
*/
public $customGlobalFilter;

/**
*
* @var string
*/
public $customFilterType;

/**
* Column constructor.
Expand Down Expand Up @@ -82,7 +94,7 @@ public function value($row): string
* Set visibility of the column.
* @param bool $searchable
*/
public function hide($searchable = false): void
public function hide(bool $searchable = false): void
{
$this->hidden = true;
$this->forceSearch = $searchable;
Expand All @@ -91,9 +103,17 @@ public function hide($searchable = false): void
/**
* @return bool
*/
public function hasFilter(): bool
public function hasCustomIndividualFilter(): bool
{
return $this->customIndividualFilter instanceof \Closure;
}

/**
* @return bool
*/
public function hasCustomGlobalFilter(): bool
{
return $this->customFilter instanceof \Closure;
return $this->customGlobalFilter instanceof \Closure;
}

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

namespace Ozdemir\Datatables;

class CustomFilterType
{
const INDIVIDUAL = 'individual';
const GLOBALLY = 'globally';
const ALL = 'all';
}
32 changes: 31 additions & 1 deletion src/DB/DBAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

abstract class DBAdapter implements DatabaseInterface
{
public $exactMatch = false;

/**
* @return void
Expand Down Expand Up @@ -64,17 +65,46 @@ public function makeWhereString(array $filter)
return ' WHERE '.implode(' AND ', $filter);
}


/**
* @return bool
*/
public function isExactMatch()
{
return $this->exactMatch;
}

/**
* @param bool $value
* @return void
*/
public function setExactMatch(bool $value)
{
$this->exactMatch = $value;
}

/**
* @param Query $query
* @param Column $column
* @param $word
* @param string $word
* @return string
*/
public function makeLikeString(Query $query, Column $column, string $word)
{
return $column->name.' LIKE '.$this->escape('%'.$word.'%', $query);
}

/**
* @param Query $query
* @param Column $column
* @param string $word
* @return string
*/
public function makeEqualString(Query $query, Column $column, string $word)
{
return $column->name.' = '.$this->escape( $word, $query);
}

/**
* @param array $o
* @return string
Expand Down
20 changes: 20 additions & 0 deletions src/DB/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public function makeDistinctQueryString(Query $query, string $column);
*/
public function makeWhereString(array $filter);

/**
* @param bool $value
* @return void
*/
public function setExactMatch(bool $value);

/**
* @return mixed
*/
public function isExactMatch();

/**
* @param Query $query
* @param Column $column
Expand All @@ -62,6 +73,15 @@ public function makeWhereString(array $filter);
*/
public function makeLikeString(Query $query, Column $column, string $word);


/**
* @param Query $query
* @param Column $column
* @param string $word
* @return mixed
*/
public function makeEqualString(Query $query, Column $column, string $word);

/**
* @param array $o
* @return mixed
Expand Down
23 changes: 21 additions & 2 deletions src/Datatables.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,18 @@ public function edit($column, Closure $closure): Datatables
* @param Closure $closure
* @return Datatables
*/
public function filter($column, Closure $closure): Datatables
public function filter($column, Closure $closure, $filterType = CustomFilterType::INDIVIDUAL): Datatables
{
$column = $this->columns->getByName($column);
$column->customFilter = $closure;
$column->customIndividualFilter = $closure;

$column->customFilterType = $filterType;
if ($filterType !== CustomFilterType::GLOBALLY) {
$column->customIndividualFilter = $closure;
}
if ($filterType !== CustomFilterType::INDIVIDUAL) {
$column->customGlobalFilter = $closure;
}

return $this;
}
Expand All @@ -130,6 +138,17 @@ public function escape($key, $value): Datatables
return $this;
}

/**
* @param bool $value
* @return Datatables
*/
public function forceExactMatch(bool $value): Datatables
{
$this->db->setExactMatch($value);

return $this;
}

/**
* @param $name
* @return Datatables
Expand Down
17 changes: 14 additions & 3 deletions src/FilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@ class FilterHelper
*/
private $db;

/**
* @var string|null
*/
private $searchValue;


/**
* FilterHelper constructor.
* @param Query $query
* @param Column $column
* @param DatabaseInterface $db
*/
public function __construct(Query $query, Column $column, DatabaseInterface $db)
public function __construct(Query $query, Column $column, DatabaseInterface $db, $searchValue = null)
{
$this->query = $query;
$this->column = $column;
$this->db = $db;
$this->searchValue = $searchValue;
}

/**
Expand All @@ -51,14 +58,18 @@ public function escape($value): string
*/
public function searchValue(): string
{
return $this->column->searchValue();
return $this->searchValue ?? $this->column->searchValue();
}

/**
* @return string
*/
public function defaultFilter(): string
{
if ($this->db->isExactMatch()) {
return $this->db->makeEqualString($this->query, $this->column, $this->searchValue());
}

return $this->db->makeLikeString($this->query, $this->column, $this->searchValue());
}

Expand Down Expand Up @@ -116,4 +127,4 @@ public function lessThan($value): string
return $this->column->name.' <= '.$this->escape($value);
}

}
}
4 changes: 2 additions & 2 deletions src/Iterators/IndividualSearchableColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class IndividualSearchableColumns extends FilterIterator
*/
public function accept(): bool
{
return $this->current()->searchValue() !== '' || $this->current()->hasFilter();
return $this->current()->searchValue() !== '' || $this->current()->hasCustomIndividualFilter();
}
}
}
26 changes: 21 additions & 5 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,11 @@ protected function filterGlobal(Query $query): string
$look = [];

foreach ($columns as $column) {
$look[] = $this->db->makeLikeString($query, $column, $word);
$look[] = $this->columnGlobalFilter($column, new FilterHelper($query, $column, $this->db, $word));
}

$look = array_filter($look);

$search[] = '('.implode(' OR ', $look).')';
}

Expand All @@ -250,7 +252,7 @@ protected function filterIndividual(Query $query): string
$look = [];

foreach ($columns as $column) {
$look[] = $this->columnFilter($column, new FilterHelper($query, $column, $this->db));
$look[] = $this->columnIndividualFilter($column, new FilterHelper($query, $column, $this->db));
}

$look = array_filter($look);
Expand Down Expand Up @@ -319,10 +321,24 @@ public function defaultOrder(): string
* @param FilterHelper $helper
* @return string
*/
public function columnFilter(Column $column, FilterHelper $helper): string
public function columnIndividualFilter(Column $column, FilterHelper $helper): string
{
if ($column->hasCustomIndividualFilter()) {
return $column->customIndividualFilter->call($helper) ?? $helper->defaultFilter();
}

return $helper->defaultFilter();
}

/**
* @param Column $column
* @param FilterHelper $helper
* @return string
*/
public function columnGlobalFilter(Column $column, FilterHelper $helper): string
{
if ($column->hasFilter()) {
return $column->customFilter->call($helper) ?? $helper->defaultFilter();
if ($column->hasCustomGlobalFilter()) {
return $column->customGlobalFilter->call($helper) ?? $helper->defaultFilter();
}

return $helper->defaultFilter();
Expand Down

0 comments on commit 0cd4211

Please sign in to comment.