Skip to content

Commit 292206b

Browse files
committed
Implemented empty, notEmpty, notEndsWith, notStartsWith filters
1 parent 594c9b1 commit 292206b

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

src/Infrastructure/Model/Repository/Sql/SqlFilter.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,6 @@ protected static function apply(
171171
}
172172
self::likeQuery($placeholders, $query, $operator, $nextPlaceholder, $key, $op, $value);
173173
break;
174-
case BaseFilter::STARTS_WITH:
175-
case BaseFilter::ENDS_WITH:
176-
$op = (!$isNot) ? 'LIKE' : 'NOT LIKE';
177-
$newValue = $value.'%';
178-
if ($filterName === BaseFilter::ENDS_WITH) {
179-
$newValue = '%'.$value;
180-
}
181-
self::likeQuery($placeholders, $query, $operator, $nextPlaceholder, $key, $op, $newValue);
182-
break;
183174
case BaseFilter::EQUALS:
184175
case BaseFilter::NOT_EQUAL:
185176
$op = (!$isNot) ? 'eq' : 'neq';
@@ -188,6 +179,30 @@ protected static function apply(
188179
}
189180
self::query($placeholders, $query, $operator, $nextPlaceholder, $key, $op, $value);
190181
break;
182+
183+
case BaseFilter::EMPTY_FILTER:
184+
$op = (!$isNot) ? 'eq' : 'neq';
185+
self::query($placeholders, $query, $operator, $nextPlaceholder, $key, $op, '');
186+
break;
187+
188+
case BaseFilter::NOT_EMPTY:
189+
$op = (!$isNot) ? 'neq' : 'eq';
190+
self::query($placeholders, $query, $operator, $nextPlaceholder, $key, $op, '');
191+
break;
192+
193+
case BaseFilter::ENDS_WITH:
194+
case BaseFilter::NOT_ENDS:
195+
$op = (!$isNot) ? 'LIKE' : 'NOT LIKE';
196+
$newValue = '%'.$value;
197+
self::likeQuery($placeholders, $query, $operator, $nextPlaceholder, $key, $op, $newValue);
198+
break;
199+
200+
case BaseFilter::STARTS_WITH:
201+
case BaseFilter::NOT_STARTS:
202+
$op = (!$isNot) ? 'LIKE' : 'NOT LIKE';
203+
$newValue = $value.'%';
204+
self::likeQuery($placeholders, $query, $operator, $nextPlaceholder, $key, $op, $newValue);
205+
break;
191206
}
192207
}
193208
}

tests/Infrastructure/Model/Repository/Sql/SqlRepositoryTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,29 @@ public function testFindByMustIncludeGroup()
346346
$this->assertEquals(3, count($results));
347347
}
348348

349+
public function testFindByWithMustBeEmpty()
350+
{
351+
$filter = new Filter();
352+
$filter->must()->empty('totalOrders');
353+
354+
$fields = new Fields(['name']);
355+
$results = $this->repository->findBy($filter, null, $fields);
356+
357+
$this->assertEquals(0, count($results));
358+
}
359+
360+
public function testFindByWithMustBeNotEmpty()
361+
{
362+
$filter = new Filter();
363+
$filter->must()->notEmpty('totalOrders');
364+
365+
$fields = new Fields(['name']);
366+
367+
$results = $this->repository->findBy($filter, null, $fields);
368+
369+
$this->assertEquals(4, count($results));
370+
}
371+
349372
//--------------------------------------------------------------------------------
350373
// MUST NOT FILTER TESTS
351374
//--------------------------------------------------------------------------------
@@ -513,6 +536,28 @@ public function testFindByMustNotNotRangeTest()
513536
$this->assertEquals(3, count($results));
514537
}
515538

539+
public function testFindByWithMustNotBeEmpty()
540+
{
541+
$filter = new Filter();
542+
$filter->mustNot()->empty('totalOrders');
543+
544+
$fields = new Fields(['name']);
545+
$results = $this->repository->findBy($filter, null, $fields);
546+
547+
$this->assertEquals(4, count($results));
548+
}
549+
550+
public function testFindByWithMustNotBeNotEmpty()
551+
{
552+
$filter = new Filter();
553+
$filter->mustNot()->notEmpty('totalOrders');
554+
555+
$fields = new Fields(['name']);
556+
557+
$results = $this->repository->findBy($filter, null, $fields);
558+
559+
$this->assertEquals(0, count($results));
560+
}
516561
//--------------------------------------------------------------------------------
517562
// SHOULD FILTER TESTS
518563
//--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)