Skip to content

Commit

Permalink
Merge pull request #2 from ernesto-gomez/master
Browse files Browse the repository at this point in the history
fix code to use NullValueFilter
  • Loading branch information
ernesto-gomez authored Apr 13, 2021
2 parents d645666 + 2958b28 commit 52db042
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^7.4",
"doctrine/dbal": "^2.0",
"pccomponentes/criteria": "^0.1"
"pccomponentes/criteria": "^0.1.5"
},
"autoload": {
"psr-4": {
Expand Down
20 changes: 15 additions & 5 deletions src/Criteria/DBALCriteriaVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Doctrine\DBAL\Query\QueryBuilder;
use Pccomponentes\Criteria\Domain\Criteria\AndFilter;
use Pccomponentes\Criteria\Domain\Criteria\Criteria;
use Pccomponentes\Criteria\Domain\Criteria\Filter;
use Pccomponentes\Criteria\Domain\Criteria\FilterInterface;
use Pccomponentes\Criteria\Domain\Criteria\NullValueFilter;
use Pccomponentes\Criteria\Domain\Criteria\FilterOperator;
use Pccomponentes\Criteria\Domain\Criteria\FilterVisitorInterface;
use Pccomponentes\Criteria\Domain\Criteria\OrFilter;
Expand Down Expand Up @@ -58,7 +58,7 @@ public function visitOr(OrFilter $filter): string
return '( '. $this->buildExpression($filter->left()) .' OR '. $this->buildExpression($filter->right()) .' )';
}

public function visitFilter(Filter $filter): string
public function visitFilter(FilterInterface $filter): string
{
$this->countParams++;

Expand All @@ -68,6 +68,12 @@ public function visitFilter(Filter $filter): string
$this->mapType($filter),
);

if ($filter instanceof NullValueFilter) {
return $this->mapFieldValue($filter->field()->value())
. ' '
. $filter->operator()->value();
}

return $this->mapFieldValue($filter->field()->value())
. ' '
. $this->mapOperator($filter)
Expand All @@ -82,7 +88,7 @@ private function buildExpression(FilterInterface $filter)
return $filter->accept($this);
}

private function mapType(Filter $filter): ?int
private function mapType(FilterInterface $filter): ?int
{
if (\in_array($filter->operator()->value(), [FilterOperator::IN, FilterOperator::NOT_IN])) {
return \Doctrine\DBAL\Connection::PARAM_STR_ARRAY;
Expand All @@ -91,7 +97,7 @@ private function mapType(Filter $filter): ?int
return null;
}

private function mapOperator(Filter $filter): string
private function mapOperator(FilterInterface $filter): string
{
if (FilterOperator::CONTAINS === $filter->operator()->value()) {
return 'LIKE';
Expand All @@ -104,8 +110,12 @@ private function mapOperator(Filter $filter): string
return $filter->operator()->value();
}

private function mapParameter(Filter $filter)
private function mapParameter(FilterInterface $filter)
{
if (\in_array($filter->operator()->value(), [FilterOperator::IS_NULL, FilterOperator::IS_NOT_NULL])) {
return;
}

if (FilterOperator::CONTAINS === $filter->operator()->value()) {
return '%' . $filter->value()->value() . '%';
}
Expand Down

0 comments on commit 52db042

Please sign in to comment.