From ea5432334c0a31465777480860e1c6016670d5ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Selahattin=20=C3=9Cnl=C3=BC?= Date: Fri, 11 Nov 2016 18:17:10 +0300 Subject: [PATCH] #10 - add whereIn and whereNotIn support --- src/QueryBuilder.php | 24 +++++++++++++++++----- src/UriParser.php | 48 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index b629a34..f1330f1 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -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) @@ -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) @@ -330,4 +344,4 @@ private function customFilterName($key) { return 'filterBy' . studly_case($key); } -} \ No newline at end of file +} diff --git a/src/UriParser.php b/src/UriParser.php index 9f4d288..2846305 100644 --- a/src/UriParser.php +++ b/src/UriParser.php @@ -10,6 +10,8 @@ class UriParser protected $pattern = '/!=|=|<=|<|>=|>/'; + protected $arrayQueryPattern = '/(.*)\[\]/'; + protected $constantParameters = [ 'order_by', 'group_by', @@ -79,6 +81,19 @@ 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); @@ -86,19 +101,48 @@ private function appendQueryParameter($parameter) 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);