|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the trait {@see \Mailcode\Mailcode_Traits_Commands_ProtectedContent}. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Commands |
|
7
|
|
|
* @see \Mailcode\Mailcode_Traits_Commands_ProtectedContent |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode; |
|
13
|
|
|
|
|
14
|
|
|
use Mailcode\Parser\PreParser; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @package Mailcode |
|
18
|
|
|
* @subpackage Commands |
|
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @see Mailcode_Interfaces_Commands_ProtectedContent |
|
22
|
|
|
*/ |
|
23
|
|
|
trait Mailcode_Traits_Commands_ProtectedContent |
|
24
|
|
|
{ |
|
25
|
|
|
protected string $content = ''; |
|
26
|
|
|
private ?Mailcode_Collection $nestedMailcode = null; |
|
27
|
|
|
protected ?Mailcode_Parser_Statement_Tokenizer_Token_Number $contentIDToken = null; |
|
28
|
|
|
|
|
29
|
|
|
public function getContent() : string |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->content; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getContentTrimmed() : string |
|
35
|
|
|
{ |
|
36
|
|
|
return trim($this->content); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getContentIDToken() : ?Mailcode_Parser_Statement_Tokenizer_Token_Number |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->contentIDToken; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function validateSyntax_content_id() : void |
|
45
|
|
|
{ |
|
46
|
|
|
$contentIDToken = $this->requireParams() |
|
|
|
|
|
|
47
|
|
|
->getInfo() |
|
48
|
|
|
->getTokenByIndex(0); |
|
49
|
|
|
|
|
50
|
|
|
if($contentIDToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Number) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->contentIDToken = $contentIDToken; |
|
53
|
|
|
$this->loadContent(); |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->validationResult->makeError( |
|
58
|
|
|
t('The content ID parameter is missing.'), |
|
59
|
|
|
Mailcode_Interfaces_Commands_ProtectedContent::VALIDATION_ERROR_CONTENT_ID_MISSING |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getNestedMailcode() : Mailcode_Collection |
|
64
|
|
|
{ |
|
65
|
|
|
if(isset($this->nestedMailcode)) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->nestedMailcode; |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if($this->isMailcodeEnabled()) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$collection = Mailcode::create()->parseString($this->getContent()); |
|
73
|
|
|
} |
|
74
|
|
|
else |
|
75
|
|
|
{ |
|
76
|
|
|
$collection = new Mailcode_Collection(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->nestedMailcode = $collection; |
|
80
|
|
|
|
|
81
|
|
|
return $collection; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function validateSyntax_nested_mailcode() : void |
|
85
|
|
|
{ |
|
86
|
|
|
$collection = $this->getNestedMailcode(); |
|
87
|
|
|
|
|
88
|
|
|
if($collection->isValid()) |
|
89
|
|
|
{ |
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$errors = $collection->getErrors(); |
|
94
|
|
|
|
|
95
|
|
|
foreach($errors as $error) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->getValidationResult()->makeError( |
|
|
|
|
|
|
98
|
|
|
$error->getMessage(), |
|
99
|
|
|
$error->getCode() |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getContentID() : int |
|
105
|
|
|
{ |
|
106
|
|
|
if(isset($this->contentIDToken)) |
|
107
|
|
|
{ |
|
108
|
|
|
return (int)$this->contentIDToken->getValue(); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
throw new Mailcode_Exception( |
|
112
|
|
|
'No content ID set', |
|
113
|
|
|
'', |
|
114
|
|
|
Mailcode_Interfaces_Commands_ProtectedContent::ERROR_NO_CONTENT_ID_TOKEN |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function loadContent() : void |
|
119
|
|
|
{ |
|
120
|
|
|
$contentID = $this->getContentID(); |
|
121
|
|
|
|
|
122
|
|
|
$this->content = PreParser::getContent($contentID); |
|
123
|
|
|
|
|
124
|
|
|
PreParser::clearContent($contentID); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function getNormalized() : string |
|
128
|
|
|
{ |
|
129
|
|
|
return (new Mailcode_Commands_Normalizer_ProtectedContent($this))->normalize(); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function getVariables() : Mailcode_Variables_Collection_Regular |
|
133
|
|
|
{ |
|
134
|
|
|
$variables = parent::getVariables(); |
|
135
|
|
|
|
|
136
|
|
|
if($this->isMailcodeEnabled()) |
|
137
|
|
|
{ |
|
138
|
|
|
$nested = $this->getNestedMailcode() |
|
139
|
|
|
->getVariables() |
|
140
|
|
|
->getAll(); |
|
141
|
|
|
|
|
142
|
|
|
foreach($nested as $variable) |
|
143
|
|
|
{ |
|
144
|
|
|
$variables->add($variable); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $variables; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|