1
|
|
<?php
|
2
|
|
|
3
|
|
namespace Nuwave\Lighthouse\WhereConditions;
|
4
|
|
|
5
|
|
use GraphQL\Error\Error;
|
6
|
|
|
7
|
|
class SQLOperator implements Operator
|
8
|
|
{
|
9
|
1
|
public static function missingValueForColumn(string $column): string
|
10
|
|
{
|
11
|
1
|
return "Did not receive a value to match the WhereConditions for column {$column}.";
|
12
|
|
}
|
13
|
|
|
14
|
1
|
public function enumDefinition(): string
|
15
|
|
{
|
16
|
|
return /** @lang GraphQL */ <<<'GRAPHQL'
|
17
|
1
|
"The available SQL operators that are used to filter query results."
|
18
|
|
enum SQLOperator {
|
19
|
|
"Equal operator (`=`)"
|
20
|
|
EQ @enum(value: "=")
|
21
|
|
|
22
|
|
"Not equal operator (`!=`)"
|
23
|
|
NEQ @enum(value: "!=")
|
24
|
|
|
25
|
|
"Greater than operator (`>`)"
|
26
|
|
GT @enum(value: ">")
|
27
|
|
|
28
|
|
"Greater than or equal operator (`>=`)"
|
29
|
|
GTE @enum(value: ">=")
|
30
|
|
|
31
|
|
"Less than operator (`<`)"
|
32
|
|
LT @enum(value: "<")
|
33
|
|
|
34
|
|
"Less than or equal operator (`<=`)"
|
35
|
|
LTE @enum(value: "<=")
|
36
|
|
|
37
|
|
"Simple pattern matching (`LIKE`)"
|
38
|
|
LIKE @enum(value: "LIKE")
|
39
|
|
|
40
|
|
"Negation of simple pattern matching (`NOT LIKE`)"
|
41
|
|
NOT_LIKE @enum(value: "NOT_LIKE")
|
42
|
|
|
43
|
|
"Whether a value is within a set of values (`IN`)"
|
44
|
|
IN @enum(value: "In")
|
45
|
|
|
46
|
|
"Whether a value is not within a set of values (`NOT IN`)"
|
47
|
|
NOT_IN @enum(value: "NotIn")
|
48
|
|
|
49
|
|
"Whether a value is within a range of values (`BETWEEN`)"
|
50
|
|
BETWEEN @enum(value: "Between")
|
51
|
|
|
52
|
|
"Whether a value is not within a range of values (`NOT BETWEEN`)"
|
53
|
|
NOT_BETWEEN @enum(value: "NotBetween")
|
54
|
|
|
55
|
|
"Whether a value is null (`IS NULL`)"
|
56
|
|
IS_NULL @enum(value: "Null")
|
57
|
|
|
58
|
|
"Whether a value is not null (`IS NOT NULL`)"
|
59
|
|
IS_NOT_NULL @enum(value: "NotNull")
|
60
|
|
}
|
61
|
|
GRAPHQL;
|
62
|
|
}
|
63
|
|
|
64
|
1
|
public function default(): string
|
65
|
|
{
|
66
|
1
|
return 'EQ';
|
67
|
|
}
|
68
|
|
|
69
|
1
|
public function defaultHasOperator(): string
|
70
|
|
{
|
71
|
1
|
return 'GTE';
|
72
|
|
}
|
73
|
|
|
74
|
1
|
public function applyConditions($builder, array $whereConditions, string $boolean)
|
75
|
|
{
|
76
|
1
|
$column = $whereConditions['column'];
|
77
|
|
|
78
|
|
// Laravel's conditions always start off with this prefix
|
79
|
1
|
$method = 'where';
|
80
|
|
|
81
|
|
// The first argument to conditions methods is always the column name
|
82
|
1
|
$args = [$column];
|
83
|
|
|
84
|
|
// Some operators require calling Laravel's conditions in different ways
|
85
|
1
|
$operator = $whereConditions['operator'];
|
86
|
1
|
$arity = $this->operatorArity($operator);
|
87
|
|
|
88
|
1
|
if ($arity === 3) {
|
89
|
|
// Usually, the operator is passed as the second argument to the condition
|
90
|
|
// method, e.g. ->where('some_col', '=', $value)
|
91
|
1
|
$args[] = $operator;
|
92
|
|
} else {
|
93
|
|
// We utilize the fact that the operators are named after Laravel's condition
|
94
|
|
// methods so we can simply append the name, e.g. whereNull, whereNotBetween
|
95
|
1
|
$method .= $operator;
|
96
|
|
}
|
97
|
|
|
98
|
1
|
if ($arity > 1) {
|
99
|
|
// The conditions with arity 1 require no args apart from the column name.
|
100
|
|
// All other arities take a value to query against.
|
101
|
1
|
if (! array_key_exists('value', $whereConditions)) {
|
102
|
1
|
throw new Error(
|
103
|
1
|
self::missingValueForColumn($column)
|
104
|
|
);
|
105
|
|
}
|
106
|
|
|
107
|
1
|
$args[] = $whereConditions['value'];
|
108
|
|
}
|
109
|
|
|
110
|
|
// The condition methods always have the `$boolean` arg after the value
|
111
|
1
|
$args[] = $boolean;
|
112
|
|
|
113
|
1
|
return $builder->{$method}(...$args);
|
114
|
|
}
|
115
|
|
|
116
|
1
|
protected function operatorArity(string $operator): int
|
117
|
|
{
|
118
|
1
|
if (in_array($operator, ['Null', 'NotNull'])) {
|
119
|
1
|
return 1;
|
120
|
|
}
|
121
|
|
|
122
|
1
|
if (in_array($operator, ['In', 'NotIn', 'Between', 'NotBetween'])) {
|
123
|
1
|
return 2;
|
124
|
|
}
|
125
|
|
|
126
|
1
|
return 3;
|
127
|
|
}
|
128
|
|
}
|