Completed
Pull Request — master (#59)
by
unknown
02:08
created

Filter::scope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Spatie\QueryBuilder\Filters\FiltersExact;
7
use Spatie\QueryBuilder\Filters\FiltersPartial;
8
use Spatie\QueryBuilder\Filters\FiltersScope;
9
10
class Filter
11
{
12
    /** @var string */
13
    protected $filterClass;
14
15
    /** @var string */
16
    protected $property;
17
18
    public function __construct(string $property, string $filterClass)
19
    {
20
        $this->property = $property;
21
        $this->filterClass = $filterClass;
22
    }
23
24
    public function filter(Builder $builder, $value)
25
    {
26
        $filterClass = new $this->filterClass;
27
28
        ($filterClass)($builder, $value, $this->property);
29
    }
30
31
    public static function exact(string $property) : self
32
    {
33
        return new static($property, FiltersExact::class);
34
    }
35
36
    public static function partial(string $property) : self
37
    {
38
        return new static($property, FiltersPartial::class);
39
    }
40
41
    public static function scope(string $property) : self
42
    {
43
        return new static($property, FiltersScope::class);
44
    }
45
46
    public static function custom(string $property, string $filterClass) : self
47
    {
48
        return new static($property, $filterClass);
49
    }
50
51
    public function getProperty(): string
52
    {
53
        return $this->property;
54
    }
55
56
    public function isForProperty(string $property): bool
57
    {
58
        return $this->property === $property;
59
    }
60
}
61