1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 12/22/14 |
5
|
|
|
* Time: 11:37 AM. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Sql\QueryFormatter\Helper; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Sql\QueryFormatter\Formatter; |
14
|
|
|
use NilPortugues\Sql\QueryFormatter\Tokenizer\Tokenizer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Parentheses. |
18
|
|
|
*/ |
19
|
|
|
class Parentheses |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $inlineParentheses = false; |
25
|
|
|
/** |
26
|
|
|
* @var \NilPortugues\Sql\QueryFormatter\Formatter |
27
|
|
|
*/ |
28
|
|
|
protected $formatter; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Indent |
32
|
|
|
*/ |
33
|
|
|
protected $indentation; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Formatter $formatter |
37
|
|
|
* @param Indent $indentation |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Formatter $formatter, Indent $indentation) |
40
|
|
|
{ |
41
|
|
|
$this->formatter = $formatter; |
42
|
|
|
$this->indentation = $indentation; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function getInlineParentheses() |
49
|
|
|
{ |
50
|
|
|
return $this->inlineParentheses; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param bool $inlineParentheses |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function setInlineParentheses($inlineParentheses) |
59
|
|
|
{ |
60
|
|
|
$this->inlineParentheses = $inlineParentheses; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $token |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function stringIsOpeningParentheses($token) |
71
|
|
|
{ |
72
|
|
|
return $token[Tokenizer::TOKEN_VALUE] === '('; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* |
77
|
|
|
*/ |
78
|
|
|
public function writeNewInlineParentheses() |
79
|
|
|
{ |
80
|
|
|
$this->inlineParentheses = true; |
81
|
|
|
$this->formatter->setInlineCount(0); |
82
|
|
|
$this->indentation->setInlineIndented(false); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $token |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function invalidParenthesesTokenValue($token) |
91
|
|
|
{ |
92
|
|
|
return $token[Tokenizer::TOKEN_VALUE] === ';' |
93
|
|
|
|| $token[Tokenizer::TOKEN_VALUE] === '('; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $token |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function invalidParenthesesTokenType($token) |
102
|
|
|
{ |
103
|
|
|
return $token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_RESERVED_TOP_LEVEL |
104
|
|
|
|| $token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_RESERVED_NEWLINE |
105
|
|
|
|| $token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_COMMENT |
106
|
|
|
|| $token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_BLOCK_COMMENT; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $token |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function stringIsClosingParentheses($token) |
115
|
|
|
{ |
116
|
|
|
return $token[Tokenizer::TOKEN_VALUE] === ')'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $tab |
121
|
|
|
* @param $queryValue |
122
|
|
|
*/ |
123
|
|
|
public function writeInlineParenthesesBlock($tab, $queryValue) |
124
|
|
|
{ |
125
|
|
|
$this->formatter->setFormattedSql(\rtrim($this->formatter->getFormattedSql(), ' ')); |
126
|
|
|
|
127
|
|
|
if ($this->indentation->getInlineIndented()) { |
128
|
|
|
$indentTypes = $this->indentation->getIndentTypes(); |
129
|
|
|
\array_shift($indentTypes); |
130
|
|
|
$this->indentation->setIndentTypes($indentTypes); |
131
|
|
|
$this->indentation->setIndentLvl($this->indentation->getIndentLvl() - 1); |
132
|
|
|
|
133
|
|
|
$this->formatter->appendToFormattedSql("\n".str_repeat($tab, $this->indentation->getIndentLvl())); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->inlineParentheses = false; |
137
|
|
|
$this->formatter->appendToFormattedSql($queryValue.' '); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|