This repository has been archived by the owner on Dec 28, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from Lctrs/feature/in
filter: add In and NotIn
- Loading branch information
Showing
4 changed files
with
166 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lctrs\DBALSpecification\Filter; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Query\QueryBuilder; | ||
use Lctrs\DBALSpecification\Filter; | ||
use Lctrs\DBALSpecification\Operand\Operand; | ||
|
||
final class In implements Filter | ||
{ | ||
/** @var Operand */ | ||
private $x; | ||
/** @var list<string> */ | ||
private $y; | ||
|
||
/** | ||
* @param list<string> $y | ||
*/ | ||
public function __construct(Operand $x, array $y) | ||
{ | ||
$this->x = $x; | ||
$this->y = $y; | ||
} | ||
|
||
public function getFilter(QueryBuilder $queryBuilder): ?string | ||
{ | ||
return $queryBuilder->expr()->in( | ||
$this->x->transform($queryBuilder), | ||
$queryBuilder->createNamedParameter($this->y, Connection::PARAM_STR_ARRAY) | ||
); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lctrs\DBALSpecification\Filter; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Query\QueryBuilder; | ||
use Lctrs\DBALSpecification\Filter; | ||
use Lctrs\DBALSpecification\Operand\Operand; | ||
|
||
final class NotIn implements Filter | ||
{ | ||
/** @var Operand */ | ||
private $x; | ||
/** @var list<string> */ | ||
private $y; | ||
|
||
/** | ||
* @param list<string> $y | ||
*/ | ||
public function __construct(Operand $x, array $y) | ||
{ | ||
$this->x = $x; | ||
$this->y = $y; | ||
} | ||
|
||
public function getFilter(QueryBuilder $queryBuilder): ?string | ||
{ | ||
return $queryBuilder->expr()->notIn( | ||
$this->x->transform($queryBuilder), | ||
$queryBuilder->createNamedParameter($this->y, Connection::PARAM_STR_ARRAY) | ||
); | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lctrs\DBALSpecification\Test\Unit\Filter; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Query\Expression\ExpressionBuilder; | ||
use Doctrine\DBAL\Query\QueryBuilder; | ||
use Lctrs\DBALSpecification\Filter\In; | ||
use Lctrs\DBALSpecification\Operand\Field; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class InTest extends TestCase | ||
{ | ||
/** @var QueryBuilder */ | ||
private $queryBuilder; | ||
|
||
protected function setUp(): void | ||
{ | ||
$connection = $this->createMock(Connection::class); | ||
$exprBuilder = new ExpressionBuilder($connection); | ||
|
||
$connection | ||
->expects(self::any()) | ||
->method('getExpressionBuilder') | ||
->willReturn($exprBuilder); | ||
$connection | ||
->expects(self::any()) | ||
->method('createQueryBuilder') | ||
->willReturn(new QueryBuilder($connection)); | ||
|
||
$this->queryBuilder = new QueryBuilder($connection); | ||
} | ||
|
||
public function testItCallsIn(): void | ||
{ | ||
self::assertSame( | ||
'foo IN (:dcValue1)', | ||
(new In(new Field('foo'), ['foo', 'bar']))->getFilter($this->queryBuilder) | ||
); | ||
|
||
self::assertSame( | ||
$this->queryBuilder->getParameter('dcValue1'), | ||
['foo', 'bar'] | ||
); | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lctrs\DBALSpecification\Test\Unit\Filter; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Query\Expression\ExpressionBuilder; | ||
use Doctrine\DBAL\Query\QueryBuilder; | ||
use Lctrs\DBALSpecification\Filter\NotIn; | ||
use Lctrs\DBALSpecification\Operand\Field; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class NotInTest extends TestCase | ||
{ | ||
/** @var QueryBuilder */ | ||
private $queryBuilder; | ||
|
||
protected function setUp(): void | ||
{ | ||
$connection = $this->createMock(Connection::class); | ||
$exprBuilder = new ExpressionBuilder($connection); | ||
|
||
$connection | ||
->expects(self::any()) | ||
->method('getExpressionBuilder') | ||
->willReturn($exprBuilder); | ||
$connection | ||
->expects(self::any()) | ||
->method('createQueryBuilder') | ||
->willReturn(new QueryBuilder($connection)); | ||
|
||
$this->queryBuilder = new QueryBuilder($connection); | ||
} | ||
|
||
public function testItCallsIn(): void | ||
{ | ||
self::assertSame( | ||
'foo NOT IN (:dcValue1)', | ||
(new NotIn(new Field('foo'), ['foo', 'bar']))->getFilter($this->queryBuilder) | ||
); | ||
|
||
self::assertSame( | ||
$this->queryBuilder->getParameter('dcValue1'), | ||
['foo', 'bar'] | ||
); | ||
} | ||
} |