Skip to content

Commit b688f43

Browse files
committed
Added "in" and "not-in" suffixes
1 parent df17e9e commit b688f43

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,23 @@ Or all the books except the ones with id 5 or 6:
140140
/api/books?id-not=5|6
141141
```
142142

143+
The same could be achieved using the `-in` suffix:
144+
```
145+
/api/books?id-in=5,6
146+
```
147+
Respectively the `not-in` suffix:
148+
```
149+
/api/books?id-not-in=5,6
150+
```
151+
152+
143153
#####Suffixes#####
144154
Suffix | Operator | Meaning
145155
------------- | ------------- | -------------
146156
-lk | LIKE | Same as the SQL `LIKE` operator
147157
-not-lk | NOT LIKE | Same as the SQL `NOT LIKE` operator
158+
-in | IN | Same as the SQL `IN` operator
159+
-not-in | NOT IN | Same as the SQL `NOT IN` operator
148160
-min | >= | Greater than or equal to
149161
-max | <= | Smaller than or equal to
150162
-st | < | Smaller than

src/Parser.php

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,11 @@ protected function parseFilter($filterParams)
549549
'max' => '<=',
550550
'lk' => 'LIKE',
551551
'not-lk' => 'NOT LIKE',
552+
'in' => 'IN',
553+
'not-in' => 'NOT IN',
552554
'not' => '!=',
553555
];
554-
556+
555557
$supportedPrefixesStr = implode('|', $supportedPostfixes);
556558
$supportedPostfixesStr = implode('|', array_keys($supportedPostfixes));
557559

@@ -576,35 +578,50 @@ protected function parseFilter($filterParams)
576578

577579
$column = $keyMatches[2];
578580

579-
$values = explode('|', $filterParamValue);
581+
if($comparator == 'IN')
582+
{
583+
$values = explode(',', $filterParamValue);
580584

581-
if(count($values) > 1)
585+
$this->query->whereIn($column, $values);
586+
}
587+
else if($comparator == 'NOT IN')
582588
{
583-
$this->query->where(function($query) use($column, $comparator, $values)
584-
{
585-
foreach($values as $value)
586-
{
587-
if($comparator == 'LIKE' || $comparator == 'NOT LIKE') $value = preg_replace('/(^\*|\*$)/', '%', $value);
589+
$values = explode(',', $filterParamValue);
588590

589-
//Link the filters with AND of there is a "not" and with OR if there's none
590-
if($comparator == '!=' || $comparator == 'NOT LIKE')
591-
{
592-
$query->where($column, $comparator, $value);
593-
}
594-
else
595-
{
596-
$query->orWhere($column, $comparator, $value);
597-
}
598-
}
599-
});
591+
$this->query->whereNotIn($column, $values);
600592
}
601593
else
602594
{
603-
$value = $values[0];
595+
$values = explode('|', $filterParamValue);
596+
597+
if(count($values) > 1)
598+
{
599+
$this->query->where(function($query) use($column, $comparator, $values)
600+
{
601+
foreach($values as $value)
602+
{
603+
if($comparator == 'LIKE' || $comparator == 'NOT LIKE') $value = preg_replace('/(^\*|\*$)/', '%', $value);
604+
605+
//Link the filters with AND of there is a "not" and with OR if there's none
606+
if($comparator == '!=' || $comparator == 'NOT LIKE')
607+
{
608+
$query->where($column, $comparator, $value);
609+
}
610+
else
611+
{
612+
$query->orWhere($column, $comparator, $value);
613+
}
614+
}
615+
});
616+
}
617+
else
618+
{
619+
$value = $values[0];
604620

605-
if($comparator == 'LIKE' || $comparator == 'NOT LIKE') $value = preg_replace('/(^\*|\*$)/', '%', $value);
621+
if($comparator == 'LIKE' || $comparator == 'NOT LIKE') $value = preg_replace('/(^\*|\*$)/', '%', $value);
606622

607-
$this->query->where($column, $comparator, $value);
623+
$this->query->where($column, $comparator, $value);
624+
}
608625
}
609626
}
610627
}

tests/ApiHandlerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function setUp()
2929
'id-max' => 6,
3030
'id-gt' => 7,
3131
'id-st' => 8,
32+
'id-in' => '1,2',
33+
'id-not-in' => '3,4',
3234
//Pagination
3335
'_limit' => 5,
3436
'_offset' => 10,
@@ -184,6 +186,12 @@ public function testGetBuilder()
184186
//assert for id-st
185187
$this->assertContains(['type' => 'Basic', 'column' => 'id', 'operator' => '<', 'value' => 8, 'boolean' => 'and'], $wheres);
186188

189+
//assert for id-in
190+
$this->assertContains(['type' => 'In', 'column' => 'id', 'values' => ['1', '2'], 'boolean' => 'and'], $wheres);
191+
192+
//assert for id-not-in
193+
$this->assertContains(['type' => 'NotIn', 'column' => 'id', 'values' => ['3', '4'], 'boolean' => 'and'], $wheres);
194+
187195
//
188196
// Limit
189197
//

0 commit comments

Comments
 (0)