Skip to content

Commit a7224ec

Browse files
authored
Added Numeric filter (#6)
* Added Numeric filter * Added feature coverage with JSON with Tag filter
1 parent fe13c0b commit a7224ec

File tree

7 files changed

+513
-13
lines changed

7 files changed

+513
-13
lines changed

src/Enum/Condition.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ enum Condition: string
1111
case equal = '==';
1212
case notEqual = '!=';
1313
case greaterThan = '>';
14+
case greaterThanOrEqual = '>=';
1415
case lowerThan = '<';
16+
case lowerThanOrEqual = '<=';
1517
case pattern = '%';
18+
case between = 'between';
1619
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Vladvildanov\PredisVl\Query\Filter;
4+
5+
use Vladvildanov\PredisVl\Enum\Condition;
6+
7+
abstract class AbstractFilter implements FilterInterface
8+
{
9+
/**
10+
* Mappings according to Redis Query Syntax
11+
*
12+
* @link https://redis.io/docs/interact/search-and-query/advanced-concepts/query_syntax/
13+
* @var array
14+
*/
15+
protected array $conditionMappings = [
16+
'==' => '',
17+
'!=' => '-'
18+
];
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function __construct(string $fieldName, Condition $condition, mixed $value)
24+
{
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
abstract public function toExpression(): string;
31+
}

src/Query/Filter/NumericFilter.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Vladvildanov\PredisVl\Query\Filter;
4+
5+
use Exception;
6+
use Vladvildanov\PredisVl\Enum\Condition;
7+
8+
class NumericFilter extends AbstractFilter
9+
{
10+
/**
11+
* Creates numeric filter based on condition.
12+
* Value can be provided as integer (single value) and in case of between condition as array of two integers.
13+
*/
14+
public function __construct(
15+
private readonly string $fieldName,
16+
private readonly Condition $condition,
17+
private readonly mixed $value
18+
) {
19+
parent::__construct($fieldName, $condition, $value);
20+
}
21+
22+
/**
23+
* @inheritDoc
24+
* @throws Exception
25+
*/
26+
public function toExpression(): string
27+
{
28+
if ($this->condition === Condition::equal || $this->condition === Condition::notEqual) {
29+
$condition = $this->conditionMappings[$this->condition->value];
30+
return "$condition@$this->fieldName:[$this->value $this->value]";
31+
}
32+
33+
if ($this->condition === Condition::greaterThan) {
34+
return "@$this->fieldName:[($this->value +inf]";
35+
}
36+
37+
if ($this->condition === Condition::greaterThanOrEqual) {
38+
return "@$this->fieldName:[$this->value +inf]";
39+
}
40+
41+
if ($this->condition === Condition::lowerThan) {
42+
return "@$this->fieldName:[-inf ($this->value]";
43+
}
44+
45+
if ($this->condition === Condition::lowerThanOrEqual) {
46+
return "@$this->fieldName:[-inf $this->value]";
47+
}
48+
49+
if ($this->condition === Condition::between && !is_array($this->value)) {
50+
throw new Exception('Between condition requires int[] as $ value');
51+
}
52+
53+
return "@$this->fieldName:[{$this->value[0]} {$this->value[1]}]";
54+
}
55+
}

src/Query/Filter/TagFilter.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,8 @@
66
use Vladvildanov\PredisVl\Enum\Condition;
77
use Vladvildanov\PredisVl\Enum\Logical;
88

9-
class TagFilter implements FilterInterface
9+
class TagFilter extends AbstractFilter
1010
{
11-
/**
12-
* Mappings according to Redis Query Syntax
13-
*
14-
* @link https://redis.io/docs/interact/search-and-query/advanced-concepts/query_syntax/
15-
* @var array
16-
*/
17-
private array $conditionMappings = [
18-
'==' => '',
19-
'!=' => '-'
20-
];
21-
2211
/**
2312
* Creates tag filter based on condition.
2413
* Value can be provided as a string (single tag) or as an array (multiple tags).
@@ -35,6 +24,7 @@ public function __construct(
3524
'tags' => 'array',
3625
])] private $value
3726
) {
27+
parent::__construct($this->fieldName, $this->condition, $this->value);
3828
}
3929

4030
/**

0 commit comments

Comments
 (0)