Skip to content

Commit

Permalink
Add andWhere to WhereTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Kay committed Nov 28, 2014
1 parent d1b4428 commit ce6b044
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
55 changes: 46 additions & 9 deletions src/Builder/Traits/WhereTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,47 @@ public function where(...$expressions)
return $this;
}

public function andWhere(...$expressions)
{
$currentSet = $this->_getCurrentSet();
$newSet = $this->_getNewSet($expressions);

$finalSet = new PredicateSet();
$finalSet->addPredicate($currentSet);
$finalSet->addPredicate($newSet);

/**
* @var $this IStatement
* @var $where WhereClause
*/
$where = $this->getClause('WHERE');
$where->clearPredicates();
$where->addPredicate($finalSet);
$this->addClause($where);
return $this;
}

public function orWhere(...$expressions)
{
$currentSet = $this->_getCurrentSet();
$newSet = $this->_getNewSet($expressions);

$finalSet = new OrPredicateSet();
$finalSet->addPredicate($currentSet);
$finalSet->addPredicate($newSet);

/**
* @var $this IStatement
* @var $where WhereClause
*/
$where = $this->getClause('WHERE');
$where->clearPredicates();
$where->addPredicate($finalSet);
$this->addClause($where);
return $this;
}

private function _getCurrentSet()
{
/**
* @var $this IStatement
Expand All @@ -42,6 +82,11 @@ public function orWhere(...$expressions)
$currentSet->setPredicates($where->getPredicates());
}

return $currentSet;
}

private function _getNewSet(...$expressions)
{
$newPredicates = WhereClause::buildPredicates($expressions);
if(count($newPredicates) === 1)
{
Expand All @@ -52,14 +97,6 @@ public function orWhere(...$expressions)
$newSet = new PredicateSet();
$newSet->setPredicates($newPredicates);
}

$finalSet = new OrPredicateSet();
$finalSet->addPredicate($currentSet);
$finalSet->addPredicate($newSet);

$where->clearPredicates();
$where->addPredicate($finalSet);
$this->addClause($where);
return $this;
return $newSet;
}
}
8 changes: 8 additions & 0 deletions tests/Builder/Traits/WhereTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public function testCreate()
. 'OR (username = "testing" AND status = "active")',
QueryAssembler::stringify($class)
);

$class->andWhere(['username' => 'tested', 'status' => 'disabled']);
$this->assertEquals(
'WHERE ((username = "test" AND status = "pending") '
. 'OR (username = "testing" AND status = "active")) '
. 'AND (username = "tested" AND status = "disabled")',
QueryAssembler::stringify($class)
);
}

public function testException()
Expand Down

0 comments on commit ce6b044

Please sign in to comment.