1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Mailcode; |
6
|
|
|
|
7
|
|
|
class Mailcode_Parser_Safeguard_PlaceholderCollection |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Mailcode_Parser_Safeguard_Placeholder[] |
11
|
|
|
*/ |
12
|
|
|
private $placeholders; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string[]|NULL |
16
|
|
|
*/ |
17
|
|
|
protected $placeholderStrings; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Mailcode_Parser_Safeguard_Placeholder[] $placeholders |
21
|
|
|
*/ |
22
|
|
|
public function __construct(array $placeholders) |
23
|
|
|
{ |
24
|
|
|
$this->placeholders = array_values($placeholders); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return Mailcode_Parser_Safeguard_Placeholder[] |
29
|
|
|
*/ |
30
|
|
|
public function getAll() : array |
31
|
|
|
{ |
32
|
|
|
return $this->placeholders; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param int $index Zero-based index of the command |
37
|
|
|
* @return Mailcode_Parser_Safeguard_Placeholder |
38
|
|
|
* @throws Mailcode_Exception |
39
|
|
|
*/ |
40
|
|
|
public function getByIndex(int $index) : Mailcode_Parser_Safeguard_Placeholder |
41
|
|
|
{ |
42
|
|
|
if(isset($this->placeholders[$index])) |
43
|
|
|
{ |
44
|
|
|
return $this->placeholders[$index]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
throw new Mailcode_Exception( |
48
|
|
|
'Cannot get first placeholder, no placeholders found.', |
49
|
|
|
'', |
50
|
|
|
Mailcode_Parser_Safeguard::ERROR_NO_FIRST_PLACEHOLDER |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getByCommand(Mailcode_Commands_Command $command) : Mailcode_Parser_Safeguard_Placeholder |
55
|
|
|
{ |
56
|
|
|
foreach($this->placeholders as $placeholder) |
57
|
|
|
{ |
58
|
|
|
if($placeholder->getCommand() === $command) |
59
|
|
|
{ |
60
|
|
|
return $placeholder; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
throw new Mailcode_Exception( |
65
|
|
|
'Placeholder not found by command.', |
66
|
|
|
'None of the placeholders have the specified command.', |
67
|
|
|
Mailcode_Parser_Safeguard::ERROR_NO_PLACEHOLDER_FOR_COMMAND |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retrieves a placeholder instance by its ID. |
73
|
|
|
* |
74
|
|
|
* @param int $id |
75
|
|
|
* @throws Mailcode_Exception If the placeholder was not found. |
76
|
|
|
* @return Mailcode_Parser_Safeguard_Placeholder |
77
|
|
|
*/ |
78
|
|
|
public function getByID(int $id) : Mailcode_Parser_Safeguard_Placeholder |
79
|
|
|
{ |
80
|
|
|
foreach($this->placeholders as $placeholder) |
81
|
|
|
{ |
82
|
|
|
if($placeholder->getID() === $id) |
83
|
|
|
{ |
84
|
|
|
return $placeholder; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
throw new Mailcode_Exception( |
89
|
|
|
'No such safeguard placeholder.', |
90
|
|
|
sprintf( |
91
|
|
|
'The placeholder ID [%s] is not present in the safeguard instance.', |
92
|
|
|
$id |
93
|
|
|
), |
94
|
|
|
Mailcode_Parser_Safeguard::ERROR_PLACEHOLDER_NOT_FOUND |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieves a placeholder instance by its replacement text. |
100
|
|
|
* |
101
|
|
|
* @param string $string |
102
|
|
|
* @throws Mailcode_Exception |
103
|
|
|
* @return Mailcode_Parser_Safeguard_Placeholder |
104
|
|
|
*/ |
105
|
|
|
public function getByString(string $string) : Mailcode_Parser_Safeguard_Placeholder |
106
|
|
|
{ |
107
|
|
|
foreach($this->placeholders as $placeholder) |
108
|
|
|
{ |
109
|
|
|
if($placeholder->getReplacementText() === $string) |
110
|
|
|
{ |
111
|
|
|
return $placeholder; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
throw new Mailcode_Exception( |
116
|
|
|
'No such safeguard placeholder.', |
117
|
|
|
sprintf( |
118
|
|
|
'The placeholder replacement string [%s] is not present in the safeguard instance.', |
119
|
|
|
$string |
120
|
|
|
), |
121
|
|
|
Mailcode_Parser_Safeguard::ERROR_PLACEHOLDER_NOT_FOUND |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Retrieves a list of all placeholder IDs used in the text. |
127
|
|
|
* |
128
|
|
|
* @return string[] |
129
|
|
|
*/ |
130
|
|
|
public function getStrings() : array |
131
|
|
|
{ |
132
|
|
|
if(is_array($this->placeholderStrings)) |
133
|
|
|
{ |
134
|
|
|
return $this->placeholderStrings; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->placeholderStrings = array(); |
138
|
|
|
|
139
|
|
|
foreach($this->placeholders as $placeholder) |
140
|
|
|
{ |
141
|
|
|
$this->placeholderStrings[] = $placeholder->getReplacementText(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->placeholderStrings; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function hasPlaceholders() : bool |
148
|
|
|
{ |
149
|
|
|
return $this->countPlaceholders() > 0; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function isStringPlaceholder(string $subject) : bool |
153
|
|
|
{ |
154
|
|
|
$ids = $this->getStrings(); |
155
|
|
|
|
156
|
|
|
return in_array($subject, $ids, true); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function countPlaceholders() : int |
160
|
|
|
{ |
161
|
|
|
return count($this->placeholders); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function getFirst() : Mailcode_Parser_Safeguard_Placeholder |
165
|
|
|
{ |
166
|
|
|
return $this->getByIndex(0); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|