5
5
use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
6
6
use Illuminate \Database \Query \Builder as QueryBuilder ;
7
7
use InvalidArgumentException ;
8
+ use LastDragon_ru \LaraASP \GraphQL \PackageTranslator ;
8
9
use LastDragon_ru \LaraASP \GraphQL \SearchBy \Contracts \OperatorNegationable ;
9
10
use LastDragon_ru \LaraASP \GraphQL \SearchBy \Operators \Comparison \ComparisonOperator ;
10
11
use LastDragon_ru \LaraASP \GraphQL \SearchBy \Operators \Complex \ComplexOperator ;
16
17
use function implode ;
17
18
use function key ;
18
19
use function reset ;
19
- use function sprintf ;
20
20
21
21
class SearchBuilder {
22
22
/**
@@ -39,7 +39,10 @@ class SearchBuilder {
39
39
* |\LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical\LogicalOperator
40
40
* |\LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Complex\ComplexOperator> $operators
41
41
*/
42
- public function __construct (array $ operators ) {
42
+ public function __construct (
43
+ protected PackageTranslator $ translator ,
44
+ array $ operators ,
45
+ ) {
43
46
foreach ($ operators as $ operator ) {
44
47
if ($ operator instanceof ComparisonOperator) {
45
48
$ this ->comparison [$ operator ->getName ()] = $ operator ;
@@ -84,9 +87,11 @@ public function process(
84
87
85
88
// More than one property?
86
89
if (count ($ input ) > 1 ) {
87
- throw new SearchLogicException (sprintf (
88
- 'Only one property allowed, found: `%s`. ' ,
89
- implode ('`, ` ' , array_keys ($ input )),
90
+ throw new SearchLogicException ($ this ->translator ->get (
91
+ 'search_by.errors.too_many_properties ' ,
92
+ [
93
+ 'properties ' => implode ('`, ` ' , array_keys ($ input )),
94
+ ],
90
95
));
91
96
}
92
97
@@ -216,16 +221,18 @@ public function processComparison(
216
221
217
222
// Empty?
218
223
if (count ($ conditions ) <= 0 ) {
219
- throw new SearchLogicException (
220
- 'Search condition cannot be empty. ' ,
221
- );
224
+ throw new SearchLogicException ($ this -> translator -> get (
225
+ 'search_by.errors.empty_condition ' ,
226
+ )) ;
222
227
}
223
228
224
229
// More than one operator?
225
230
if (count ($ conditions ) > 1 ) {
226
- throw new SearchLogicException (sprintf (
227
- 'Only one comparison operator allowed, found: `%s`. ' ,
228
- implode ('`, ` ' , array_keys ($ conditions )),
231
+ throw new SearchLogicException ($ this ->translator ->get (
232
+ 'search_by.errors.too_many_operators ' ,
233
+ [
234
+ 'operators ' => implode ('`, ` ' , array_keys ($ conditions )),
235
+ ],
229
236
));
230
237
}
231
238
@@ -236,18 +243,22 @@ public function processComparison(
236
243
237
244
// Found?
238
245
if (!$ operator ) {
239
- throw new SearchLogicException (sprintf (
240
- 'Operator `%s` not found. ' ,
241
- $ name ,
246
+ throw new SearchLogicException ($ this ->translator ->get (
247
+ 'search_by.errors.unknown_operator ' ,
248
+ [
249
+ 'operator ' => $ name ,
250
+ ],
242
251
));
243
252
}
244
253
245
254
// Not allowed?
246
255
if ($ not && !($ operator instanceof OperatorNegationable)) {
247
- throw new SearchLogicException (sprintf (
248
- 'Operator `%s` cannot be used with `%s`. ' ,
249
- $ name ,
250
- Not::Name,
256
+ throw new SearchLogicException ($ this ->translator ->get (
257
+ 'search_by.errors.unsupported_option ' ,
258
+ [
259
+ 'operator ' => $ name ,
260
+ 'option ' => Not::Name,
261
+ ],
251
262
));
252
263
}
253
264
0 commit comments