Skip to content

Commit

Permalink
#10 - add whereIn and whereNotIn support
Browse files Browse the repository at this point in the history
  • Loading branch information
selahattinunlu committed Nov 11, 2016
1 parent d4b348a commit ea54323
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
24 changes: 19 additions & 5 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,33 @@ private function addWhereToQuery($where)
{
extract($where);

// For array values (whereIn, whereNotIn)
if (isset($values)) {
$value = $values;
}
if (!isset($operator)) {
$operator = '';
}

if ($this->isExcludedParameter($key)) {
return;
}

if ($this->hasCustomFilter($key)) {
return $this->applyCustomFilter($key, $operator, $value);
return $this->applyCustomFilter($key, $operator, $value, $type);
}

if (! $this->hasTableColumn($key)) {
throw new UnknownColumnException("Unknown column '{$key}'");
}

$this->query->where($key, $operator, $value);
if ($type == 'In') {
$this->query->whereIn($key, $value);
} else if ($type == 'NotIn') {
$this->query->whereNotIn($key, $value);
} else {
$this->query->where($key, $operator, $value);
}
}

private function addOrderByToQuery($order)
Expand All @@ -262,11 +276,11 @@ private function addOrderByToQuery($order)
$this->query->orderBy($column, $direction);
}

private function applyCustomFilter($key, $operator, $value)
private function applyCustomFilter($key, $operator, $value, $type = 'Basic')
{
$callback = [$this, $this->customFilterName($key)];

$this->query = call_user_func($callback, $this->query, $value, $operator);
$this->query = call_user_func($callback, $this->query, $value, $operator, $type);
}

private function isRelationColumn($column)
Expand Down Expand Up @@ -330,4 +344,4 @@ private function customFilterName($key)
{
return 'filterBy' . studly_case($key);
}
}
}
48 changes: 46 additions & 2 deletions src/UriParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class UriParser

protected $pattern = '/!=|=|<=|<|>=|>/';

protected $arrayQueryPattern = '/(.*)\[\]/';

protected $constantParameters = [
'order_by',
'group_by',
Expand Down Expand Up @@ -79,26 +81,68 @@ private function setQueryParameters($queryUri)
}

private function appendQueryParameter($parameter)
{
// whereIn expression
preg_match($this->arrayQueryPattern, $parameter, $arrayMatches);
if (count($arrayMatches) > 0) {
$this->appendQueryParameterAsWhereIn($parameter, $arrayMatches[1]);
return;
}

// basic where expression
$this->appendQueryParameterAsBasicWhere($parameter);
}

private function appendQueryParameterAsBasicWhere($parameter)
{
preg_match($this->pattern, $parameter, $matches);

$operator = $matches[0];

list($key, $value) = explode($operator, $parameter);

if (! $this->isConstantParameter($key) &&
$this->isLikeQuery($value)) {
if (! $this->isConstantParameter($key) && $this->isLikeQuery($value)) {
$operator = 'like';
$value = str_replace('*', '%', $value);
}

$this->queryParameters[] = [
'type' => 'Basic',
'key' => $key,
'operator' => $operator,
'value' => $value
];
}

private function appendQueryParameterAsWhereIn($parameter, $key)
{
if (str_contains($parameter, '!=')) {
$type = 'NotIn';
$seperator = '!=';
} else {
$type = 'In';
$seperator = '=';
}

$index = null;
foreach ($this->queryParameters as $_index => $queryParameter) {
if ($queryParameter['type'] == $type && $queryParameter['key'] == $key) {
$index = $_index;
break;
}
}

if ($index !== null) {
$this->queryParameters[$index]['values'][] = explode($seperator, $parameter)[1];
} else {
$this->queryParameters[] = [
'type' => $type,
'key' => $key,
'values' => [explode($seperator, $parameter)[1]]
];
}
}

public function hasQueryUri()
{
return ($this->queryUri);
Expand Down

0 comments on commit ea54323

Please sign in to comment.