Skip to content

Commit

Permalink
Merge pull request #5 from mgf145/laravel5
Browse files Browse the repository at this point in the history
refactor mktime method
  • Loading branch information
sedehi authored Oct 3, 2018
2 parents 0556d2e + ba73a4e commit f6f6d23
Showing 1 changed file with 58 additions and 21 deletions.
79 changes: 58 additions & 21 deletions src/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ public function scopeFilter($query, array $filter = null){
}
}

private function mktime(){

private function mktime()
{
if(config('filterable.date_type') === 'gregorian') {
return 'mktime';
}else {
if(!function_exists('jmktime')) {
throw new \Exception('jmktime functions are unavailable');
}

return 'jmktime';
}
if(!function_exists('jmktime')) {
throw new \Exception('jmktime functions are unavailable');
}
return 'jmktime';
}

private function convertDate($date, $last = false){
Expand Down Expand Up @@ -130,25 +128,64 @@ private function clauseScope($query, $value){
}
}

private function clauseBetween($query, $key, $value){

$dates = array_unique(array_merge(config('filterable.date_fields'), $this->dates));
$betweenValue = [];
private function clauseBetween($query, $key, $value)
{
if(is_array($value['between'])) {
$this->clause = 'whereBetween';
$this->column = $key;
foreach($value['between'] as $vBetween) {
if(request()->has($vBetween) && !is_null(request($vBetween))) {
if(in_array($key, $dates)) {
$betweenValue[] = $this->convertDate(request()->get($vBetween), (last($value['between']) == $vBetween) ? true : false);
}else {
$betweenValue[] = request()->get($vBetween);
}
}
if ((request()->has($value['between'][0]) && !is_null(request($value['between'][0]))) &&
(request()->has($value['between'][1]) && !is_null(request($value['between'][1])))) {
$this->setPropertiesByType('both', $key, $value);
} elseif (
(request()->has($value['between'][0]) && !is_null(request($value['between'][0]))) &&
(!request()->has($value['between'][1]) || is_null(request($value['between'][1])))
) {
$this->setPropertiesByType('first', $key, $value);
} elseif (
(!request()->has($value['between'][0]) || is_null(request($value['between'][0]))) &&
(request()->has($value['between'][1]) && !is_null(request($value['between'][1])))
) {
$this->setPropertiesByType('second', $key, $value);
}
$this->value = $betweenValue;
$this->clause($query);
}
}

private function setPropertiesByType($type, $key, $value)
{
$dates = array_unique(array_merge(config('filterable.date_fields'), $this->dates));
switch ($type) {
case 'both':
if(in_array($key, $dates)) {
$this->value = [
$this->convertDate(request($value['between'][0])),
$this->convertDate(request($value['between'][1]),true)
];
} else {
$this->value = [
request($value['between'][0]),
request($value['between'][1])
];
}
break;
case 'first':
$this->clause = 'where';
$this->operator = '>=';
if(in_array($key, $dates)) {
$this->value = $this->convertDate(request($value['between'][0]));
} else {
$this->value = request($value['between'][0]);
}
break;
case 'second':
$this->clause = 'where';
$this->operator = '<=';
if(in_array($key, $dates)) {
$this->value = $this->convertDate(request($value['between'][1]),true);
} else {
$this->value = request($value['between'][1]);
}
break;
}
}
}

0 comments on commit f6f6d23

Please sign in to comment.