1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Mailcode; |
6
|
|
|
|
7
|
|
|
class Mailcode_Parser_Statement_Info_Variables |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Mailcode_Parser_Statement_Info |
11
|
|
|
*/ |
12
|
|
|
private $info; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer |
16
|
|
|
*/ |
17
|
|
|
private $tokenizer; |
18
|
|
|
|
19
|
|
|
public function __construct(Mailcode_Parser_Statement_Info $info, Mailcode_Parser_Statement_Tokenizer $tokenizer) |
20
|
|
|
{ |
21
|
|
|
$this->info = $info; |
22
|
|
|
$this->tokenizer = $tokenizer; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Whether the whole statement is a variable being assigned a value. |
27
|
|
|
* |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
public function isAssignment(): bool |
31
|
|
|
{ |
32
|
|
|
$variable = $this->getByIndex(0); |
33
|
|
|
$operand = $this->info->getOperandByIndex(1); |
34
|
|
|
$value = $this->info->getTokenByIndex(2); |
35
|
|
|
|
36
|
|
|
if ($variable && $operand && $value && $operand->isAssignment()) { |
37
|
|
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Whether the whole statement is a variable being compared to something. |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function isComparison(): bool |
49
|
|
|
{ |
50
|
|
|
$variable = $this->getByIndex(0); |
51
|
|
|
$operand = $this->info->getOperandByIndex(1); |
52
|
|
|
$value = $this->info->getTokenByIndex(2); |
53
|
|
|
|
54
|
|
|
if ($variable && $operand && $value && $operand->isComparator()) { |
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retrieves all variables used in the statement. |
63
|
|
|
* |
64
|
|
|
* @return Mailcode_Variables_Variable[] |
65
|
|
|
* @throws Mailcode_Parser_Exception |
66
|
|
|
*/ |
67
|
|
|
public function getAll(): array |
68
|
|
|
{ |
69
|
|
|
$result = array(); |
70
|
|
|
$tokens = $this->tokenizer->getTokens(); |
71
|
|
|
|
72
|
|
|
foreach ($tokens as $token) { |
73
|
|
|
if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) { |
74
|
|
|
$result[] = $token->getVariable(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Variable[] |
83
|
|
|
*/ |
84
|
|
|
public function getTokens(): array |
85
|
|
|
{ |
86
|
|
|
$result = array(); |
87
|
|
|
$tokens = $this->tokenizer->getTokens(); |
88
|
|
|
|
89
|
|
|
foreach ($tokens as $token) { |
90
|
|
|
if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) { |
91
|
|
|
$result[] = $token; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieves a variable by its position in the command's parameters. |
100
|
|
|
* Returns null if there is no parameter at the specified index, or |
101
|
|
|
* if it is of another type. |
102
|
|
|
* |
103
|
|
|
* @param int $index Zero-based index. |
104
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL |
105
|
|
|
*/ |
106
|
|
|
public function getByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token_Variable |
107
|
|
|
{ |
108
|
|
|
$token = $this->info->getTokenByIndex($index); |
109
|
|
|
|
110
|
|
|
if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) { |
111
|
|
|
return $token; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|