Last active
August 25, 2021 14:55
-
-
Save michitheonlyone/73f3bf192596c0d2383c59760bbda56e to your computer and use it in GitHub Desktop.
PHP Doctrine DBAL Variable Filter Value Class
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
<?php declare(strict_types=1); | |
namespace Acme\Common\Application; | |
final class Filter | |
{ | |
private string $column; | |
private int $option; | |
private string $value; | |
const OPTION_WORD = 0; | |
const OPTION_MIN = 1; | |
const OPTION_MAX = 2; | |
public function __construct(string $column, int $option, string $value) | |
{ | |
$this->column = $column; | |
$this->option = $option; | |
$this->value = $value; | |
} | |
public function getColumn(): string | |
{ | |
return $this->column; | |
} | |
public function getOption(): int | |
{ | |
return $this->option; | |
} | |
public function getValue(): string | |
{ | |
return $this->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
<?php declare(strict_types=1); | |
namespace Acme\Common\Infrastructure; | |
use Doctrine\DBAL\Query\QueryBuilder; | |
use Acme\Common\Application\Filter; | |
final class DbalFilter | |
{ | |
public static function applyFilter(QueryBuilder $queryBuilder, Filter $filter): ?QueryBuilder | |
{ | |
switch ($filter->getOption()) { | |
case Filter::OPTION_MIN: | |
return $queryBuilder->andWhere( | |
$queryBuilder->expr()->gte( | |
$filter->getColumn(), | |
$queryBuilder->createNamedParameter($filter->getValue()) | |
) | |
); | |
case Filter::OPTION_MAX: | |
return $queryBuilder->andWhere( | |
$queryBuilder->expr()->lte( | |
$filter->getColumn(), | |
$queryBuilder->createNamedParameter($filter->getValue()) | |
) | |
); | |
default: | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment