|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 12/23/14 |
|
5
|
|
|
* Time: 1:26 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 UserDefined. |
|
17
|
|
|
*/ |
|
18
|
|
|
final class UserDefined |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param Tokenizer $tokenizer |
|
22
|
|
|
* @param string $string |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function isUserDefinedVariable(Tokenizer $tokenizer, $string) |
|
27
|
|
|
{ |
|
28
|
|
|
if (!$tokenizer->getNextToken() && self::isUserDefinedVariableString($string)) { |
|
29
|
|
|
$tokenizer->setNextToken(self::getUserDefinedVariableString($string)); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $string |
|
35
|
|
|
* |
|
36
|
|
|
* @return bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected static function isUserDefinedVariableString(&$string) |
|
39
|
|
|
{ |
|
40
|
|
|
return !empty($string[0]) && !empty($string[1]) && ($string[0] === '@' && isset($string[1])); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Gets the user defined variables for in quoted or non-quoted fashion. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $string |
|
47
|
|
|
* |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected static function getUserDefinedVariableString(&$string) |
|
51
|
|
|
{ |
|
52
|
|
|
$returnData = [ |
|
53
|
|
|
Tokenizer::TOKEN_VALUE => null, |
|
54
|
|
|
Tokenizer::TOKEN_TYPE => Tokenizer::TOKEN_TYPE_VARIABLE, |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
self::setTokenValueStartingWithAtSymbolAndWrapped($returnData, $string); |
|
58
|
|
|
self::setTokenValueStartingWithAtSymbol($returnData, $string); |
|
59
|
|
|
|
|
60
|
|
|
return $returnData; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $returnData |
|
65
|
|
|
* @param string $string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected static function setTokenValueStartingWithAtSymbolAndWrapped(array &$returnData, $string) |
|
68
|
|
|
{ |
|
69
|
|
|
if (!empty($string[1]) && ($string[1] === '"' || $string[1] === '\'' || $string[1] === '`')) { |
|
70
|
|
|
$returnData[Tokenizer::TOKEN_VALUE] = '@'.Quoted::wrapStringWithQuotes(\substr($string, 1)); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $returnData |
|
76
|
|
|
* @param string $string |
|
77
|
|
|
*/ |
|
78
|
|
|
protected static function setTokenValueStartingWithAtSymbol(array &$returnData, $string) |
|
79
|
|
|
{ |
|
80
|
|
|
if (null === $returnData[Tokenizer::TOKEN_VALUE]) { |
|
81
|
|
|
$matches = []; |
|
82
|
|
|
\preg_match('/^(@[a-zA-Z0-9\._\$]+)/', $string, $matches); |
|
83
|
|
|
if ($matches) { |
|
|
|
|
|
|
84
|
|
|
$returnData[Tokenizer::TOKEN_VALUE] = $matches[1]; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.