Completed
Push — master ( 3c1bd9...d781ee )
by wen
12:17
created

Filters::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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