Skip to content

Commit

Permalink
add whereOr condition
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedhafezqo committed May 10, 2018
1 parent f3ec287 commit 39d514d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/Builders/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class QueryBuilder implements SearchInRangeContract, SearchContract
*/
protected $whereNotIn = [];

/**
* The query whereOr clauses.
*
* @var array
*/
protected $whereOr = [];

/**
* The query in range clauses.
*
Expand Down Expand Up @@ -160,6 +167,18 @@ public function whereTerm($attribute, $value)
return $this;
}

/**
* add Where Or to the main filter
* @param $attribute
* @param $value
* @return $this
*/
public function whereOr($terms)
{
$this->whereOr = $terms;
return $this;
}

/**
* add exist constrains to the main filter
* @param $attribute
Expand Down Expand Up @@ -198,6 +217,7 @@ public function resetBuilder()
$this->notInRange = [];
$this->exist = [];
$this->whereTerms = [];
$this->whereOr = [];
$this->match = [];
$this->query = new BoolQuery();
$this->filter = new BoolQuery();
Expand Down Expand Up @@ -241,6 +261,13 @@ public function prepareQuery()
$this->prepareWhereTermsCondition($term);
}

// add Terms Or to main query
$boolOr = new BoolQuery();
foreach ($this->whereOr as $attribute => $value) {
$this->prepareWhereOrCondition($attribute, $value, $boolOr);
}
$this->filter->addMust($boolOr);

// add exists constrains to the query
foreach ($this->exist as $exist) {
$this->prepareExistCondition($exist);
Expand Down
9 changes: 8 additions & 1 deletion tests/Builders/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,20 @@ public function test_notInRange_it_return_obj()
$this->assertInstanceOf(QueryBuilder::class, $repoObj);
}

public function test_wereTerm_it_return_obj()
public function test_whereTerm_it_return_obj()
{
$whereTerm = $this->queryBuilderObj->whereTerm('item', 'value');

$this->assertInstanceOf(QueryBuilder::class, $whereTerm);
}

public function test_whereOr_it_return_obj()
{
$whereOr = $this->queryBuilderObj->whereOr('terms');

$this->assertInstanceOf(QueryBuilder::class, $whereOr);
}

public function test_exist_it_return_obj()
{
$exist = $this->queryBuilderObj->exist('item');
Expand Down

0 comments on commit 39d514d

Please sign in to comment.