Skip to content

Commit

Permalink
condition like
Browse files Browse the repository at this point in the history
  • Loading branch information
mvkasatkin committed Sep 18, 2017
1 parent a9c1584 commit 5b96ad4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
24 changes: 18 additions & 6 deletions src/condition/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Condition
const LESS_OR_EQUALS = 5;
const GREATER_OR_EQUALS = 6;
const BETWEEN = 7;
const IN = 8;
const LIKE = 8;
const IN = 9;

protected $conditions = [];
protected $sort = [];
Expand Down Expand Up @@ -121,6 +122,20 @@ public function addBetweenCondition(string $field, $valueFrom, $valueTo)
return $this;
}

/**
* @param string $field
* @param $value
* @param bool $left
* @param bool $right
*
* @return $this
*/
public function addLikeCondition(string $field, $value, $left = true, $right = true)
{
$this->conditions[] = [self::LIKE, $field, $value, $left, $right];
return $this;
}

/**
* @param string $field
* @param array $array
Expand All @@ -134,15 +149,12 @@ public function addInCondition(string $field, array $array, bool $isNumeric = fa
}

/**
* @param $sortField
* @param $sortDir
* @param string $sortField
* @param int $sortDir SORT FLAG (SORT_ASC, SORT_DESC)
* @return $this
*/
public function addSort(string $sortField, int $sortDir)
{
if(!is_numeric($sortDir)) {
$sortDir = strtolower($sortDir) == 'asc' ? SORT_ASC : SORT_DESC;
}
$this->sort[$sortField] = $sortDir;
return $this;
}
Expand Down
6 changes: 6 additions & 0 deletions src/condition/ConditionDbParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public function parse(QueryBuilder $queryBuilder, Condition $condition = null)
$queryBuilder->andWhere("{$cond[0]} BETWEEN " . $queryBuilder->createNamedParameter($cond[1])
. " AND " . $queryBuilder->createNamedParameter($cond[2]));
break;
case Condition::LIKE:
$value = $cond[2] ? '%' : '';
$value .= $cond[1];
$value .= $cond[3] ? '%' : '';
$queryBuilder->andWhere("{$cond[0]} LIKE " . $queryBuilder->createNamedParameter($value));
break;
case Condition::IN:
if($cond[1]) {
$isNumeric = $cond[2];
Expand Down
17 changes: 14 additions & 3 deletions tests/condition/ConditionDbParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
class ConditionDbParserTest extends \PHPUnit\Framework\TestCase
{

public function testParseEmpty()
{
/** @var \Doctrine\DBAL\Query\QueryBuilder $qb */
$qb = $this->createMock(\Doctrine\DBAL\Query\QueryBuilder::class);
$parser = new \WebComplete\core\condition\ConditionDbParser();
$parser->parse($qb);
$this->assertTrue(true);
}

public function testParse()
{
$condition = new Condition();
Expand All @@ -15,6 +24,7 @@ public function testParse()
$condition->addLessOrEqualsCondition('e', 5);
$condition->addGreaterOrEqualsCondition('f', 6);
$condition->addBetweenCondition('g', 7, 8);
$condition->addLikeCondition('l', 'asd');
$condition->addInCondition('h', [9, 10]);
$condition->addInCondition('i', [9, 10], true);
$condition->addInCondition('j', []);
Expand All @@ -30,7 +40,7 @@ public function testParse()
$qb->select('*')->from('tmp');
$parser->parse($qb, $condition);

$sql = 'SELECT * FROM tmp WHERE (a = :dcValue1) AND (b <> :dcValue2) AND (c < :dcValue3) AND (d > :dcValue4) AND (e <= :dcValue5) AND (f >= :dcValue6) AND (g BETWEEN :dcValue7 AND :dcValue8) AND (h IN (:dcValue9,:dcValue10)) AND (i IN (9,10)) AND (1 = 2) ORDER BY f2 desc LIMIT 15 OFFSET 5';
$sql = 'SELECT * FROM tmp WHERE (a = :dcValue1) AND (b <> :dcValue2) AND (c < :dcValue3) AND (d > :dcValue4) AND (e <= :dcValue5) AND (f >= :dcValue6) AND (g BETWEEN :dcValue7 AND :dcValue8) AND (l LIKE :dcValue9) AND (h IN (:dcValue10,:dcValue11)) AND (i IN (9,10)) AND (1 = 2) ORDER BY f2 desc LIMIT 15 OFFSET 5';
$this->assertEquals($sql, $qb->getSQL());
$this->assertEquals([
'dcValue1' => 1,
Expand All @@ -41,8 +51,9 @@ public function testParse()
'dcValue6' => 6,
'dcValue7' => 7,
'dcValue8' => 8,
'dcValue9' => 9,
'dcValue10' => 10,
'dcValue9' => '%asd%',
'dcValue10' => 9,
'dcValue11' => 10,

], $qb->getParameters());
}
Expand Down
4 changes: 4 additions & 0 deletions tests/condition/ConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function testConditions()
$condition->addGreaterOrEqualsCondition('f', 6);
$condition->addBetweenCondition('g', 7, 8);
$condition->addInCondition('h', [9, 10]);
$condition->addLikeCondition('i', 'asd', true, false);
$condition->addSort('f1', SORT_ASC);
$condition->addSort('f2', SORT_DESC);
$condition->offset(5);
Expand All @@ -33,6 +34,7 @@ public function testConditions()
[Condition::GREATER_OR_EQUALS, 'f', 6],
[Condition::BETWEEN, 'g', 7, 8],
[Condition::IN, 'h', [9, 10], false],
[Condition::LIKE, 'i', 'asd', true, false],
], $condition->getConditions());
}

Expand All @@ -47,6 +49,7 @@ public function testConditionsArgs()
[Condition::GREATER_OR_EQUALS, 'f', 6],
[Condition::BETWEEN, 'g', 7, 8],
[Condition::IN, 'h', [9, 10]],
[Condition::LIKE, 'i', 'asd', true, false],
], 'f1', SORT_DESC, 10, 5);

$this->assertEquals(10, $condition->getOffset());
Expand All @@ -61,6 +64,7 @@ public function testConditionsArgs()
[Condition::GREATER_OR_EQUALS, 'f', 6],
[Condition::BETWEEN, 'g', 7, 8],
[Condition::IN, 'h', [9, 10]],
[Condition::LIKE, 'i', 'asd', true, false],
], $condition->getConditions());
}

Expand Down

0 comments on commit 5b96ad4

Please sign in to comment.