Completed
Push — master ( 7e212d...676b8c )
by wen
12:32
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
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\Display\Extensions;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Sco\Admin\Contracts\Display\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
23
        return $this;
24
    }
25
26
    /**
27
     *
28
     * @return \Illuminate\Support\Collection
29
     */
30
    public function getActive()
31
    {
32
        return $this->filter(function (FilterInterface $filter) {
33
            return $filter->isActive();
34
        });
35
    }
36
37
    /**
38
     * {@inheritdoc}
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 getDisplayValues()
48
    {
49
        return $this->mapWithKeys(function (FilterInterface $filter) {
50
            return [
51
                $filter->getName() => $filter->getDisplayValue(),
52
            ];
53
        });
54
    }
55
}
56