@@ -43,6 +43,15 @@ final class BinaryExpression implements LogicalObject, MappableObject
43
43
private $ left = null ;
44
44
private $ right = null ;
45
45
private $ logic = null ;
46
+ private $ brackets = true ;
47
+
48
+ /**
49
+ * @return BinaryExpression
50
+ */
51
+ public static function create ($ left , $ right , $ logic )
52
+ {
53
+ return new self ($ left , $ right , $ logic );
54
+ }
46
55
47
56
public function __construct ($ left , $ right , $ logic )
48
57
{
@@ -66,30 +75,41 @@ public function getLogic()
66
75
return $ this ->logic ;
67
76
}
68
77
78
+ /**
79
+ * @param boolean $noBrackets
80
+ * @return BinaryExpression
81
+ */
82
+ public function noBrackets ($ noBrackets = true )
83
+ {
84
+ $ this ->brackets = !$ noBrackets ;
85
+ return $ this ;
86
+ }
87
+
69
88
public function toDialectString (Dialect $ dialect )
70
89
{
71
- return
72
- '( '
73
- .$ dialect ->toFieldString ($ this ->left )
90
+ $ sql = $ dialect ->toFieldString ($ this ->left )
74
91
.' ' .$ dialect ->logicToString ($ this ->logic ).' '
75
- .$ dialect ->toValueString ($ this ->right )
76
- . ' ) ' ;
92
+ .$ dialect ->toValueString ($ this ->right );
93
+ return $ this -> brackets ? " ( { $ sql } ) " : $ sql ;
77
94
}
78
95
79
96
/**
80
97
* @return BinaryExpression
81
98
**/
82
99
public function toMapped (ProtoDAO $ dao , JoinCapableQuery $ query )
83
100
{
84
- return new self (
101
+ $ expression = new self (
85
102
$ dao ->guessAtom ($ this ->left , $ query ),
86
103
$ dao ->guessAtom ($ this ->right , $ query ),
87
104
$ this ->logic
88
105
);
106
+
107
+ return $ expression ->noBrackets (!$ this ->brackets );
89
108
}
90
109
91
110
public function toBoolean (Form $ form )
92
111
{
112
+ Assert::isTrue ($ this ->brackets , 'brackets must be enabled ' );
93
113
$ left = $ form ->toFormValue ($ this ->left );
94
114
$ right = $ form ->toFormValue ($ this ->right );
95
115
0 commit comments