|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Noitran\RQL\Processors; |
|
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\Expressions\AndExpr; |
|
10
|
|
|
use Noitran\RQL\Expressions\OrExpr; |
|
11
|
|
|
use Noitran\RQL\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 array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $methodMap = [ |
|
27
|
|
|
'$eq' => 'where', |
|
28
|
|
|
'$notEq' => 'where', |
|
29
|
|
|
'$lt' => 'where', |
|
30
|
|
|
'$lte' => 'where', |
|
31
|
|
|
'$gt' => 'where', |
|
32
|
|
|
'$gte' => 'where', |
|
33
|
|
|
'$like' => 'where', |
|
34
|
|
|
|
|
35
|
|
|
'$in' => 'whereIn', |
|
36
|
|
|
'$notIn' => 'whereNotIn', |
|
37
|
|
|
'$between' => 'whereBetween', |
|
38
|
|
|
|
|
39
|
|
|
'$or' => null, |
|
40
|
|
|
'$and' => null, |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected static $comparisonMethods = [ |
|
47
|
|
|
'$eq', |
|
48
|
|
|
'$notEq', |
|
49
|
|
|
'$lt', |
|
50
|
|
|
'$lte', |
|
51
|
|
|
'$gt', |
|
52
|
|
|
'$gte', |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* EloquentProcessor constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Builder $builder |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct($builder) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->builder = $builder; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return Builder|Model |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getBuilder() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->builder; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param ExprQueue $exprClasses |
|
75
|
|
|
* |
|
76
|
|
|
* @return Builder |
|
77
|
|
|
*/ |
|
78
|
|
|
public function process(ExprQueue $exprClasses): Builder |
|
79
|
|
|
{ |
|
80
|
|
|
foreach ($exprClasses as $exprClass) { |
|
81
|
|
|
/** @var ExprInterface $exprClass */ |
|
82
|
|
|
if (in_array($exprClass->getExpression(), self::$comparisonMethods, true)) { |
|
83
|
|
|
return $this->applyComparison($exprClass); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (in_array($exprClass->getExpression(), ['$in', '$notIn', '$between'], true)) { |
|
87
|
|
|
return $this->applyIfValueIsArray($exprClass); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ($exprClass instanceof OrExpr) { |
|
91
|
|
|
return $this->applyBetween($exprClass); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($exprClass instanceof AndExpr) { |
|
95
|
|
|
return $this->applyBetween($exprClass); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->getBuilder(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param ExprInterface $exprClass |
|
104
|
|
|
* |
|
105
|
|
|
* @return Builder |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function applyComparison(ExprInterface $exprClass): Builder |
|
108
|
|
|
{ |
|
109
|
|
|
$method = self::$methodMap[$exprClass->getExpression()]; |
|
110
|
|
|
|
|
111
|
|
|
return $this->getBuilder()->{$method}( |
|
112
|
|
|
$exprClass->getColumn(), |
|
113
|
|
|
$exprClass->getOperator(), |
|
114
|
|
|
$exprClass->getValue() |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param ExprInterface $exprClass |
|
120
|
|
|
* |
|
121
|
|
|
* @return Builder |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function applyIfValueIsArray(ExprInterface $exprClass): Builder |
|
124
|
|
|
{ |
|
125
|
|
|
$method = self::$methodMap[$exprClass->getExpression()]; |
|
126
|
|
|
|
|
127
|
|
|
return $this->getBuilder()->{$method}( |
|
128
|
|
|
$exprClass->getColumn(), |
|
129
|
|
|
$exprClass->getValue() |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.