Skip to content

Commit f6f6d23

Browse files
authored
Merge pull request #5 from mgf145/laravel5
refactor mktime method
2 parents 0556d2e + ba73a4e commit f6f6d23

File tree

1 file changed

+58
-21
lines changed

1 file changed

+58
-21
lines changed

src/Filterable.php

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ public function scopeFilter($query, array $filter = null){
5252
}
5353
}
5454

55-
private function mktime(){
56-
55+
private function mktime()
56+
{
5757
if(config('filterable.date_type') === 'gregorian') {
5858
return 'mktime';
59-
}else {
60-
if(!function_exists('jmktime')) {
61-
throw new \Exception('jmktime functions are unavailable');
62-
}
63-
64-
return 'jmktime';
6559
}
60+
if(!function_exists('jmktime')) {
61+
throw new \Exception('jmktime functions are unavailable');
62+
}
63+
return 'jmktime';
6664
}
6765

6866
private function convertDate($date, $last = false){
@@ -130,25 +128,64 @@ private function clauseScope($query, $value){
130128
}
131129
}
132130

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+
{
137133
if(is_array($value['between'])) {
138134
$this->clause = 'whereBetween';
139135
$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);
148149
}
149-
$this->value = $betweenValue;
150150
$this->clause($query);
151151
}
152152
}
153153

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+
}
154191
}

0 commit comments

Comments
 (0)