Skip to content

Commit

Permalink
QueryBuilder - added lt, gt condition handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed Oct 8, 2015
1 parent d286a03 commit 86796b5
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class QueryBuilder extends \yii\base\Object
{
private $_sort = [
SORT_ASC => '_asc',
SORT_ASC => '_asc',
SORT_DESC => '_desc',
];

Expand Down Expand Up @@ -57,20 +57,22 @@ public function build($query)

return [
'queryParts' => $parts,
'index' => $query->index,
'type' => $query->type,
'options' => $options,
'index' => $query->index,
'type' => $query->type,
'options' => $options,
];
}

public function buildCondition($condition)
{
static $builders = [
'and' => 'buildAndCondition',
'and' => 'buildAndCondition',
'between' => 'buildBetweenCondition',
'eq' => 'buildEqCondition',
'in' => 'buildInCondition',
'like' => 'buildLikeCondition',
'eq' => 'buildEqCondition',
'in' => 'buildInCondition',
'like' => 'buildLikeCondition',
'gt' => 'buildGreaterThenCondition',
'lt' => 'buildLessThanCondition',
];
if (empty($condition)) {
return [];
Expand Down Expand Up @@ -110,14 +112,29 @@ private function buildHashCondition($condition)
}

private function buildLikeCondition($operator, $operands)
{
return [$operands[0] . '_like' => $operands[1]];
}

private function buildGreaterThenCondition($operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidParamException("Operator '$operator' requires three operands.");
}

return [$operands[0] . '_like' => $operands[1]];
return [$operands[0] . '_gt' => $operands[1]];
}

private function buildLessThanCondition($operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidParamException("Operator '$operator' requires three operands.");
}

return [$operands[0] . '_lt' => $operands[1]];
}


private function buildAndCondition($operator, $operands)
{
$parts = [];
Expand All @@ -140,10 +157,10 @@ private function buildBetweenCondition($operator, $operands)

private function buildInCondition($operator, $operands)
{
$key = array_shift($operands);
$key = array_shift($operands);
$value = array_shift($operands);

return [$key . '_in' => (array) $value];
return [$key . '_in' => (array)$value];
}

private function buildEqCondition($operator, $operands)
Expand Down

0 comments on commit 86796b5

Please sign in to comment.