Skip to content

Commit fe83725

Browse files
feat(QueryFilter): add query filter for finding objects whose field values are within an array or string
1 parent f46a205 commit fe83725

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

docs/QUERIES_FILTERS_AND_SORTING.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ value array contains a given value for array fields.
5252
- Name: `contains`
5353
- Example: `https://pfsense.example.com/api/v2/examples?fieldname__contains=example`
5454

55+
### In (in)
56+
57+
Search for objects whose field value is within a given array of values (when the filter value is an array), or search
58+
for objects whose field value is a substring of a given string (when the filter value is a string).
59+
- Name: `in`
60+
- Examples:
61+
- `https://pfsense.example.com/api/v2/examples?fieldname__in=example`
62+
- `https://pfsense.example.com/api/v2/examples?fieldname__in[]=example1&fieldname__in[]=example2`
63+
5564
### Less Than (lt)
5665

5766
Search for objects whose field value is less than a given integer.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace RESTAPI\QueryFilters;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\QueryFilter;
8+
9+
/**
10+
* Defines a query filter that checks if the given field value is within a set of values.
11+
*/
12+
class InQueryFilter extends QueryFilter {
13+
public string $name = 'in';
14+
15+
/**
16+
* Checks if the field's value is one of the values in the given filter value array. If the field value is an
17+
* array, this filter will check if the field's value matches an item in the filter value array exactly. Otherwise,
18+
* this filter will check if the field's value is a substring of the filter value string.
19+
* @param mixed $field_value The value of the field being used to filter.
20+
* @param mixed $filter_value The value of the filter criteria to evaluate against.
21+
* @return bool true if the field_value contains the filter_value, false otherwise.
22+
*/
23+
public function evaluate(mixed $field_value, mixed $filter_value): bool {
24+
# When filter value is an array and field value is not, check if the field value is in the filter value array
25+
if (is_array($filter_value) and !is_array($field_value)) {
26+
return in_array($field_value, $filter_value);
27+
}
28+
29+
return str_contains(strval($filter_value), strval($field_value));
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace RESTAPI\Tests;
4+
5+
use RESTAPI\Core\TestCase;
6+
use RESTAPI\QueryFilters\InQueryFilter;
7+
8+
class APIQueryFiltersInQueryFilterTestCase extends TestCase {
9+
/**
10+
* Checks that the 'evaluate' method correctly returns true when the field value is found within the filter value,
11+
* and false when it is not.
12+
*/
13+
public function test_evaluate(): void {
14+
$filter = new InQueryFilter();
15+
16+
# Check with string values
17+
$this->assert_is_true($filter->evaluate('test', 'test'));
18+
$this->assert_is_false($filter->evaluate('test', 'notfound'));
19+
20+
# Check with array filter value and non-array field value
21+
$this->assert_is_true($filter->evaluate('apple', ['apple', 'banana']));
22+
$this->assert_is_false($filter->evaluate('cherry', ['apple', 'banana']));
23+
}
24+
}

0 commit comments

Comments
 (0)