1
+ <?php
2
+
3
+ namespace Rcalicdan \FiberAsync \QueryBuilder \Traits ;
4
+
5
+ trait QueryAdvancedConditionsTrait
6
+ {
7
+ /**
8
+ * Add a custom condition group with specific logic.
9
+ *
10
+ * @param callable(static): void $callback Callback function that receives a new query builder instance.
11
+ * @param string $logicalOperator How this group connects to others ('AND' or 'OR').
12
+ * @return static Returns a new query builder instance for method chaining.
13
+ */
14
+ public function whereGroup (callable $ callback , string $ logicalOperator = 'AND ' ): static
15
+ {
16
+ $ subBuilder = new static ($ this ->table );
17
+ $ callback ($ subBuilder );
18
+
19
+ $ subSql = $ subBuilder ->buildWhereClause ();
20
+
21
+ if ($ subSql === '' ) {
22
+ return $ this ;
23
+ }
24
+
25
+ return $ this ->whereRaw ("( {$ subSql }) " , $ subBuilder ->getCompiledBindings (), $ logicalOperator );
26
+ }
27
+
28
+ /**
29
+ * Add nested WHERE conditions with custom logic.
30
+ *
31
+ * @param callable(static): void $callback Callback function for nested conditions.
32
+ * @param string $operator How to connect with existing conditions.
33
+ * @return static Returns a new query builder instance for method chaining.
34
+ */
35
+ public function whereNested (callable $ callback , string $ operator = 'AND ' ): static
36
+ {
37
+ return $ this ->whereGroup ($ callback , $ operator );
38
+ }
39
+
40
+ /**
41
+ * Add a nested OR WHERE condition with custom logic.
42
+ *
43
+ * @param callable(static): void $callback Callback function for nested conditions.
44
+ * @return static Returns a new query builder instance for method chaining.
45
+ */
46
+ public function orWhereNested (callable $ callback ): static
47
+ {
48
+ return $ this ->whereGroup ($ callback , 'OR ' );
49
+ }
50
+
51
+ /**
52
+ * Add conditions with EXISTS clause.
53
+ *
54
+ * @param callable(static): void $callback Callback function for the EXISTS subquery.
55
+ * @param string $operator Logical operator ('AND' or 'OR').
56
+ * @return static Returns a new query builder instance for method chaining.
57
+ */
58
+ public function whereExists (callable $ callback , string $ operator = 'AND ' ): static
59
+ {
60
+ $ subBuilder = new static ;
61
+ $ callback ($ subBuilder );
62
+
63
+ $ subSql = $ subBuilder ->buildSelectQuery ();
64
+ $ condition = "EXISTS ( {$ subSql }) " ;
65
+
66
+ return $ this ->whereRaw ($ condition , $ subBuilder ->getCompiledBindings (), $ operator );
67
+ }
68
+
69
+ /**
70
+ * Add conditions with NOT EXISTS clause.
71
+ *
72
+ * @param callable(static): void $callback Callback function for the NOT EXISTS subquery.
73
+ * @param string $operator Logical operator ('AND' or 'OR').
74
+ * @return static Returns a new query builder instance for method chaining.
75
+ */
76
+ public function whereNotExists (callable $ callback , string $ operator = 'AND ' ): static
77
+ {
78
+ $ subBuilder = new static ;
79
+ $ callback ($ subBuilder );
80
+
81
+ $ subSql = $ subBuilder ->buildSelectQuery ();
82
+ $ condition = "NOT EXISTS ( {$ subSql }) " ;
83
+
84
+ return $ this ->whereRaw ($ condition , $ subBuilder ->getCompiledBindings (), $ operator );
85
+ }
86
+
87
+ /**
88
+ * Add an OR WHERE EXISTS clause.
89
+ *
90
+ * @param callable(static): void $callback Callback function for the EXISTS subquery.
91
+ * @return static Returns a new query builder instance for method chaining.
92
+ */
93
+ public function orWhereExists (callable $ callback ): static
94
+ {
95
+ return $ this ->whereExists ($ callback , 'OR ' );
96
+ }
97
+
98
+ /**
99
+ * Add an OR WHERE NOT EXISTS clause.
100
+ *
101
+ * @param callable(static): void $callback Callback function for the NOT EXISTS subquery.
102
+ * @return static Returns a new query builder instance for method chaining.
103
+ */
104
+ public function orWhereNotExists (callable $ callback ): static
105
+ {
106
+ return $ this ->whereNotExists ($ callback , 'OR ' );
107
+ }
108
+
109
+ /**
110
+ * Add a WHERE clause with a subquery.
111
+ *
112
+ * @param string $column The column name.
113
+ * @param string $operator The comparison operator.
114
+ * @param callable(static): void $callback Callback function for the subquery.
115
+ * @return static Returns a new query builder instance for method chaining.
116
+ */
117
+ public function whereSub (string $ column , string $ operator , callable $ callback ): static
118
+ {
119
+ $ subBuilder = new static ;
120
+ $ callback ($ subBuilder );
121
+
122
+ $ subSql = $ subBuilder ->buildSelectQuery ();
123
+ $ condition = "{$ column } {$ operator } ( {$ subSql }) " ;
124
+
125
+ return $ this ->whereRaw ($ condition , $ subBuilder ->getCompiledBindings ());
126
+ }
127
+ }
0 commit comments