6
6
7
7
class FilterBuilder extends Builder
8
8
{
9
+ public $ wheres = [
10
+ 'must ' => [],
11
+ 'must_not ' => []
12
+ ];
13
+
9
14
public function __construct ($ model , $ callback = null )
10
15
{
11
16
$ this ->model = $ model ;
@@ -28,98 +33,103 @@ public function where($field, $value)
28
33
$ operator = '= ' ;
29
34
}
30
35
31
- $ this ->wheres [] = [
32
- 'type ' => 'basic ' ,
33
- 'field ' => $ field ,
34
- 'operator ' => $ operator ,
35
- 'value ' => $ value
36
- ];
36
+ switch ($ operator ) {
37
+ case '= ' :
38
+ $ this ->wheres ['must ' ][] = ['term ' => [$ field => $ value ]];
39
+ break ;
40
+
41
+ case '> ' :
42
+ $ this ->wheres ['must ' ][] = ['range ' => [$ field => ['gt ' => $ value ]]];
43
+ break ;
44
+
45
+ case '< ' ;
46
+ $ this ->wheres ['must ' ][] = ['range ' => [$ field => ['lt ' => $ value ]]];
47
+ break ;
48
+
49
+ case '>= ' :
50
+ $ this ->wheres ['must ' ][] = ['range ' => [$ field => ['gte ' => $ value ]]];
51
+ break ;
52
+
53
+ case '<= ' :
54
+ $ this ->wheres ['must ' ][] = ['range ' => [$ field => ['lte ' => $ value ]]];
55
+ break ;
56
+
57
+ case '!= ' :
58
+ case '<> ' :
59
+ $ this ->wheres ['must_not ' ][] = ['term ' => [$ field => $ value ]];
60
+ break ;
61
+ }
37
62
38
63
return $ this ;
39
64
}
40
65
41
66
public function whereIn ($ field , array $ value )
42
67
{
43
- $ this ->wheres [] = [
44
- 'type ' => 'in ' ,
45
- 'field ' => $ field ,
46
- 'not ' => false ,
47
- 'value ' => $ value
48
- ];
68
+ $ this ->wheres ['must ' ][] = ['terms ' => [$ field => $ value ]];
49
69
50
70
return $ this ;
51
71
}
52
72
53
73
public function whereNotIn ($ field , array $ value )
54
74
{
55
- $ this ->wheres [] = [
56
- 'type ' => 'in ' ,
57
- 'field ' => $ field ,
58
- 'not ' => true ,
59
- 'value ' => $ value
60
- ];
75
+ $ this ->wheres ['must_not ' ][] = ['terms ' => [$ field => $ value ]];
61
76
62
77
return $ this ;
63
78
}
64
79
65
80
public function whereBetween ($ field , array $ value )
66
81
{
67
- $ this ->wheres [] = [
68
- 'type ' => 'between ' ,
69
- 'field ' => $ field ,
70
- 'not ' => false ,
71
- 'value ' => $ value
72
- ];
82
+ $ this ->wheres ['must ' ][] = ['range ' => [$ field => ['gte ' => $ value [0 ], 'lte ' => $ value [1 ]]]];
73
83
74
84
return $ this ;
75
85
}
76
86
77
87
public function whereNotBetween ($ field , array $ value )
78
88
{
79
- $ this ->wheres [] = [
80
- 'type ' => 'between ' ,
81
- 'field ' => $ field ,
82
- 'not ' => true ,
83
- 'value ' => $ value
84
- ];
89
+ $ this ->wheres ['must_not ' ][] = ['range ' => [$ field => ['gte ' => $ value [0 ], 'lte ' => $ value [1 ]]]];
85
90
86
91
return $ this ;
87
92
}
88
93
89
94
public function whereExists ($ field )
90
95
{
91
- $ this ->wheres [] = [
92
- 'type ' => 'exists ' ,
93
- 'field ' => $ field ,
94
- 'not ' => false
95
- ];
96
+ $ this ->wheres ['must ' ][] = ['exists ' => ['field ' => $ field ]];
96
97
97
98
return $ this ;
98
99
}
99
100
100
101
public function whereNotExists ($ field )
101
102
{
102
- $ this ->wheres [] = [
103
- 'type ' => ' exists ' ,
104
- 'field ' => $ field,
105
- ' not ' => true
103
+ $ this ->wheres [' must_not ' ][ ] = [
104
+ 'exists ' => [
105
+ 'field ' => $ field
106
+ ]
106
107
];
107
108
108
109
return $ this ;
109
110
}
110
111
111
112
public function whereRegexp ($ field , $ value , $ flags = 'ALL ' )
112
113
{
113
- $ this ->wheres [] = [
114
- 'type ' => 'regexp ' ,
115
- 'field ' => $ field ,
116
- 'value ' => $ value ,
117
- 'flags ' => $ flags
114
+ $ this ->wheres ['must ' ][] = [
115
+ 'regexp ' => [
116
+ $ field => [
117
+ 'value ' => $ value ,
118
+ 'flags ' => $ flags
119
+ ]
120
+ ]
118
121
];
119
122
120
123
return $ this ;
121
124
}
122
125
126
+ public function orderBy ($ column , $ direction = 'asc ' )
127
+ {
128
+ $ this ->orders [] = [$ column => strtolower ($ direction ) == 'asc ' ? 'asc ' : 'desc ' ];
129
+
130
+ return $ this ;
131
+ }
132
+
123
133
public function explain ()
124
134
{
125
135
return $ this ->engine ()->explain ($ this );
0 commit comments