2020namespace Doctrine \ORM \Query \AST \Functions ;
2121
2222use Doctrine \ORM \Query \Lexer ;
23+ use Doctrine \ORM \Query \Parser ;
24+ use Doctrine \ORM \Query \SqlWalker ;
2325use Doctrine \DBAL \Platforms \AbstractPlatform ;
2426
2527/**
3638class TrimFunction extends FunctionNode
3739{
3840 /**
39- * @var bool
41+ * @var boolean
4042 */
4143 public $ leading ;
4244
4345 /**
44- * @var bool
46+ * @var boolean
4547 */
4648 public $ trailing ;
4749
4850 /**
49- * @var bool
51+ * @var boolean
5052 */
5153 public $ both ;
5254
5355 /**
54- * @var bool
56+ * @var boolean
5557 */
5658 public $ trimChar = false ;
5759
60+ /**
61+ * @var \Doctrine\ORM\Query\AST\Node
62+ */
5863 public $ stringPrimary ;
5964
6065 /**
61- * @override
66+ * {@inheritdoc}
6267 */
63- public function getSql (\ Doctrine \ ORM \ Query \ SqlWalker $ sqlWalker )
68+ public function getSql (SqlWalker $ sqlWalker )
6469 {
65- $ pos = AbstractPlatform::TRIM_UNSPECIFIED ;
66- if ($ this ->leading ) {
67- $ pos = AbstractPlatform::TRIM_LEADING ;
68- } else if ($ this ->trailing ) {
69- $ pos = AbstractPlatform::TRIM_TRAILING ;
70- } else if ($ this ->both ) {
71- $ pos = AbstractPlatform::TRIM_BOTH ;
72- }
73-
74- return $ sqlWalker ->getConnection ()->getDatabasePlatform ()->getTrimExpression (
75- $ sqlWalker ->walkStringPrimary ($ this ->stringPrimary ),
76- $ pos ,
77- ($ this ->trimChar != false ) ? $ sqlWalker ->getConnection ()->quote ($ this ->trimChar ) : false
78- );
70+ $ stringPrimary = $ sqlWalker ->walkStringPrimary ($ this ->stringPrimary );
71+ $ platform = $ sqlWalker ->getConnection ()->getDatabasePlatform ();
72+ $ trimMode = $ this ->getTrimMode ();
73+ $ trimChar = ($ this ->trimChar !== false )
74+ ? $ sqlWalker ->getConnection ()->quote ($ this ->trimChar )
75+ : false ;
76+
77+ return $ platform ->getTrimExpression ($ stringPrimary , $ trimMode , $ trimChar );
7978 }
8079
8180 /**
82- * @override
81+ * {@inheritdoc}
8382 */
84- public function parse (\ Doctrine \ ORM \ Query \ Parser $ parser )
83+ public function parse (Parser $ parser )
8584 {
8685 $ lexer = $ parser ->getLexer ();
8786
8887 $ parser ->match (Lexer::T_IDENTIFIER );
8988 $ parser ->match (Lexer::T_OPEN_PARENTHESIS );
9089
91- if (strcasecmp ('leading ' , $ lexer ->lookahead ['value ' ]) === 0 ) {
92- $ parser ->match (Lexer::T_LEADING );
93- $ this ->leading = true ;
94- } else if (strcasecmp ('trailing ' , $ lexer ->lookahead ['value ' ]) === 0 ) {
95- $ parser ->match (Lexer::T_TRAILING );
96- $ this ->trailing = true ;
97- } else if (strcasecmp ('both ' , $ lexer ->lookahead ['value ' ]) === 0 ) {
98- $ parser ->match (Lexer::T_BOTH );
99- $ this ->both = true ;
100- }
90+ $ this ->parseTrimMode ($ parser );
10191
10292 if ($ lexer ->isNextToken (Lexer::T_STRING )) {
10393 $ parser ->match (Lexer::T_STRING );
94+
10495 $ this ->trimChar = $ lexer ->token ['value ' ];
10596 }
10697
@@ -112,4 +103,61 @@ public function parse(\Doctrine\ORM\Query\Parser $parser)
112103
113104 $ parser ->match (Lexer::T_CLOSE_PARENTHESIS );
114105 }
106+
107+ /**
108+ * @param \Doctrine\ORM\Query\Parser $parser
109+ *
110+ * @return integer
111+ */
112+ private function getTrimMode ()
113+ {
114+ if ($ this ->leading ) {
115+ return AbstractPlatform::TRIM_LEADING ;
116+ }
117+
118+ if ($ this ->trailing ) {
119+ return AbstractPlatform::TRIM_TRAILING ;
120+ }
121+
122+ if ($ this ->both ) {
123+ return AbstractPlatform::TRIM_BOTH ;
124+ }
125+
126+ return AbstractPlatform::TRIM_UNSPECIFIED ;
127+ }
128+
129+ /**
130+ * @param \Doctrine\ORM\Query\Parser $parser
131+ *
132+ * @return void
133+ */
134+ private function parseTrimMode (Parser $ parser )
135+ {
136+ $ lexer = $ parser ->getLexer ();
137+ $ value = $ lexer ->lookahead ['value ' ];
138+
139+ if (strcasecmp ('leading ' , $ value ) === 0 ) {
140+ $ parser ->match (Lexer::T_LEADING );
141+
142+ $ this ->leading = true ;
143+
144+ return ;
145+ }
146+
147+ if (strcasecmp ('trailing ' , $ value ) === 0 ) {
148+ $ parser ->match (Lexer::T_TRAILING );
149+
150+ $ this ->trailing = true ;
151+
152+ return ;
153+ }
154+
155+ if (strcasecmp ('both ' , $ value ) === 0 ) {
156+ $ parser ->match (Lexer::T_BOTH );
157+
158+ $ this ->both = true ;
159+
160+ return ;
161+ }
162+ }
115163}
0 commit comments