@@ -22,18 +22,18 @@ public function where(
2222 string $ boolean = Keyword::AND
2323 ): self {
2424 if ($ column instanceof Closure) {
25- return tap ($ this , function () use ($ boolean , $ column ) {
26- if (empty ($ this ->query )) {
27- $ queryTemplate = "(%s) " ;
28- } else {
29- $ queryTemplate = "$ this ->query $ boolean (%s) " ;
30- $ this ->query = '' ;
31- }
25+ if (empty ($ this ->query )) {
26+ $ queryTemplate = "(%s) " ;
27+ } else {
28+ $ queryTemplate = "$ this ->query $ boolean (%s) " ;
29+ $ this ->query = '' ;
30+ }
31+
32+ $ column ($ this );
3233
33- $ column ( $ this );
34+ $ this -> query = sprintf ( $ queryTemplate , $ this -> query );
3435
35- $ this ->query = sprintf ($ queryTemplate , $ this ->query );
36- });
36+ return $ this ;
3737 }
3838
3939 if (count (func_get_args ()) === 2 ) {
@@ -42,7 +42,9 @@ public function where(
4242
4343 $ this ->invalidBooleanOrOperator ($ boolean , $ operator , $ value );
4444
45- return tap ($ this , fn () => $ this ->appendQuery ("$ column $ operator {$ this ->quote ($ operator , $ value )}" , $ boolean ));
45+ $ this ->appendQuery ("$ column $ operator {$ this ->quote ($ operator , $ value )}" , $ boolean );
46+
47+ return $ this ;
4648 }
4749
4850 public function orWhere (string |Closure $ column , mixed $ operator = Operator::EQUALS , mixed $ value = null ): self
@@ -51,12 +53,14 @@ public function orWhere(string|Closure $column, mixed $operator = Operator::EQUA
5153 [$ column , $ operator , $ value ] = [$ column , is_array ($ operator ) ? Operator::IN : Operator::EQUALS , $ operator ];
5254 }
5355
54- return tap ($ this , fn () => $ this ->where ($ column , $ operator , $ value , Keyword::OR ));
56+ $ this ->where ($ column , $ operator , $ value , Keyword::OR );
57+
58+ return $ this ;
5559 }
5660
5761 public function when (mixed $ value , callable $ callback ): self
5862 {
59- $ value = value ( $ value, $ this );
63+ $ value = $ value instanceof Closure ? $ value ( $ this ) : $ value ;
6064
6165 if ($ value ) {
6266 return $ callback ($ this , $ value ) ?: $ this ;
@@ -67,7 +71,7 @@ public function when(mixed $value, callable $callback): self
6771
6872 public function whenNot (mixed $ value , callable $ callback ): self
6973 {
70- $ value = value ( $ value, $ this );
74+ $ value = $ value instanceof Closure ? $ value ( $ this ) : $ value ;
7175
7276 if (! $ value ) {
7377 return $ callback ($ this , $ value ) ?: $ this ;
@@ -78,12 +82,16 @@ public function whenNot(mixed $value, callable $callback): self
7882
7983 public function orderBy (string $ column , string $ direction ): self
8084 {
81- return tap ($ this , fn () => $ this ->appendQuery (Keyword::ORDER_BY ." $ column $ direction " ));
85+ $ this ->appendQuery (Keyword::ORDER_BY ." $ column $ direction " );
86+
87+ return $ this ;
8288 }
8389
8490 public function rawQuery (string $ query ): self
8591 {
86- return tap ($ this , fn () => $ this ->appendQuery ($ query ));
92+ $ this ->appendQuery ($ query );
93+
94+ return $ this ;
8795 }
8896
8997 public function getQuery (): string
@@ -99,13 +107,16 @@ public function __toString(): string
99107 private function quote (string $ operator , mixed $ value ): string
100108 {
101109 if (in_array ($ operator , [Operator::IN , Operator::NOT_IN , Operator::WAS_IN , Operator::WAS_NOT_IN ])) {
102- $ values = array_reduce (array_wrap ($ value ), function ($ prev , $ current ) {
103- if ($ prev === null ) {
104- return '" ' .str_replace ('" ' , '\\" ' , $ current ).'" ' ;
110+ $ values = array_reduce (
111+ is_array ($ value ) ? $ value : [$ value ],
112+ function ($ prev , $ current ) {
113+ if ($ prev === null ) {
114+ return '" ' .str_replace ('" ' , '\\" ' , $ current ).'" ' ;
115+ }
116+
117+ return $ prev .', " ' .str_replace ('" ' , '\\" ' , $ current ).'" ' ;
105118 }
106-
107- return $ prev .', " ' .str_replace ('" ' , '\\" ' , $ current ).'" ' ;
108- });
119+ );
109120
110121 return "( $ values) " ;
111122 }
0 commit comments