Skip to content

Commit

Permalink
Added btw operator that translates to BETWEEN
Browse files Browse the repository at this point in the history
* Closes #9
  • Loading branch information
hschimpf committed Aug 1, 2024
1 parent e64d189 commit 0fb48e9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
41 changes: 37 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ The available operators are:
- `gt`: Translates to a `field_name > "value"` filter.
- `gte`: Translates to a `field_name >= "value"` filter.
- `in`: Translates to a `field_name IN ("value1", "value2", ...)` filter.
- `btw`: Translates to a `field_name BETWEEN "value1" AND "value2"` filter.

Operators are also grouped by field type:

- `string`: Translates to the operators `eq`, `ne` and `has`.
- `numeric`: Translates to the operators `eq`, `ne`, `lt`, `lte`, `gt`, `gte` and `in`.
- `numeric`: Translates to the operators `eq`, `ne`, `lt`, `lte`, `gt`, `gte`, `in`, and `btw`.
- `boolean`: Translates to the operators `eq` and `ne`.
- `date`: Translates to the operators `eq`, `ne`, `lt`, `lte`, `gt`, and `gte`.
- `date`: Translates to the operators `eq`, `ne`, `lt`, `lte`, `gt`, `gte`, and `btw`.

#### Example implementation

Expand All @@ -70,7 +71,7 @@ class CountryFilters extends \HDSSolutions\Laravel\API\ResourceFilters {
protected array $allowed_columns = [
'name' => 'string',
'code' => 'string',
'something' => [ 'gt', 'lt' ],
'size_km2' => [ 'gt', 'lt', 'btw' ],
];

}
Expand All @@ -93,7 +94,7 @@ class CountryFilters extends \HDSSolutions\Laravel\API\ResourceFilters {
protected array $allowed_columns = [
'name' => 'string',
'code' => 'string',
'something' => [ 'gt', 'lt' ],
'size_km2' => [ 'gt', 'lt', 'btw' ],
'regions_count' => 'number',
];

Expand All @@ -119,6 +120,37 @@ class CountryFilters extends \HDSSolutions\Laravel\API\ResourceFilters {
{
"id": 123,
"name": "Country name",
"size_km2": 125000,
...
},
{ ... },
{ ... },
{ ... },
...
],
"links": {
...
}
"meta": {
...
}
}
```
- Filtering by country size:
```http request
GET https://localhost/api/countries?size_km2[btw]=100000,500000
Accept: application/json
```
Example response:
```json5
{
"data": [
{
"id": 123,
"name": "Country name",
"size_km2": 125000,
...
},
{ ... },
Expand Down Expand Up @@ -148,6 +180,7 @@ class CountryFilters extends \HDSSolutions\Laravel\API\ResourceFilters {
{
"id": 123,
"name": "Country name",
"size_km2": 125000,
...
},
{ ... },
Expand Down
26 changes: 23 additions & 3 deletions src/ResourceFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ abstract class ResourceFilters {
'ne' => '!=',
'has' => 'like',
'in' => 'in',
'btw' => 'between',
];

/**
Expand All @@ -35,9 +36,9 @@ abstract class ResourceFilters {
*/
private const TYPES = [
'string' => [ 'eq', 'ne', 'has' ],
'numeric' => [ 'eq', 'ne', 'lt', 'lte', 'gt', 'gte', 'in' ],
'numeric' => [ 'eq', 'ne', 'lt', 'lte', 'gt', 'gte', 'in', 'btw' ],
'boolean' => [ 'eq', 'ne' ],
'date' => [ 'eq', 'ne', 'lt', 'lte', 'gt', 'gte' ],
'date' => [ 'eq', 'ne', 'lt', 'lte', 'gt', 'gte', 'btw' ],
];

/**
Expand Down Expand Up @@ -123,13 +124,21 @@ private function addQueryFilter(Builder $query, string $column, string $operator
$value,
));

// capture special case for WHERE IN
// special case for WHERE IN
} elseif ($operator === 'in') {
$query->whereIn(
column: $this->column_mappings[ $column ] ?? $column,
values: $this->parseValue($operator, $value),
);

// special case for BETWEEN
} elseif ($operator === 'btw') {
$query->whereBetween(
column: $this->column_mappings[ $column ] ?? $column,
values: $this->parseValue($operator, $value),
);

// fallback to default filtering
} else {
$query->where(
column: $this->column_mappings[ $column ] ?? $column,
Expand All @@ -152,6 +161,17 @@ private function parseValue(string $operator, $value) {
return explode(',', $value);
}

if ($operator === 'btw') {
if (count($value = explode(',', $value)) !== 2) {
throw new RuntimeException(
message: 'Invalid value count for "btw" filter',
code: Response::HTTP_BAD_REQUEST,
);
}

return $value;
}

return $value;
}

Expand Down

0 comments on commit 0fb48e9

Please sign in to comment.