1: | <?php |
2: | |
3: | namespace Baril\Sqlout; |
4: | |
5: | use Closure; |
6: | use Laravel\Scout\Builder as ScoutBuilder; |
7: | |
8: | class Builder extends ScoutBuilder |
9: | { |
10: | public const NATURAL_LANGUAGE = 'in natural language mode'; |
11: | public const QUERY_EXPANSION = 'in natural language mode with query expansion'; |
12: | public const BOOLEAN = 'in boolean mode'; |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | public $mode; |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | public $scopes = []; |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | public function __construct($model, $query, $callback = null, $softDelete = false) |
38: | { |
39: | parent::__construct($model, $query, $callback, $softDelete); |
40: | if ($softDelete) { |
41: | unset($this->wheres['__soft_deleted']); |
42: | } |
43: | } |
44: | |
45: | public function __call($method, $parameters) |
46: | { |
47: | if (static::hasMacro($method)) { |
48: | return parent::__call($method, $parameters); |
49: | } |
50: | |
51: | $this->scopes[] = [$method, $parameters]; |
52: | return $this; |
53: | } |
54: | |
55: | public function scope(Closure $callback) |
56: | { |
57: | $this->scopes[] = $callback; |
58: | return $this; |
59: | } |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | public function where($field, $value) |
69: | { |
70: | $args = func_get_args(); |
71: | $this->scopes[] = ['where', $args]; |
72: | return $this; |
73: | } |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | public function withTrashed() |
81: | { |
82: | $this->scopes[] = ['withTrashed', []]; |
83: | return $this; |
84: | } |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | public function onlyTrashed() |
92: | { |
93: | $this->scopes[] = ['onlyTrashed', []]; |
94: | return $this; |
95: | } |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | public function orderByScore($direction = 'desc') |
104: | { |
105: | return $this->orderBy('_score', $direction); |
106: | } |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | public function only($fields) |
115: | { |
116: | return parent::where('field', $fields); |
117: | } |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | public function mode($mode) |
126: | { |
127: | $this->mode = trim(strtolower($mode)); |
128: | return $this; |
129: | } |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | public function inNaturalLanguageMode() |
138: | { |
139: | return $this->mode(static::NATURAL_LANGUAGE); |
140: | } |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | |
147: | |
148: | public function withQueryExpansion() |
149: | { |
150: | return $this->mode(static::QUERY_EXPANSION); |
151: | } |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | public function inBooleanMode() |
160: | { |
161: | return $this->mode(static::BOOLEAN); |
162: | } |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | public function count() |
170: | { |
171: | return $this->engine()->getTotalCount( |
172: | $this->engine()->search($this) |
173: | ); |
174: | } |
175: | } |
176: | |