Skip to content

Commit

Permalink
add some conditions (#758)
Browse files Browse the repository at this point in the history
* add some conditions

* add docs
  • Loading branch information
nghiennet89 authored Aug 21, 2021
1 parent 2bd5680 commit 9746e49
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 24 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,23 @@ $posts = $this->repository->findWhere([
//Default Condition =
'state_id'=>'10',
'country_id'=>'15',

//Custom Condition
['columnName','>','10']
['columnName1','>','10'],

//DATE, DAY, MONTH, YEAR
['columnName2','DATE','2021-07-02'], //whereDate
['columnName3','DATE >=','2021-07-02'], //whereDate with operator

['columnName4','IN',['value1','value2']], //whereIn
['columnName5','NOTIN',['value1','value2']], //whereNotIn
['columnName6','EXIST',''], //whereExists

//HAS, HASMORPH, DOESNTHAVE, DOESNTHAVEMORPH
['columnName7','HAS',function($query){}], //whereHas

//BETWEEN, BETWEENCOLUMNS, NOTBETWEEN, NOTBETWEENCOLUMNS
['columnName8','BETWEEN',[10, 100]], //whereBetween
]);
```

Expand Down Expand Up @@ -855,7 +870,7 @@ Result will have something like this
]
```

WhereIn filter
WhereIn filter

`http://prettus.local/product?search=price:300,500&searchFields=price:in`

Expand Down
126 changes: 104 additions & 22 deletions src/Prettus/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Prettus\Repository\Eloquent;

use Closure;
Expand Down Expand Up @@ -26,8 +27,9 @@

/**
* Class BaseRepository
*
* @package Prettus\Repository\Eloquent
* @author Anderson Andrade <[email protected]>
* @author Anderson Andrade <[email protected]>
*/
abstract class BaseRepository implements RepositoryInterface, RepositoryCriteriaInterface
{
Expand Down Expand Up @@ -269,7 +271,7 @@ public function scopeQuery(\Closure $scope)
/**
* Retrieve data array for populate field select
*
* @param string $column
* @param string $column
* @param string|null $key
*
* @return \Illuminate\Support\Collection|array
Expand All @@ -284,7 +286,8 @@ public function lists($column, $key = null)
/**
* Retrieve data array for populate field select
* Compatible with Laravel 5.3
* @param string $column
*
* @param string $column
* @param string|null $key
*
* @return \Illuminate\Support\Collection|array
Expand All @@ -299,10 +302,11 @@ public function pluck($column, $key = null)
/**
* Sync relations
*
* @param $id
* @param $relation
* @param $attributes
* @param $id
* @param $relation
* @param $attributes
* @param bool $detaching
*
* @return mixed
*/
public function sync($id, $relation, $attributes, $detaching = true)
Expand All @@ -316,6 +320,7 @@ public function sync($id, $relation, $attributes, $detaching = true)
* @param $id
* @param $relation
* @param $attributes
*
* @return mixed
*/
public function syncWithoutDetaching($id, $relation, $attributes)
Expand Down Expand Up @@ -350,7 +355,7 @@ public function all($columns = ['*'])
/**
* Count results of repository
*
* @param array $where
* @param array $where
* @param string $columns
*
* @return int
Expand Down Expand Up @@ -452,7 +457,7 @@ public function firstOrCreate(array $attributes = [])
/**
* Retrieve data of repository with limit applied
*
* @param int $limit
* @param int $limit
* @param array $columns
*
* @return mixed
Expand All @@ -469,8 +474,8 @@ public function limit($limit, $columns = ['*'])
* Retrieve all data of repository, paginated
*
* @param null|int $limit
* @param array $columns
* @param string $method
* @param array $columns
* @param string $method
*
* @return mixed
*/
Expand All @@ -490,7 +495,7 @@ public function paginate($limit = null, $columns = ['*'], $method = "paginate")
* Retrieve all data of repository, simple paginated
*
* @param null|int $limit
* @param array $columns
* @param array $columns
*
* @return mixed
*/
Expand Down Expand Up @@ -617,11 +622,11 @@ public function findWhereBetween($field, array $values, $columns = ['*'])
/**
* Save a new entity in repository
*
* @throws ValidatorException
*
* @param array $attributes
*
* @return mixed
* @throws ValidatorException
*
*/
public function create(array $attributes)
{
Expand Down Expand Up @@ -654,12 +659,12 @@ public function create(array $attributes)
/**
* Update a entity in repository by id
*
* @throws ValidatorException
*
* @param array $attributes
* @param $id
*
* @return mixed
* @throws ValidatorException
*
*/
public function update(array $attributes, $id)
{
Expand Down Expand Up @@ -705,12 +710,12 @@ public function update(array $attributes, $id)
/**
* Update or Create an entity in repository
*
* @throws ValidatorException
*
* @param array $attributes
* @param array $values
*
* @return mixed
* @throws ValidatorException
*
*/
public function updateOrCreate(array $attributes, array $values = [])
{
Expand Down Expand Up @@ -824,7 +829,8 @@ public function with($relations)
/**
* Add subselect queries to count the relations.
*
* @param mixed $relations
* @param mixed $relations
*
* @return $this
*/
public function withCount($relations)
Expand All @@ -836,7 +842,7 @@ public function withCount($relations)
/**
* Load relation with closure
*
* @param string $relation
* @param string $relation
* @param closure $closure
*
* @return $this
Expand Down Expand Up @@ -865,7 +871,7 @@ public function hidden(array $fields)
/**
* Set the "orderBy" value of the query.
*
* @param mixed $column
* @param mixed $column
* @param string $direction
*
* @return $this
Expand Down Expand Up @@ -1058,14 +1064,90 @@ protected function applyCriteria()
* Applies the given where conditions to the model.
*
* @param array $where
*
* @return void
*/
protected function applyConditions(array $where)
{
foreach ($where as $field => $value) {
if (is_array($value)) {
list($field, $condition, $val) = $value;
$this->model = $this->model->where($field, $condition, $val);
//smooth input
$condition = preg_replace('/\s\s+/', ' ', trim($condition));

//split to get operator, syntax: "DATE >", "DATE =", "DAY <"
$operator = explode(' ', $condition);
if (count($operator) > 1) {
$condition = $operator[0];
$operator = $operator[1];
} else $operator = null;
switch (strtoupper($condition)) {
case 'IN':
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
$this->model = $this->model->whereIn($field, $val);
break;
case 'NOTIN':
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
$this->model = $this->model->whereNotIn($field, $val);
break;
case 'DATE':
if (!$operator) $operator = '=';
$this->model = $this->model->whereDate($field, $operator, $val);
break;
case 'DAY':
if (!$operator) $operator = '=';
$this->model = $this->model->whereDay($field, $operator, $val);
break;
case 'MONTH':
if (!$operator) $operator = '=';
$this->model = $this->model->whereMonth($field, $operator, $val);
break;
case 'YEAR':
if (!$operator) $operator = '=';
$this->model = $this->model->whereYear($field, $operator, $val);
break;
case 'EXISTS':
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function");
$this->model = $this->model->whereExists($val);
break;
case 'HAS':
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function");
$this->model = $this->model->whereHas($field, $val);
break;
case 'HASMORPH':
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function");
$this->model = $this->model->whereHasMorph($field, $val);
break;
case 'DOESNTHAVE':
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function");
$this->model = $this->model->whereDoesntHave($field, $val);
break;
case 'DOESNTHAVEMORPH':
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function");
$this->model = $this->model->whereDoesntHaveMorph($field, $val);
break;
case 'BETWEEN':
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
$this->model = $this->model->whereBetween($field, $val);
break;
case 'BETWEENCOLUMNS':
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
$this->model = $this->model->whereBetweenColumns($field, $val);
break;
case 'NOTBETWEEN':
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
$this->model = $this->model->whereNotBetween($field, $val);
break;
case 'NOTBETWEENCOLUMNS':
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
$this->model = $this->model->whereNotBetweenColumns($field, $val);
break;
case 'RAW':
$this->model = $this->model->whereRaw($val);
break;
default:
$this->model = $this->model->where($field, $condition, $val);
}
} else {
$this->model = $this->model->where($field, '=', $value);
}
Expand Down Expand Up @@ -1104,7 +1186,7 @@ public function parserResult($result)

return $model;
});
} elseif ($result instanceof Presentable) {
} else if ($result instanceof Presentable) {
$result = $result->setPresenter($this->presenter);
}

Expand Down

0 comments on commit 9746e49

Please sign in to comment.