1
1
<?php
2
+
2
3
namespace Prettus \Repository \Eloquent ;
3
4
4
5
use Closure ;
26
27
27
28
/**
28
29
* Class BaseRepository
30
+ *
29
31
* @package Prettus\Repository\Eloquent
30
- * @author Anderson Andrade <[email protected] >
32
+ * @author Anderson Andrade <[email protected] >
31
33
*/
32
34
abstract class BaseRepository implements RepositoryInterface, RepositoryCriteriaInterface
33
35
{
@@ -269,7 +271,7 @@ public function scopeQuery(\Closure $scope)
269
271
/**
270
272
* Retrieve data array for populate field select
271
273
*
272
- * @param string $column
274
+ * @param string $column
273
275
* @param string|null $key
274
276
*
275
277
* @return \Illuminate\Support\Collection|array
@@ -284,7 +286,8 @@ public function lists($column, $key = null)
284
286
/**
285
287
* Retrieve data array for populate field select
286
288
* Compatible with Laravel 5.3
287
- * @param string $column
289
+ *
290
+ * @param string $column
288
291
* @param string|null $key
289
292
*
290
293
* @return \Illuminate\Support\Collection|array
@@ -299,10 +302,11 @@ public function pluck($column, $key = null)
299
302
/**
300
303
* Sync relations
301
304
*
302
- * @param $id
303
- * @param $relation
304
- * @param $attributes
305
+ * @param $id
306
+ * @param $relation
307
+ * @param $attributes
305
308
* @param bool $detaching
309
+ *
306
310
* @return mixed
307
311
*/
308
312
public function sync ($ id , $ relation , $ attributes , $ detaching = true )
@@ -316,6 +320,7 @@ public function sync($id, $relation, $attributes, $detaching = true)
316
320
* @param $id
317
321
* @param $relation
318
322
* @param $attributes
323
+ *
319
324
* @return mixed
320
325
*/
321
326
public function syncWithoutDetaching ($ id , $ relation , $ attributes )
@@ -350,7 +355,7 @@ public function all($columns = ['*'])
350
355
/**
351
356
* Count results of repository
352
357
*
353
- * @param array $where
358
+ * @param array $where
354
359
* @param string $columns
355
360
*
356
361
* @return int
@@ -452,7 +457,7 @@ public function firstOrCreate(array $attributes = [])
452
457
/**
453
458
* Retrieve data of repository with limit applied
454
459
*
455
- * @param int $limit
460
+ * @param int $limit
456
461
* @param array $columns
457
462
*
458
463
* @return mixed
@@ -469,8 +474,8 @@ public function limit($limit, $columns = ['*'])
469
474
* Retrieve all data of repository, paginated
470
475
*
471
476
* @param null|int $limit
472
- * @param array $columns
473
- * @param string $method
477
+ * @param array $columns
478
+ * @param string $method
474
479
*
475
480
* @return mixed
476
481
*/
@@ -490,7 +495,7 @@ public function paginate($limit = null, $columns = ['*'], $method = "paginate")
490
495
* Retrieve all data of repository, simple paginated
491
496
*
492
497
* @param null|int $limit
493
- * @param array $columns
498
+ * @param array $columns
494
499
*
495
500
* @return mixed
496
501
*/
@@ -617,11 +622,11 @@ public function findWhereBetween($field, array $values, $columns = ['*'])
617
622
/**
618
623
* Save a new entity in repository
619
624
*
620
- * @throws ValidatorException
621
- *
622
625
* @param array $attributes
623
626
*
624
627
* @return mixed
628
+ * @throws ValidatorException
629
+ *
625
630
*/
626
631
public function create (array $ attributes )
627
632
{
@@ -654,12 +659,12 @@ public function create(array $attributes)
654
659
/**
655
660
* Update a entity in repository by id
656
661
*
657
- * @throws ValidatorException
658
- *
659
662
* @param array $attributes
660
663
* @param $id
661
664
*
662
665
* @return mixed
666
+ * @throws ValidatorException
667
+ *
663
668
*/
664
669
public function update (array $ attributes , $ id )
665
670
{
@@ -705,12 +710,12 @@ public function update(array $attributes, $id)
705
710
/**
706
711
* Update or Create an entity in repository
707
712
*
708
- * @throws ValidatorException
709
- *
710
713
* @param array $attributes
711
714
* @param array $values
712
715
*
713
716
* @return mixed
717
+ * @throws ValidatorException
718
+ *
714
719
*/
715
720
public function updateOrCreate (array $ attributes , array $ values = [])
716
721
{
@@ -824,7 +829,8 @@ public function with($relations)
824
829
/**
825
830
* Add subselect queries to count the relations.
826
831
*
827
- * @param mixed $relations
832
+ * @param mixed $relations
833
+ *
828
834
* @return $this
829
835
*/
830
836
public function withCount ($ relations )
@@ -836,7 +842,7 @@ public function withCount($relations)
836
842
/**
837
843
* Load relation with closure
838
844
*
839
- * @param string $relation
845
+ * @param string $relation
840
846
* @param closure $closure
841
847
*
842
848
* @return $this
@@ -865,7 +871,7 @@ public function hidden(array $fields)
865
871
/**
866
872
* Set the "orderBy" value of the query.
867
873
*
868
- * @param mixed $column
874
+ * @param mixed $column
869
875
* @param string $direction
870
876
*
871
877
* @return $this
@@ -1058,14 +1064,90 @@ protected function applyCriteria()
1058
1064
* Applies the given where conditions to the model.
1059
1065
*
1060
1066
* @param array $where
1067
+ *
1061
1068
* @return void
1062
1069
*/
1063
1070
protected function applyConditions (array $ where )
1064
1071
{
1065
1072
foreach ($ where as $ field => $ value ) {
1066
1073
if (is_array ($ value )) {
1067
1074
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
+ }
1069
1151
} else {
1070
1152
$ this ->model = $ this ->model ->where ($ field , '= ' , $ value );
1071
1153
}
@@ -1104,7 +1186,7 @@ public function parserResult($result)
1104
1186
1105
1187
return $ model ;
1106
1188
});
1107
- } elseif ($ result instanceof Presentable) {
1189
+ } else if ($ result instanceof Presentable) {
1108
1190
$ result = $ result ->setPresenter ($ this ->presenter );
1109
1191
}
1110
1192
0 commit comments