@@ -52,17 +52,15 @@ public function scopeFilter($query, array $filter = null){
52
52
}
53
53
}
54
54
55
- private function mktime (){
56
-
55
+ private function mktime ()
56
+ {
57
57
if (config ('filterable.date_type ' ) === 'gregorian ' ) {
58
58
return 'mktime ' ;
59
- }else {
60
- if (!function_exists ('jmktime ' )) {
61
- throw new \Exception ('jmktime functions are unavailable ' );
62
- }
63
-
64
- return 'jmktime ' ;
65
59
}
60
+ if (!function_exists ('jmktime ' )) {
61
+ throw new \Exception ('jmktime functions are unavailable ' );
62
+ }
63
+ return 'jmktime ' ;
66
64
}
67
65
68
66
private function convertDate ($ date , $ last = false ){
@@ -130,25 +128,64 @@ private function clauseScope($query, $value){
130
128
}
131
129
}
132
130
133
- private function clauseBetween ($ query , $ key , $ value ){
134
-
135
- $ dates = array_unique (array_merge (config ('filterable.date_fields ' ), $ this ->dates ));
136
- $ betweenValue = [];
131
+ private function clauseBetween ($ query , $ key , $ value )
132
+ {
137
133
if (is_array ($ value ['between ' ])) {
138
134
$ this ->clause = 'whereBetween ' ;
139
135
$ this ->column = $ key ;
140
- foreach ($ value ['between ' ] as $ vBetween ) {
141
- if (request ()->has ($ vBetween ) && !is_null (request ($ vBetween ))) {
142
- if (in_array ($ key , $ dates )) {
143
- $ betweenValue [] = $ this ->convertDate (request ()->get ($ vBetween ), (last ($ value ['between ' ]) == $ vBetween ) ? true : false );
144
- }else {
145
- $ betweenValue [] = request ()->get ($ vBetween );
146
- }
147
- }
136
+ if ((request ()->has ($ value ['between ' ][0 ]) && !is_null (request ($ value ['between ' ][0 ]))) &&
137
+ (request ()->has ($ value ['between ' ][1 ]) && !is_null (request ($ value ['between ' ][1 ])))) {
138
+ $ this ->setPropertiesByType ('both ' , $ key , $ value );
139
+ } elseif (
140
+ (request ()->has ($ value ['between ' ][0 ]) && !is_null (request ($ value ['between ' ][0 ]))) &&
141
+ (!request ()->has ($ value ['between ' ][1 ]) || is_null (request ($ value ['between ' ][1 ])))
142
+ ) {
143
+ $ this ->setPropertiesByType ('first ' , $ key , $ value );
144
+ } elseif (
145
+ (!request ()->has ($ value ['between ' ][0 ]) || is_null (request ($ value ['between ' ][0 ]))) &&
146
+ (request ()->has ($ value ['between ' ][1 ]) && !is_null (request ($ value ['between ' ][1 ])))
147
+ ) {
148
+ $ this ->setPropertiesByType ('second ' , $ key , $ value );
148
149
}
149
- $ this ->value = $ betweenValue ;
150
150
$ this ->clause ($ query );
151
151
}
152
152
}
153
153
154
+ private function setPropertiesByType ($ type , $ key , $ value )
155
+ {
156
+ $ dates = array_unique (array_merge (config ('filterable.date_fields ' ), $ this ->dates ));
157
+ switch ($ type ) {
158
+ case 'both ' :
159
+ if (in_array ($ key , $ dates )) {
160
+ $ this ->value = [
161
+ $ this ->convertDate (request ($ value ['between ' ][0 ])),
162
+ $ this ->convertDate (request ($ value ['between ' ][1 ]),true )
163
+ ];
164
+ } else {
165
+ $ this ->value = [
166
+ request ($ value ['between ' ][0 ]),
167
+ request ($ value ['between ' ][1 ])
168
+ ];
169
+ }
170
+ break ;
171
+ case 'first ' :
172
+ $ this ->clause = 'where ' ;
173
+ $ this ->operator = '>= ' ;
174
+ if (in_array ($ key , $ dates )) {
175
+ $ this ->value = $ this ->convertDate (request ($ value ['between ' ][0 ]));
176
+ } else {
177
+ $ this ->value = request ($ value ['between ' ][0 ]);
178
+ }
179
+ break ;
180
+ case 'second ' :
181
+ $ this ->clause = 'where ' ;
182
+ $ this ->operator = '<= ' ;
183
+ if (in_array ($ key , $ dates )) {
184
+ $ this ->value = $ this ->convertDate (request ($ value ['between ' ][1 ]),true );
185
+ } else {
186
+ $ this ->value = request ($ value ['between ' ][1 ]);
187
+ }
188
+ break ;
189
+ }
190
+ }
154
191
}
0 commit comments