1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 12/23/14 |
5
|
|
|
* Time: 1:22 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\Tokenizer\Parser; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Sql\QueryFormatter\Tokenizer\Tokenizer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Comment. |
17
|
|
|
*/ |
18
|
|
|
final class Comment |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param Tokenizer $tokenizer |
22
|
|
|
* @param string $string |
23
|
|
|
*/ |
24
|
|
|
public static function isComment(Tokenizer $tokenizer, $string) |
25
|
|
|
{ |
26
|
|
|
if (!$tokenizer->getNextToken() && self::isCommentString($string)) { |
27
|
|
|
$tokenizer->setNextToken(self::getCommentString($string)); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $string |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
protected static function isCommentString($string) |
37
|
|
|
{ |
38
|
|
|
return !empty($string[0]) && ($string[0] === '#' || self::isTwoCharacterComment($string)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $string |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
protected static function isTwoCharacterComment($string) |
47
|
|
|
{ |
48
|
|
|
return !empty($string[1]) && (isset($string[1]) && (self::startsWithDoubleDash($string) || self::startsAsBlock($string))); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $string |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
protected static function startsWithDoubleDash($string) |
57
|
|
|
{ |
58
|
|
|
return !empty($string[1]) && ($string[0] === '-' && ($string[1] === $string[0])); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $string |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
protected static function startsAsBlock($string) |
67
|
|
|
{ |
68
|
|
|
return !empty($string[1]) && ($string[0] === '/' && $string[1] === '*'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $string |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected static function getCommentString($string) |
77
|
|
|
{ |
78
|
|
|
$last = \strpos($string, '*/', 2) + 2; |
79
|
|
|
$type = Tokenizer::TOKEN_TYPE_BLOCK_COMMENT; |
80
|
|
|
|
81
|
|
|
if (!empty($string[0]) && ($string[0] === '-' || $string[0] === '#')) { |
82
|
|
|
$last = \strpos($string, "\n"); |
83
|
|
|
$type = Tokenizer::TOKEN_TYPE_COMMENT; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$last = ($last === false) ? \strlen($string) : $last; |
87
|
|
|
|
88
|
|
|
return [ |
89
|
|
|
Tokenizer::TOKEN_VALUE => \substr($string, 0, $last), |
90
|
|
|
Tokenizer::TOKEN_TYPE => $type, |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|