Completed
Push — master ( 08cc44...3c1bd9 )
by wen
12:21
created

Filters::getValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\View\Extensions;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Sco\Admin\Contracts\View\Filters\FilterInterface;
7
8
class Filters extends Extension
9
{
10
    public function add($value)
11
    {
12
        if (!($value instanceof FilterInterface)) {
13
            throw new \InvalidArgumentException(
14
                sprintf(
15
                    'filter must be %s',
16
                    FilterInterface::class
17
                )
18
            );
19
        }
20
21
        $this->push($value);
22
        return $this;
23
    }
24
25
    public function getActive()
26
    {
27
        return $this->filter(function (FilterInterface $filter) {
28
            return $filter->isActive();
29
        });
30
    }
31
32
    public function apply(Builder $query)
33
    {
34
        $this->getActive()->each(function (FilterInterface $filter) use ($query) {
35
            $filter->apply($query);
36
        });
37
    }
38
39
    public function getValues()
40
    {
41
        return $this->mapWithKeys(function (FilterInterface $filter) {
42
            return [
43
                $filter->getName() => $filter->getValue(),
44
            ];
45
        });
46
    }
47
}
48