1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 12/22/14 |
5
|
|
|
* Time: 10:09 PM. |
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 Comment. |
18
|
|
|
*/ |
19
|
|
|
class Comment |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \NilPortugues\Sql\QueryFormatter\Formatter |
23
|
|
|
*/ |
24
|
|
|
protected $formatter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Indent |
28
|
|
|
*/ |
29
|
|
|
protected $indentation; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var NewLine |
33
|
|
|
*/ |
34
|
|
|
protected $newLine; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Formatter $formatter |
38
|
|
|
* @param Indent $indentation |
39
|
|
|
* @param NewLine $newLine |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Formatter $formatter, Indent $indentation, NewLine $newLine) |
42
|
|
|
{ |
43
|
|
|
$this->formatter = $formatter; |
44
|
|
|
$this->indentation = $indentation; |
45
|
|
|
$this->newLine = $newLine; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $token |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function stringHasCommentToken($token) |
54
|
|
|
{ |
55
|
|
|
return $token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_COMMENT |
56
|
|
|
|| $token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_BLOCK_COMMENT; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $token |
61
|
|
|
* @param string $tab |
62
|
|
|
* @param $queryValue |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function writeCommentBlock($token, $tab, $queryValue) |
67
|
|
|
{ |
68
|
|
|
if ($token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_BLOCK_COMMENT) { |
69
|
|
|
$indent = \str_repeat($tab, $this->indentation->getIndentLvl()); |
70
|
|
|
|
71
|
|
|
$this->formatter->appendToFormattedSql("\n".$indent); |
72
|
|
|
$queryValue = \str_replace("\n", "\n".$indent, $queryValue); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->formatter->appendToFormattedSql($queryValue); |
76
|
|
|
$this->newLine->setNewline(true); |
77
|
|
|
|
78
|
|
|
return $this->formatter->getFormattedSql(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|