Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/Traits/SearchTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ public function searchQuery(array $filter = []): self
if (Str::endsWith($fieldName, '_not_in_list')) {
$field = Str::replace('_not_in_list', '', $fieldName);
$this->query->whereNotIn($field, $value);
} elseif (Str::endsWith($fieldName, '_gte')) {
$field = Str::replace('_gte', '', $fieldName);
$this->filterGreater($field, false, $fieldName);
} elseif (Str::endsWith($fieldName, '_gt')) {
$field = Str::replace('_gt', '', $fieldName);
$this->filterGreater($field, true, $fieldName);
} elseif (Str::endsWith($fieldName, '_lte')) {
$field = Str::replace('_lte', '', $fieldName);
$this->filterLess($field, false, $fieldName);
} elseif (Str::endsWith($fieldName, '_lt')) {
$field = Str::replace('_lt', '', $fieldName);
$this->filterLess($field, true, $fieldName);
} elseif (Str::endsWith($fieldName, '_from')) {
$field = Str::replace('_from', '', $fieldName);
$this->filterFrom($field, false, $fieldName);
Expand Down Expand Up @@ -201,7 +213,7 @@ public function orderBy(?string $default = null, bool $defaultDesc = false): sel

protected function getDesc(bool $isDesc): string
{
return $isDesc ? 'DESC' : 'ASC';
return ($isDesc) ? 'DESC' : 'ASC';
}

public function filterMoreThan(string $field, $value): self
Expand Down Expand Up @@ -270,10 +282,16 @@ protected function getQuerySearchCallback(string $field, string $mask): Closure
};
}

public function filterFrom(string $field, bool $strict = true, ?string $filterName = null): self
/** @deprecated use filterGreater instead */
public function filterFrom(string $field, bool $isStrict = true, ?string $filterName = null): self
{
return $this->filterGreater($field, $isStrict, $filterName);
}

public function filterGreater(string $field, bool $isStrict = true, ?string $filterName = null): self
{
$filterName = empty($filterName) ? 'from' : $filterName;
$sign = $strict ? '>' : '>=';
$sign = ($isStrict) ? '>' : '>=';

if (isset($this->filter[$filterName])) {
$this->addWhere($this->query, $field, $this->filter[$filterName], $sign);
Expand All @@ -282,10 +300,16 @@ public function filterFrom(string $field, bool $strict = true, ?string $filterNa
return $this;
}

public function filterTo(string $field, bool $strict = true, ?string $filterName = null): self
/** @deprecated use filterLess instead */
public function filterTo(string $field, bool $isStrict = true, ?string $filterName = null): self
{
return $this->filterLess($field, $isStrict, $filterName);
}

public function filterLess(string $field, bool $isStrict = true, ?string $filterName = null): self
{
$filterName = empty($filterName) ? 'to' : $filterName;
$sign = $strict ? '<' : '<=';
$filterName = (empty($filterName)) ? 'to' : $filterName;
$sign = ($isStrict) ? '<' : '<=';

if (isset($this->filter[$filterName])) {
$this->addWhere($this->query, $field, $this->filter[$filterName], $sign);
Expand Down
8 changes: 6 additions & 2 deletions tests/SearchTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,12 @@ public function testSearchQueryWithFilters()

$this->testRepositoryClass
->searchQuery([
'date_from' => Carbon::now(),
'date_to' => Carbon::now(),
'date_gte' => Carbon::now(),
'date_lte' => Carbon::now(),
'created_at_from' => Carbon::now(),
'created_at_to' => Carbon::now(),
'updated_at_gt' => Carbon::now(),
'updated_at_lt' => Carbon::now(),
'user_id_in_list' => [1, 2],
'user_id_not_in_list' => [3, 4],
'name' => 'text_name',
Expand Down
23 changes: 19 additions & 4 deletions tests/support/Traits/SqlMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,34 @@ protected function mockGetSearchResultWithRelations(array $selectResult): void

protected function mockGetSearchResultWithFilters(array $selectResult): void
{
$pdo = $this->mockSelect("select count(*) as aggregate from `test_models` where `date` >= ? and `date` <= ? and `user_id` in (?, ?) and `user_id` not in (?, ?) and `name` in (?) and `test_models`.`deleted_at` is null", [
$pdo = $this->mockSelect(
"select count(*) as aggregate from `test_models` where `date` >= ? and `date` <= ? and `created_at` >= ? and `created_at` <= ? and `updated_at` > ? and `updated_at` < ? and `user_id` in (?, ?) and `user_id` not in (?, ?) and `name` in (?) and `test_models`.`deleted_at` is null", [
Carbon::now(),
Carbon::now(),
1, 2, 3, 4,
Carbon::now(),
Carbon::now(),
Carbon::now(),
Carbon::now(),
1,
2,
3,
4,
'text_name',
], [
['aggregate' => 1]
]);

$this->mockSelect("select * from `test_models` where `date` >= ? and `date` <= ? and `user_id` in (?, ?) and `user_id` not in (?, ?) and `name` in (?) and `test_models`.`deleted_at` is null order by `id` asc limit 15 offset 0", [
$this->mockSelect("select * from `test_models` where `date` >= ? and `date` <= ? and `created_at` >= ? and `created_at` <= ? and `updated_at` > ? and `updated_at` < ? and `user_id` in (?, ?) and `user_id` not in (?, ?) and `name` in (?) and `test_models`.`deleted_at` is null order by `id` asc limit 15 offset 0", [
Carbon::now(),
Carbon::now(),
1, 2, 3, 4,
Carbon::now(),
Carbon::now(),
Carbon::now(),
Carbon::now(),
1,
2,
3,
4,
'text_name',
], $selectResult, $pdo);
}
Expand Down