DateFilter::isRange()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Yaro\Jarboe\Table\Filters;
4
5
class DateFilter extends AbstractFilter
6
{
7
    private $range = false;
8
9 2
    public function range(bool $enable = true)
10
    {
11 2
        $this->range = $enable;
12
13 2
        return $this;
14
    }
15
16 3
    public function isRange(): bool
17
    {
18 3
        return $this->range;
19
    }
20
21 1
    public function render()
22
    {
23 1
        $view = 'date';
24 1
        if ($this->isRange()) {
25
            $view .= '_range';
26
        }
27
28 1
        return view('jarboe::crud.filters.'. $view, [
29 1
            'filter' => $this,
30
        ]);
31
    }
32
33 2
    public function apply($model)
34
    {
35 2
        $value = $this->value();
36 2
        if (is_null($value)) {
37 1
            return;
38
        }
39
40 1
        if ($this->isRange()) {
41
            $model->when($value['from'] ?? null, function ($query, $value) {
42 1
                return $query->whereDate($this->field()->name(), '>=', $value);
43 1
            });
44
            $model->when($value['to'] ?? null, function ($query, $value) {
45 1
                return $query->whereDate($this->field()->name(), '<=', $value);
46 1
            });
47 1
            return;
48
        }
49
50 1
        $model->whereDate(
51 1
            $this->field()->name(),
52 1
            $this->sign,
53
            $value
54
        );
55 1
    }
56
}
57