-
-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add operator filter (<, =, >, ...) (#940)
* [FEAT] add filter by operators * [TEST] add test cases for filter by operator * [FEAT] add dynamic operator to filer by operators * [TEST] test dynamic operator filter
- Loading branch information
1 parent
34f35ce
commit 09d9162
Showing
6 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Spatie\QueryBuilder\Enums; | ||
|
||
enum FilterOperator: string | ||
{ | ||
case DYNAMIC = ''; | ||
case EQUAL = '='; | ||
case LESS_THAN = '<'; | ||
case GREATER_THAN = '>'; | ||
case LESS_THAN_OR_EQUAL = '<='; | ||
case GREATER_THAN_OR_EQUAL = '>='; | ||
case NOT_EQUAL = '<>'; | ||
|
||
public function isDynamic() | ||
{ | ||
return self::DYNAMIC === $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Spatie\QueryBuilder\Filters; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Spatie\QueryBuilder\Enums\FilterOperator; | ||
|
||
/** | ||
* @template TModelClass of \Illuminate\Database\Eloquent\Model | ||
* @template-implements \Spatie\QueryBuilder\Filters\Filter<TModelClass> | ||
*/ | ||
class FiltersOperator extends FiltersExact implements Filter | ||
{ | ||
public function __construct(protected bool $addRelationConstraint, protected FilterOperator $filterOperator, protected string $boolean) | ||
{ | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function __invoke(Builder $query, $value, string $property) | ||
{ | ||
$filterOperator = $this->filterOperator; | ||
|
||
if ($this->addRelationConstraint) { | ||
if ($this->isRelationProperty($query, $property)) { | ||
$this->withRelationConstraint($query, $value, $property); | ||
|
||
return; | ||
} | ||
} | ||
|
||
if (is_array($value)) { | ||
$query->where(function ($query) use ($value, $property) { | ||
foreach($value as $item) { | ||
$this->__invoke($query, $item, $property); | ||
} | ||
}); | ||
|
||
return; | ||
} | ||
else if ($this->filterOperator->isDynamic()) { | ||
$filterOperator = $this->getDynamicFilterOperator($value, $this); | ||
$this->removeDynamicFilterOperatorFromValue($value, $filterOperator); | ||
} | ||
|
||
$query->where($query->qualifyColumn($property), $filterOperator->value, $value, $this->boolean); | ||
} | ||
|
||
protected function getDynamicFilterOperator(string $value): FilterOperator | ||
{ | ||
$filterOperator = FilterOperator::EQUAL; | ||
|
||
// match filter operators and assign the filter operator. | ||
foreach(FilterOperator::cases() as $filterOperatorCase) { | ||
if (str_starts_with($value, $filterOperatorCase->value) && ! $filterOperatorCase->isDynamic()) { | ||
$filterOperator = $filterOperatorCase; | ||
} | ||
} | ||
|
||
return $filterOperator; | ||
} | ||
|
||
protected function removeDynamicFilterOperatorFromValue(string &$value, FilterOperator $filterOperator) | ||
{ | ||
if (str_contains($value, $filterOperator->value)) { | ||
$value = substr_replace($value, '', 0, strlen($filterOperator->value)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters