99 */
1010namespace NilPortugues \SqlQueryFormatter ;
1111
12+ use NilPortugues \SqlQueryFormatter \Helper \Indent ;
1213use NilPortugues \SqlQueryFormatter \Helper \Tokenizer ;
1314
1415/**
@@ -24,11 +25,6 @@ class Formatter
2425 */
2526 private $ tab = " " ;
2627
27- /**
28- * @var int
29- */
30- private $ indentLvl = 0 ;
31-
3228 /**
3329 * @var int
3430 */
@@ -39,30 +35,11 @@ class Formatter
3935 */
4036 private $ newline = false ;
4137
42- /**
43- * @var bool
44- */
45- private $ increaseSpecialIndent = false ;
46-
47- /**
48- * @var bool
49- */
50- private $ increaseBlockIndent = false ;
51-
5238 /**
5339 * @var bool
5440 */
5541 private $ inlineParentheses = false ;
5642
57- /**
58- * @var array
59- */
60- private $ indentTypes = array ();
61-
62- /**
63- * @var bool
64- */
65- private $ inlineIndented = false ;
6643
6744 /**
6845 * @var bool
@@ -74,12 +51,18 @@ class Formatter
7451 */
7552 private $ formattedSql = '' ;
7653
54+ /**
55+ * @var Helper\Indent
56+ */
57+ private $ indentation ;
58+
7759 /**
7860 *
7961 */
8062 public function __construct ()
8163 {
8264 $ this ->tokenizer = new Tokenizer ();
65+ $ this ->indentation = new Indent ();
8366 }
8467
8568 /**
@@ -100,8 +83,8 @@ public function format($sql)
10083 foreach ($ tokens as $ i => $ token ) {
10184 $ queryValue = $ token [Tokenizer::TOKEN_VALUE ];
10285
103- $ this ->increaseSpecialIndent ();
104- $ this ->increaseBlockIndent ();
86+ $ this ->indentation -> increaseSpecialIndent ($ this );
87+ $ this ->indentation -> increaseBlockIndent ($ this );
10588
10689 $ addedNewline = $ this ->addNewLineBreak ($ tab );
10790
@@ -144,11 +127,11 @@ public function format($sql)
144127
145128 $ this ->addNewLineAfterOpeningParentheses ();
146129 } elseif ($ this ->stringIsClosingParentheses ($ token )) {
147- $ this ->decreaseIndentLevelUntilIndentTypeIsSpecial ();
130+ $ this ->indentation -> decreaseIndentLevelUntilIndentTypeIsSpecial ($ this );
148131 $ this ->addNewLineBeforeClosingParentheses ($ addedNewline , $ tab );
149132 } elseif ($ this ->isTokenTypeReservedTopLevel ($ token )) {
150- $ this ->increaseSpecialIndent = true ;
151- $ this ->decreaseSpecialIndentIfCurrentIndentTypeIsSpecial ();
133+ $ this ->indentation -> setIncreaseSpecialIndent ( true ) ;
134+ $ this ->indentation -> decreaseSpecialIndentIfCurrentIndentTypeIsSpecial ($ this );
152135 $ this ->writeNewLineBecauseOfTopLevelReservedWord ($ addedNewline , $ tab );
153136
154137 if ($ this ->tokenHasExtraWhiteSpaces ($ token )) {
@@ -214,29 +197,7 @@ private function removeTokenWhitespace(array &$originalTokens)
214197 return $ tokens ;
215198 }
216199
217- /**
218- * Increase the Special Indent if increaseSpecialIndent is true after the current iteration.
219- */
220- private function increaseSpecialIndent ()
221- {
222- if ($ this ->increaseSpecialIndent ) {
223- $ this ->indentLvl ++;
224- $ this ->increaseSpecialIndent = false ;
225- array_unshift ($ this ->indentTypes , 'special ' );
226- }
227- }
228200
229- /**
230- * Increase the Block Indent if increaseBlockIndent is true after the current iteration.
231- */
232- private function increaseBlockIndent ()
233- {
234- if ($ this ->increaseBlockIndent ) {
235- $ this ->indentLvl ++;
236- $ this ->increaseBlockIndent = false ;
237- array_unshift ($ this ->indentTypes , 'block ' );
238- }
239- }
240201
241202 /**
242203 * Adds a new line break if needed.
@@ -249,7 +210,7 @@ private function addNewLineBreak($tab)
249210 {
250211 $ addedNewline = false ;
251212 if ($ this ->newline ) {
252- $ this ->formattedSql .= "\n" . str_repeat ($ tab , $ this ->indentLvl );
213+ $ this ->formattedSql .= "\n" . str_repeat ($ tab , $ this ->indentation -> getIndentLvl () );
253214 $ this ->newline = false ;
254215 $ addedNewline = true ;
255216 }
@@ -279,7 +240,7 @@ private function stringHasCommentToken($token)
279240 private function writeCommentBlock ($ token , $ tab , $ queryValue )
280241 {
281242 if ($ token [Tokenizer::TOKEN_TYPE ] === Tokenizer::TOKEN_TYPE_BLOCK_COMMENT ) {
282- $ indent = str_repeat ($ tab , $ this ->indentLvl );
243+ $ indent = str_repeat ($ tab , $ this ->indentation -> getIndentLvl () );
283244
284245 $ this ->formattedSql .= "\n" . $ indent ;
285246 $ queryValue = str_replace ("\n" , "\n" . $ indent , $ queryValue );
@@ -309,10 +270,14 @@ private function writeInlineParenthesesBlock($tab, $queryValue)
309270 {
310271 $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
311272
312- if ($ this ->inlineIndented ) {
313- array_shift ($ this ->indentTypes );
314- $ this ->indentLvl --;
315- $ this ->formattedSql .= "\n" . str_repeat ($ tab , $ this ->indentLvl );
273+ if ($ this ->indentation ->getInlineIndented ()) {
274+ $ indentTypes = $ this ->indentation ->getIndentTypes ();
275+ array_shift ($ indentTypes );
276+ $ this ->indentation ->setIndentTypes ($ indentTypes );
277+
278+ $ this ->indentation ->setIndentLvl ($ this ->indentation ->getIndentLvl ()-1 );
279+
280+ $ this ->formattedSql .= "\n" . str_repeat ($ tab , $ this ->indentation ->getIndentLvl ());
316281 }
317282
318283 $ this ->inlineParentheses = false ;
@@ -349,7 +314,7 @@ private function writeNewInlineParentheses()
349314 {
350315 $ this ->inlineParentheses = true ;
351316 $ this ->inlineCount = 0 ;
352- $ this ->inlineIndented = false ;
317+ $ this ->indentation -> setInlineIndented ( false ) ;
353318 }
354319
355320 /**
@@ -384,8 +349,8 @@ private function invalidParenthesesTokenType($token)
384349 private function writeNewLineForLongInlineValues ($ length )
385350 {
386351 if ($ this ->inlineParentheses && $ length > 30 ) {
387- $ this ->increaseBlockIndent = true ;
388- $ this ->inlineIndented = true ;
352+ $ this ->indentation -> setIncreaseBlockIndent ( true ) ;
353+ $ this ->indentation -> setInlineIndented ( true ) ;
389354 $ this ->newline = true ;
390355 }
391356 }
@@ -408,27 +373,11 @@ private function isPrecedingCurrentTokenOfTokenTypeWhiteSpace($originalTokens, $
408373 private function addNewLineAfterOpeningParentheses ()
409374 {
410375 if (!$ this ->inlineParentheses ) {
411- $ this ->increaseBlockIndent = true ;
376+ $ this ->indentation -> setIncreaseBlockIndent ( true ) ;
412377 $ this ->newline = true ;
413378 }
414379 }
415380
416- /**
417- * Closing parentheses decrease the block indent level.
418- */
419- private function decreaseIndentLevelUntilIndentTypeIsSpecial ()
420- {
421- $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
422- $ this ->indentLvl --;
423-
424- while ($ j = array_shift ($ this ->indentTypes )) {
425- if ($ j === 'special ' ) {
426- $ this ->indentLvl --;
427- } else {
428- break ;
429- }
430- }
431- }
432381
433382 /**
434383 * @param boolean $addedNewline
@@ -437,7 +386,7 @@ private function decreaseIndentLevelUntilIndentTypeIsSpecial()
437386 private function addNewLineBeforeClosingParentheses ($ addedNewline , $ tab )
438387 {
439388 if (!$ addedNewline ) {
440- $ this ->formattedSql .= "\n" . str_repeat ($ tab , $ this ->indentLvl );
389+ $ this ->formattedSql .= "\n" . str_repeat ($ tab , $ this ->indentation -> getIndentLvl () );
441390 }
442391 }
443392
@@ -451,19 +400,6 @@ private function isTokenTypeReservedTopLevel($token)
451400 return $ token [Tokenizer::TOKEN_TYPE ] === Tokenizer::TOKEN_TYPE_RESERVED_TOP_LEVEL ;
452401 }
453402
454- /**
455- *
456- */
457- private function decreaseSpecialIndentIfCurrentIndentTypeIsSpecial ()
458- {
459- reset ($ this ->indentTypes );
460-
461- if (current ($ this ->indentTypes ) === 'special ' ) {
462- $ this ->indentLvl --;
463- array_shift ($ this ->indentTypes );
464- }
465- }
466-
467403 /**
468404 * @param boolean $addedNewline
469405 * @param string $tab
@@ -472,7 +408,7 @@ private function writeNewLineBecauseOfTopLevelReservedWord($addedNewline, $tab)
472408 {
473409 // Add a newline before the top level reserved word if necessary and indent.
474410 $ this ->formattedSql = (!$ addedNewline ) ? $ this ->formattedSql . "\n" : rtrim ($ this ->formattedSql , $ tab );
475- $ this ->formattedSql .= str_repeat ($ tab , $ this ->indentLvl );
411+ $ this ->formattedSql .= str_repeat ($ tab , $ this ->indentation -> getIndentLvl () );
476412
477413 // Add a newline after the top level reserved word
478414 $ this ->newline = true ;
@@ -545,7 +481,7 @@ private function isTokenTypeReservedNewLine($token)
545481 private function writeNewLineBeforeReservedWord ($ addedNewline , $ tab )
546482 {
547483 if (!$ addedNewline ) {
548- $ this ->formattedSql .= "\n" . str_repeat ($ tab , $ this ->indentLvl );
484+ $ this ->formattedSql .= "\n" . str_repeat ($ tab , $ this ->indentation -> getIndentLvl () );
549485 }
550486 }
551487
@@ -620,4 +556,23 @@ private function tokenIsNumberAndHasExtraWhiteSpaceRight($tokenType)
620556 && $ tokenType !== Tokenizer::TOKEN_TYPE_WORD
621557 && $ tokenType !== Tokenizer::TOKEN_TYPE_NUMBER ;
622558 }
559+
560+ /**
561+ * @param string $formattedSql
562+ *
563+ * @return $this
564+ */
565+ public function setFormattedSql ($ formattedSql )
566+ {
567+ $ this ->formattedSql = $ formattedSql ;
568+ return $ this ;
569+ }
570+
571+ /**
572+ * @return string
573+ */
574+ public function getFormattedSql ()
575+ {
576+ return $ this ->formattedSql ;
577+ }
623578}
0 commit comments