1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Collection} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Collection |
7
|
|
|
* @see Mailcode_Collection |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Commands collection: container for commands. |
16
|
|
|
* |
17
|
|
|
* @package Mailcode |
18
|
|
|
* @subpackage Collection |
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Mailcode_Collection |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Mailcode_Commands_Command[] |
25
|
|
|
*/ |
26
|
|
|
protected $commands = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Mailcode_Collection_Error[] |
30
|
|
|
*/ |
31
|
|
|
protected $errors = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Adds a command to the collection. |
35
|
|
|
* |
36
|
|
|
* @param Mailcode_Commands_Command $command |
37
|
|
|
* @return Mailcode_Collection |
38
|
|
|
*/ |
39
|
|
|
public function addCommand(Mailcode_Commands_Command $command) : Mailcode_Collection |
40
|
|
|
{ |
41
|
|
|
$hash = $command->getHash(); |
42
|
|
|
|
43
|
|
|
if(!isset($this->commands[$hash])) { |
44
|
|
|
$this->commands[$hash] = $command; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Whether there are any commands in the collection. |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function hasCommands() : bool |
56
|
|
|
{ |
57
|
|
|
return !empty($this->commands); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Counts the amount of commands in the collection. |
62
|
|
|
* |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
|
|
public function countCommands() : int |
66
|
|
|
{ |
67
|
|
|
return count($this->commands); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function addErrorMessage(string $matchedText, string $message, int $code) : void |
71
|
|
|
{ |
72
|
|
|
$this->errors[] = new Mailcode_Collection_Error_Message( |
73
|
|
|
$matchedText, |
74
|
|
|
$code, |
75
|
|
|
$message |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function addInvalidCommand(Mailcode_Commands_Command $command) : void |
80
|
|
|
{ |
81
|
|
|
$this->errors[] = new Mailcode_Collection_Error_Command($command); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return Mailcode_Collection_Error[] |
86
|
|
|
*/ |
87
|
|
|
public function getErrors() |
88
|
|
|
{ |
89
|
|
|
return $this->errors; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function isValid() : bool |
93
|
|
|
{ |
94
|
|
|
return empty($this->errors); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Retrieves all commands that were detected. |
99
|
|
|
* |
100
|
|
|
* @return \Mailcode\Mailcode_Commands_Command[] |
101
|
|
|
*/ |
102
|
|
|
public function getCommands() |
103
|
|
|
{ |
104
|
|
|
return $this->commands; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function addCommands(array $commands) : Mailcode_Collection |
108
|
|
|
{ |
109
|
|
|
foreach($commands as $command) |
110
|
|
|
{ |
111
|
|
|
$this->addCommand($command); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function mergeWith(Mailcode_Collection $collection) : Mailcode_Collection |
118
|
|
|
{ |
119
|
|
|
$this->addCommands($collection->getCommands()); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getVariables() : Mailcode_Variables_Collection |
125
|
|
|
{ |
126
|
|
|
$collection = new Mailcode_Variables_Collection_Regular(); |
127
|
|
|
|
128
|
|
|
foreach($this->commands as $command) |
129
|
|
|
{ |
130
|
|
|
$collection->mergeWith($command->getVariables()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $collection; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|