1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Mailcode; |
6
|
|
|
|
7
|
|
|
class Mailcode_Parser_Statement_Info_Keywords |
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
|
|
|
* Retrieves a keyword by its position in the command's parameters. |
27
|
|
|
* Returns null if there is no parameter at the specified index, or |
28
|
|
|
* if it is of another type. |
29
|
|
|
* |
30
|
|
|
* @param int $index Zero-based index. |
31
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword|NULL |
32
|
|
|
*/ |
33
|
|
|
public function getByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Keyword |
34
|
|
|
{ |
35
|
|
|
$token = $this->info->getTokenByIndex($index); |
36
|
|
|
|
37
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Keyword) |
38
|
|
|
{ |
39
|
|
|
return $token; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword[] |
47
|
|
|
*/ |
48
|
|
|
public function getAll() : array |
49
|
|
|
{ |
50
|
|
|
$result = array(); |
51
|
|
|
$tokens = $this->info->getTokens(); |
52
|
|
|
|
53
|
|
|
foreach($tokens as $token) |
54
|
|
|
{ |
55
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Keyword) |
56
|
|
|
{ |
57
|
|
|
$result[] = $token; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $result; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Adds or removes a keyword depending on whether it should be enabled. |
66
|
|
|
* |
67
|
|
|
* @param string $keyword The keyword name, with or without : |
68
|
|
|
* @param bool $enabled |
69
|
|
|
* @return Mailcode_Parser_Statement_Info_Keywords |
70
|
|
|
* @throws Mailcode_Exception |
71
|
|
|
*/ |
72
|
|
|
public function setEnabled(string $keyword, bool $enabled) : Mailcode_Parser_Statement_Info_Keywords |
73
|
|
|
{ |
74
|
|
|
if($enabled) |
75
|
|
|
{ |
76
|
|
|
return $this->add($keyword); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->remove($keyword); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Adds a keyword to the command. |
84
|
|
|
* |
85
|
|
|
* @param string $keyword Keyword name, with or without : |
86
|
|
|
* @return $this |
87
|
|
|
* @throws Mailcode_Exception |
88
|
|
|
*/ |
89
|
|
|
public function add(string $keyword) : Mailcode_Parser_Statement_Info_Keywords |
90
|
|
|
{ |
91
|
|
|
$keyword = rtrim($keyword, ':').':'; |
92
|
|
|
|
93
|
|
|
if(!$this->hasKeyword($keyword)) |
94
|
|
|
{ |
95
|
|
|
$this->tokenizer->appendKeyword($keyword); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Removes a keyword from the command, if it has one. |
103
|
|
|
* Has no effect otherwise. |
104
|
|
|
* |
105
|
|
|
* @param string $keyword Keyword name, with or without : |
106
|
|
|
* @return Mailcode_Parser_Statement_Info_Keywords |
107
|
|
|
*/ |
108
|
|
|
public function remove(string $keyword) : Mailcode_Parser_Statement_Info_Keywords |
109
|
|
|
{ |
110
|
|
|
$keyword = rtrim($keyword, ':').':'; |
111
|
|
|
$keywords = $this->getAll(); |
112
|
|
|
|
113
|
|
|
foreach ($keywords as $kw) |
114
|
|
|
{ |
115
|
|
|
if ($kw->getKeyword() !== $keyword) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->tokenizer->removeToken($kw); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Whether the command has the specified keyword. |
127
|
|
|
* |
128
|
|
|
* @param string $keyword Keyword name, with or without : |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function hasKeyword(string $keyword) : bool |
132
|
|
|
{ |
133
|
|
|
$keyword = rtrim($keyword, ':').':'; |
134
|
|
|
$keywords = $this->getAll(); |
135
|
|
|
|
136
|
|
|
foreach ($keywords as $kw) |
137
|
|
|
{ |
138
|
|
|
if($kw->getKeyword() === $keyword) |
139
|
|
|
{ |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|