Skip to content

Commit 9746e49

Browse files
authored
add some conditions (#758)
* add some conditions * add docs
1 parent 2bd5680 commit 9746e49

File tree

2 files changed

+121
-24
lines changed

2 files changed

+121
-24
lines changed

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,23 @@ $posts = $this->repository->findWhere([
388388
//Default Condition =
389389
'state_id'=>'10',
390390
'country_id'=>'15',
391+
391392
//Custom Condition
392-
['columnName','>','10']
393+
['columnName1','>','10'],
394+
395+
//DATE, DAY, MONTH, YEAR
396+
['columnName2','DATE','2021-07-02'], //whereDate
397+
['columnName3','DATE >=','2021-07-02'], //whereDate with operator
398+
399+
['columnName4','IN',['value1','value2']], //whereIn
400+
['columnName5','NOTIN',['value1','value2']], //whereNotIn
401+
['columnName6','EXIST',''], //whereExists
402+
403+
//HAS, HASMORPH, DOESNTHAVE, DOESNTHAVEMORPH
404+
['columnName7','HAS',function($query){}], //whereHas
405+
406+
//BETWEEN, BETWEENCOLUMNS, NOTBETWEEN, NOTBETWEENCOLUMNS
407+
['columnName8','BETWEEN',[10, 100]], //whereBetween
393408
]);
394409
```
395410

@@ -855,7 +870,7 @@ Result will have something like this
855870
]
856871
```
857872

858-
WhereIn filter
873+
WhereIn filter
859874

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

src/Prettus/Repository/Eloquent/BaseRepository.php

+104-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Prettus\Repository\Eloquent;
34

45
use Closure;
@@ -26,8 +27,9 @@
2627

2728
/**
2829
* Class BaseRepository
30+
*
2931
* @package Prettus\Repository\Eloquent
30-
* @author Anderson Andrade <[email protected]>
32+
* @author Anderson Andrade <[email protected]>
3133
*/
3234
abstract class BaseRepository implements RepositoryInterface, RepositoryCriteriaInterface
3335
{
@@ -269,7 +271,7 @@ public function scopeQuery(\Closure $scope)
269271
/**
270272
* Retrieve data array for populate field select
271273
*
272-
* @param string $column
274+
* @param string $column
273275
* @param string|null $key
274276
*
275277
* @return \Illuminate\Support\Collection|array
@@ -284,7 +286,8 @@ public function lists($column, $key = null)
284286
/**
285287
* Retrieve data array for populate field select
286288
* Compatible with Laravel 5.3
287-
* @param string $column
289+
*
290+
* @param string $column
288291
* @param string|null $key
289292
*
290293
* @return \Illuminate\Support\Collection|array
@@ -299,10 +302,11 @@ public function pluck($column, $key = null)
299302
/**
300303
* Sync relations
301304
*
302-
* @param $id
303-
* @param $relation
304-
* @param $attributes
305+
* @param $id
306+
* @param $relation
307+
* @param $attributes
305308
* @param bool $detaching
309+
*
306310
* @return mixed
307311
*/
308312
public function sync($id, $relation, $attributes, $detaching = true)
@@ -316,6 +320,7 @@ public function sync($id, $relation, $attributes, $detaching = true)
316320
* @param $id
317321
* @param $relation
318322
* @param $attributes
323+
*
319324
* @return mixed
320325
*/
321326
public function syncWithoutDetaching($id, $relation, $attributes)
@@ -350,7 +355,7 @@ public function all($columns = ['*'])
350355
/**
351356
* Count results of repository
352357
*
353-
* @param array $where
358+
* @param array $where
354359
* @param string $columns
355360
*
356361
* @return int
@@ -452,7 +457,7 @@ public function firstOrCreate(array $attributes = [])
452457
/**
453458
* Retrieve data of repository with limit applied
454459
*
455-
* @param int $limit
460+
* @param int $limit
456461
* @param array $columns
457462
*
458463
* @return mixed
@@ -469,8 +474,8 @@ public function limit($limit, $columns = ['*'])
469474
* Retrieve all data of repository, paginated
470475
*
471476
* @param null|int $limit
472-
* @param array $columns
473-
* @param string $method
477+
* @param array $columns
478+
* @param string $method
474479
*
475480
* @return mixed
476481
*/
@@ -490,7 +495,7 @@ public function paginate($limit = null, $columns = ['*'], $method = "paginate")
490495
* Retrieve all data of repository, simple paginated
491496
*
492497
* @param null|int $limit
493-
* @param array $columns
498+
* @param array $columns
494499
*
495500
* @return mixed
496501
*/
@@ -617,11 +622,11 @@ public function findWhereBetween($field, array $values, $columns = ['*'])
617622
/**
618623
* Save a new entity in repository
619624
*
620-
* @throws ValidatorException
621-
*
622625
* @param array $attributes
623626
*
624627
* @return mixed
628+
* @throws ValidatorException
629+
*
625630
*/
626631
public function create(array $attributes)
627632
{
@@ -654,12 +659,12 @@ public function create(array $attributes)
654659
/**
655660
* Update a entity in repository by id
656661
*
657-
* @throws ValidatorException
658-
*
659662
* @param array $attributes
660663
* @param $id
661664
*
662665
* @return mixed
666+
* @throws ValidatorException
667+
*
663668
*/
664669
public function update(array $attributes, $id)
665670
{
@@ -705,12 +710,12 @@ public function update(array $attributes, $id)
705710
/**
706711
* Update or Create an entity in repository
707712
*
708-
* @throws ValidatorException
709-
*
710713
* @param array $attributes
711714
* @param array $values
712715
*
713716
* @return mixed
717+
* @throws ValidatorException
718+
*
714719
*/
715720
public function updateOrCreate(array $attributes, array $values = [])
716721
{
@@ -824,7 +829,8 @@ public function with($relations)
824829
/**
825830
* Add subselect queries to count the relations.
826831
*
827-
* @param mixed $relations
832+
* @param mixed $relations
833+
*
828834
* @return $this
829835
*/
830836
public function withCount($relations)
@@ -836,7 +842,7 @@ public function withCount($relations)
836842
/**
837843
* Load relation with closure
838844
*
839-
* @param string $relation
845+
* @param string $relation
840846
* @param closure $closure
841847
*
842848
* @return $this
@@ -865,7 +871,7 @@ public function hidden(array $fields)
865871
/**
866872
* Set the "orderBy" value of the query.
867873
*
868-
* @param mixed $column
874+
* @param mixed $column
869875
* @param string $direction
870876
*
871877
* @return $this
@@ -1058,14 +1064,90 @@ protected function applyCriteria()
10581064
* Applies the given where conditions to the model.
10591065
*
10601066
* @param array $where
1067+
*
10611068
* @return void
10621069
*/
10631070
protected function applyConditions(array $where)
10641071
{
10651072
foreach ($where as $field => $value) {
10661073
if (is_array($value)) {
10671074
list($field, $condition, $val) = $value;
1068-
$this->model = $this->model->where($field, $condition, $val);
1075+
//smooth input
1076+
$condition = preg_replace('/\s\s+/', ' ', trim($condition));
1077+
1078+
//split to get operator, syntax: "DATE >", "DATE =", "DAY <"
1079+
$operator = explode(' ', $condition);
1080+
if (count($operator) > 1) {
1081+
$condition = $operator[0];
1082+
$operator = $operator[1];
1083+
} else $operator = null;
1084+
switch (strtoupper($condition)) {
1085+
case 'IN':
1086+
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
1087+
$this->model = $this->model->whereIn($field, $val);
1088+
break;
1089+
case 'NOTIN':
1090+
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
1091+
$this->model = $this->model->whereNotIn($field, $val);
1092+
break;
1093+
case 'DATE':
1094+
if (!$operator) $operator = '=';
1095+
$this->model = $this->model->whereDate($field, $operator, $val);
1096+
break;
1097+
case 'DAY':
1098+
if (!$operator) $operator = '=';
1099+
$this->model = $this->model->whereDay($field, $operator, $val);
1100+
break;
1101+
case 'MONTH':
1102+
if (!$operator) $operator = '=';
1103+
$this->model = $this->model->whereMonth($field, $operator, $val);
1104+
break;
1105+
case 'YEAR':
1106+
if (!$operator) $operator = '=';
1107+
$this->model = $this->model->whereYear($field, $operator, $val);
1108+
break;
1109+
case 'EXISTS':
1110+
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function");
1111+
$this->model = $this->model->whereExists($val);
1112+
break;
1113+
case 'HAS':
1114+
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function");
1115+
$this->model = $this->model->whereHas($field, $val);
1116+
break;
1117+
case 'HASMORPH':
1118+
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function");
1119+
$this->model = $this->model->whereHasMorph($field, $val);
1120+
break;
1121+
case 'DOESNTHAVE':
1122+
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function");
1123+
$this->model = $this->model->whereDoesntHave($field, $val);
1124+
break;
1125+
case 'DOESNTHAVEMORPH':
1126+
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function");
1127+
$this->model = $this->model->whereDoesntHaveMorph($field, $val);
1128+
break;
1129+
case 'BETWEEN':
1130+
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
1131+
$this->model = $this->model->whereBetween($field, $val);
1132+
break;
1133+
case 'BETWEENCOLUMNS':
1134+
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
1135+
$this->model = $this->model->whereBetweenColumns($field, $val);
1136+
break;
1137+
case 'NOTBETWEEN':
1138+
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
1139+
$this->model = $this->model->whereNotBetween($field, $val);
1140+
break;
1141+
case 'NOTBETWEENCOLUMNS':
1142+
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array");
1143+
$this->model = $this->model->whereNotBetweenColumns($field, $val);
1144+
break;
1145+
case 'RAW':
1146+
$this->model = $this->model->whereRaw($val);
1147+
break;
1148+
default:
1149+
$this->model = $this->model->where($field, $condition, $val);
1150+
}
10691151
} else {
10701152
$this->model = $this->model->where($field, '=', $value);
10711153
}
@@ -1104,7 +1186,7 @@ public function parserResult($result)
11041186

11051187
return $model;
11061188
});
1107-
} elseif ($result instanceof Presentable) {
1189+
} else if ($result instanceof Presentable) {
11081190
$result = $result->setPresenter($this->presenter);
11091191
}
11101192

0 commit comments

Comments
 (0)