Skip to content

Commit 0299ece

Browse files
authored
Added Text filter (#7)
* Added Text filter * Updated docs * Added missing word
1 parent a7224ec commit 0299ece

File tree

6 files changed

+444
-7
lines changed

6 files changed

+444
-7
lines changed

src/Query/Filter/AbstractFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract class AbstractFilter implements FilterInterface
2020
/**
2121
* @inheritDoc
2222
*/
23-
public function __construct(string $fieldName, Condition $condition, mixed $value)
23+
public function __construct(protected string $fieldName, protected Condition $condition, protected mixed $value)
2424
{
2525
}
2626

src/Query/Filter/NumericFilter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class NumericFilter extends AbstractFilter
1212
* Value can be provided as integer (single value) and in case of between condition as array of two integers.
1313
*/
1414
public function __construct(
15-
private readonly string $fieldName,
16-
private readonly Condition $condition,
17-
private readonly mixed $value
15+
protected string $fieldName,
16+
protected Condition $condition,
17+
protected mixed $value
1818
) {
1919
parent::__construct($fieldName, $condition, $value);
2020
}

src/Query/Filter/TagFilter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class TagFilter extends AbstractFilter
1717
* @param string|array $value
1818
*/
1919
public function __construct(
20-
private readonly string $fieldName,
21-
private readonly Condition $condition,
20+
protected string $fieldName,
21+
protected Condition $condition,
2222
#[ArrayShape([
2323
'conjunction' => Logical::class,
2424
'tags' => 'array',
25-
])] private $value
25+
])] protected mixed $value
2626
) {
2727
parent::__construct($this->fieldName, $this->condition, $this->value);
2828
}

src/Query/Filter/TextFilter.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Vladvildanov\PredisVl\Query\Filter;
4+
5+
use Vladvildanov\PredisVl\Enum\Condition;
6+
7+
class TextFilter extends AbstractFilter
8+
{
9+
/**
10+
* Creates text filter based on condition.
11+
* Value should be provided as a string, it's possible to provide multiple values with separator
12+
* to modify the behaviour.
13+
*
14+
* Example: "foo|bar" - matching values that contains foo OR bar
15+
* "foo bar" - matching values that contains foo AND bar
16+
* "%foobaz%" - fuzzy search
17+
*
18+
* Condition can be set to "pattern" to perform:index, suffix or infix search.
19+
*
20+
* @param string $fieldName
21+
* @param Condition $condition
22+
* @param string $value
23+
*/
24+
public function __construct(string $fieldName, Condition $condition, $value)
25+
{
26+
parent::__construct($fieldName, $condition, $value);
27+
}
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function toExpression(): string
33+
{
34+
if (empty($this->value)) {
35+
return '*';
36+
}
37+
38+
if ($this->condition === Condition::equal || $this->condition === Condition::notEqual) {
39+
$condition = $this->conditionMappings[$this->condition->value];
40+
41+
return "$condition@$this->fieldName:($this->value)";
42+
}
43+
44+
return "@$this->fieldName:(w'$this->value')";
45+
}
46+
}

0 commit comments

Comments
 (0)