1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Variables_Collection} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Variables |
7
|
|
|
* @see Mailcode_Variables_Collection |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Handler for all variable-related tasks. |
16
|
|
|
* |
17
|
|
|
* @package Mailcode |
18
|
|
|
* @subpackage Variables |
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class Mailcode_Variables_Collection |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Mailcode_Variables_Variable[] |
25
|
|
|
*/ |
26
|
|
|
protected $variables = array(); |
27
|
|
|
|
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->init(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function init() : void |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function add(Mailcode_Variables_Variable $variable) : Mailcode_Variables_Collection |
39
|
|
|
{ |
40
|
|
|
$this->variables[] = $variable; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function hasVariables() : bool |
46
|
|
|
{ |
47
|
|
|
return !empty($this->variables); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function countVariables() : int |
51
|
|
|
{ |
52
|
|
|
return count($this->variables); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Checks whether the collection contains a variable with the specified name. |
57
|
|
|
* |
58
|
|
|
* @param string $fullName The variable name, with or without $ sign. |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function hasVariableName(string $fullName) : bool |
62
|
|
|
{ |
63
|
|
|
$fullName = $this->fixName($fullName); |
64
|
|
|
|
65
|
|
|
foreach($this->variables as $variable) |
66
|
|
|
{ |
67
|
|
|
if($variable->getFullName() === $fullName) |
68
|
|
|
{ |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Retrieves a collection of all variable instances for |
78
|
|
|
* the specified name (there can be several with differing |
79
|
|
|
* matched texts because of spacing). |
80
|
|
|
* |
81
|
|
|
* @param string $fullName |
82
|
|
|
* @return Mailcode_Variables_Collection |
83
|
|
|
*/ |
84
|
|
|
public function getByFullName(string $fullName) : Mailcode_Variables_Collection |
85
|
|
|
{ |
86
|
|
|
$fullName = $this->fixName($fullName); |
87
|
|
|
|
88
|
|
|
$collection = new Mailcode_Variables_Collection_Regular(); |
89
|
|
|
|
90
|
|
|
foreach($this->variables as $variable) |
91
|
|
|
{ |
92
|
|
|
if($variable->getFullName() === $fullName) |
93
|
|
|
{ |
94
|
|
|
$collection->add($variable); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $collection; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Prepends the $ sign to a variable name if it does not have it. |
103
|
|
|
* |
104
|
|
|
* @param string $fullName |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function fixName(string $fullName) : string |
108
|
|
|
{ |
109
|
|
|
if(substr($fullName, 0, 1) === '$') |
110
|
|
|
{ |
111
|
|
|
return $fullName; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return '$'.$fullName; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Retrieves all variables, grouped by their hash - meaning |
119
|
|
|
* unique matched strings. |
120
|
|
|
* |
121
|
|
|
* @return Mailcode_Variables_Variable[] |
122
|
|
|
*/ |
123
|
|
|
public function getGroupedByHash() |
124
|
|
|
{ |
125
|
|
|
$entries = array(); |
126
|
|
|
|
127
|
|
|
foreach($this->variables as $variable) |
128
|
|
|
{ |
129
|
|
|
$entries[$variable->getHash()] = $variable; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->sortVariables($entries); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Retrieves all variables, grouped by their name. |
137
|
|
|
* |
138
|
|
|
* @return Mailcode_Variables_Variable[] |
139
|
|
|
*/ |
140
|
|
|
public function getGroupedByName() |
141
|
|
|
{ |
142
|
|
|
$entries = array(); |
143
|
|
|
|
144
|
|
|
foreach($this->variables as $variable) |
145
|
|
|
{ |
146
|
|
|
$entries[$variable->getFullName()] = $variable; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this->sortVariables($entries); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Retrieves all variables, in the order they were addded. |
154
|
|
|
* @return \Mailcode\Mailcode_Variables_Variable[] |
155
|
|
|
*/ |
156
|
|
|
public function getAll() |
157
|
|
|
{ |
158
|
|
|
return $this->variables; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Retrieves the full names of the variables that are present in the collection. |
163
|
|
|
* @return string[] |
164
|
|
|
*/ |
165
|
|
|
public function getNames() : array |
166
|
|
|
{ |
167
|
|
|
$result = array(); |
168
|
|
|
|
169
|
|
|
foreach($this->variables as $variable) |
170
|
|
|
{ |
171
|
|
|
$name = $variable->getFullName(); |
172
|
|
|
|
173
|
|
|
if(!in_array($name, $result)) |
174
|
|
|
{ |
175
|
|
|
$result[] = $name; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $result; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Takes a list of variables and sorts them, throwing away |
184
|
|
|
* the source array's keys. |
185
|
|
|
* |
186
|
|
|
* @param Mailcode_Variables_Variable[] $entries |
187
|
|
|
* @return Mailcode_Variables_Variable[] |
188
|
|
|
*/ |
189
|
|
|
protected function sortVariables(array $entries) |
190
|
|
|
{ |
191
|
|
|
$result = array_values($entries); |
192
|
|
|
|
193
|
|
|
usort($entries, function(Mailcode_Variables_Variable $a, Mailcode_Variables_Variable $b) |
194
|
|
|
{ |
195
|
|
|
return strnatcasecmp($a->getFullName(), $b->getFullName()); |
196
|
|
|
}); |
197
|
|
|
|
198
|
|
|
return $result; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Merges the variables collection with the target collection |
203
|
|
|
* by inheriting all that collection's variables. |
204
|
|
|
* |
205
|
|
|
* @param Mailcode_Variables_Collection $collection |
206
|
|
|
* @return Mailcode_Variables_Collection |
207
|
|
|
*/ |
208
|
|
|
public function mergeWith(Mailcode_Variables_Collection $collection) : Mailcode_Variables_Collection |
209
|
|
|
{ |
210
|
|
|
$variables = $collection->getGroupedByHash(); |
211
|
|
|
|
212
|
|
|
foreach($variables as $variable) |
213
|
|
|
{ |
214
|
|
|
$this->add($variable); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function getFirst() : ?Mailcode_Variables_Variable |
221
|
|
|
{ |
222
|
|
|
$variables = $this->getAll(); |
223
|
|
|
|
224
|
|
|
if(!empty($variables)) |
225
|
|
|
{ |
226
|
|
|
return array_shift($variables); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return null; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|