-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Aggregate filteer * Added missing unit test coverage
- Loading branch information
1 parent
b06e415
commit 73713b7
Showing
11 changed files
with
629 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace Vladvildanov\PredisVl\Query\Filter; | ||
|
||
use Vladvildanov\PredisVl\Enum\Logical; | ||
|
||
class AggregateFilter extends AbstractFilter implements AggregateFilterInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private array $filters = []; | ||
|
||
/** | ||
* Mapping for logical operators according to query syntax. | ||
* | ||
* @var array | ||
* @link https://redis.io/docs/interact/search-and-query/query/combined/ | ||
*/ | ||
private array $conjunctionMapping = [ | ||
'&&' => ' ', | ||
'||' => ' | ', | ||
]; | ||
|
||
/** | ||
* Creates an aggregated filter based on the set of given filters. | ||
* | ||
* If the same logical operator should be applied to all filters, you could pass them here in constructor. | ||
* If you need a combination of logical operators use and() or() methods to build suitable aggregate filter. | ||
* | ||
* @param FilterInterface $filters | ||
* @param Logical $conjunction | ||
*/ | ||
public function __construct($filters = [], Logical $conjunction = Logical::and) | ||
{ | ||
if (!empty($filters)) { | ||
$this->addFilters($filters, $conjunction); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function and(FilterInterface ...$filter): AggregateFilterInterface | ||
{ | ||
$this->addFilters(func_get_args(), Logical::and); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function or(FilterInterface ...$filter): AggregateFilterInterface | ||
{ | ||
$this->addFilters(func_get_args(), Logical::or); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function toExpression(): string | ||
{ | ||
return trim(implode('', $this->filters), ' |'); | ||
} | ||
|
||
/** | ||
* Add filters with given conjunction into array. | ||
* | ||
* @param FilterInterface[] $filters | ||
* @param Logical $conjunction | ||
* @return void | ||
*/ | ||
private function addFilters(array $filters, Logical $conjunction): void | ||
{ | ||
if (count($filters) === 1) { | ||
$this->filters[] = $this->conjunctionMapping[$conjunction->value]; | ||
$this->filters[] = $filters[0]->toExpression(); | ||
} else { | ||
if (!empty($this->filters)) { | ||
$this->filters[] = ' '; | ||
} | ||
|
||
foreach ($filters as $i => $filter) { | ||
$this->filters[] = $filter->toExpression(); | ||
|
||
if ($i !== count($filters) - 1) { | ||
$this->filters[] = $this->conjunctionMapping[$conjunction->value]; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Vladvildanov\PredisVl\Query\Filter; | ||
|
||
interface AggregateFilterInterface extends FilterInterface | ||
{ | ||
/** | ||
* Creates an aggregated filter with intersection (AND). | ||
* | ||
* @param FilterInterface ...$filter | ||
* @return AggregateFilterInterface | ||
*/ | ||
public function and(FilterInterface...$filter): AggregateFilterInterface; | ||
|
||
/** | ||
* Creates an aggregated filter with union (OR). | ||
* | ||
* @param FilterInterface ...$filter | ||
* @return AggregateFilterInterface | ||
*/ | ||
public function or(FilterInterface...$filter): AggregateFilterInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.