Passed
Push — master ( 70c8fa...dac2c3 )
by noitran
09:31
created

EloquentProcessor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 34
dl 0
loc 105
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getComparisonMethods() 0 3 1
A setBuilder() 0 5 1
A __construct() 0 3 1
A getMethodMap() 0 3 1
A process() 0 10 2
A getBuilder() 0 3 1
1
<?php
2
3
namespace Noitran\RQL\Processors\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Noitran\RQL\Contracts\Expression\ExprInterface;
8
use Noitran\RQL\Contracts\Processor\ProcessorInterface;
9
use Noitran\RQL\Exceptions\ExpressionException;
10
use Noitran\RQL\Processors\FilterStrategyResolver;
11
use Noitran\RQL\Queues\ExprQueue;
12
13
/**
14
 * Class EloquentProcessor
15
 */
16
class EloquentProcessor implements ProcessorInterface
17
{
18
    /**
19
     * @var Builder
20
     */
21
    protected $builder;
22
23
    /**
24
     * @var FilterStrategyResolver
25
     */
26
    protected $resolver;
27
28
    /**
29
     * @var array
30
     */
31
    public static $methodMap = [
32
        '$eq' => 'where',
33
        '$notEq' => 'where',
34
        '$lt' => 'where',
35
        '$lte' => 'where',
36
        '$gt' => 'where',
37
        '$gte' => 'where',
38
        '$like' => 'where',
39
        '$in' => 'whereIn',
40
        '$notIn' => 'whereNotIn',
41
        '$between' => 'whereBetween',
42
        '$or' => null,
43
    ];
44
45
    /**
46
     * @var array
47
     */
48
    public static $comparisonMethods = [
49
        '$eq',
50
        '$notEq',
51
        '$lt',
52
        '$lte',
53
        '$gt',
54
        '$gte',
55
        '$like',
56
    ];
57
58
    /**
59
     * @return array
60
     */
61 11
    public static function getMethodMap(): array
62
    {
63 11
        return self::$methodMap;
64
    }
65
66
    /**
67
     * @return array
68
     */
69 12
    public static function getComparisonMethods(): array
70
    {
71 12
        return self::$comparisonMethods;
72
    }
73
74
    /**
75
     * EloquentProcessor constructor.
76
     *
77
     * @param FilterStrategyResolver $resolver
78
     */
79 12
    public function __construct(FilterStrategyResolver $resolver)
80
    {
81 12
        $this->resolver = $resolver;
82 12
    }
83
84
    /**
85
     * @return Builder|Model
86
     */
87 12
    public function getBuilder()
88
    {
89 12
        return $this->builder;
90
    }
91
92
    /**
93
     * @param Builder $builder
94
     *
95
     * @return EloquentProcessor
96
     */
97 12
    public function setBuilder(Builder $builder): self
98
    {
99 12
        $this->builder = $builder;
100
101 12
        return $this;
102
    }
103
104
    /**
105
     * @param ExprQueue $exprClasses
106
     *
107
     * @throws ExpressionException
108
     *
109
     * @return Builder
110
     */
111 12
    public function process(ExprQueue $exprClasses): Builder
112
    {
113 12
        foreach ($exprClasses as $exprClass) {
114
            /** @var ExprInterface $exprClass */
115 12
            $this->builder = $this->resolver
116 12
                ->resolve($this, $exprClass)
117 12
                ->apply($this, $exprClass);
118
        }
119
120 12
        return $this->builder;
121
    }
122
}
123