1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Mailcode |
4
|
|
|
* @subpackage Translator |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace Mailcode\Translator; |
10
|
|
|
|
11
|
|
|
use Mailcode\Mailcode_Commands_Command; |
12
|
|
|
use Mailcode\Mailcode_Exception; |
13
|
|
|
use Mailcode\Mailcode_Parser_Safeguard; |
14
|
|
|
use Mailcode\Mailcode_Translator_Exception; |
15
|
|
|
use Mailcode\Translator\Syntax\ApacheVelocitySyntax; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract base class for a translator syntax, allowing the translation |
19
|
|
|
* of mailcode commands into this syntax. |
20
|
|
|
* |
21
|
|
|
* @package Mailcode |
22
|
|
|
* @subpackage Translator |
23
|
|
|
* @author Sebastian Mordziol <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class BaseSyntax implements SyntaxInterface |
26
|
|
|
{ |
27
|
|
|
public const ERROR_UNKNOWN_COMMAND_TYPE = 50401; |
28
|
|
|
public const ERROR_INVALID_COMMAND_INSTANCE = 50402; |
29
|
|
|
|
30
|
|
|
public function translateCommand(Mailcode_Commands_Command $command) : string |
31
|
|
|
{ |
32
|
|
|
return $this->createTranslator($command)->translate($command); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
* @throws Mailcode_Translator_Exception |
38
|
|
|
*/ |
39
|
|
|
public function createTranslator(Mailcode_Commands_Command $command) : BaseCommandTranslation |
40
|
|
|
{ |
41
|
|
|
$class = sprintf( |
42
|
|
|
'Mailcode\Translator\Syntax\%s\%sTranslation', |
43
|
|
|
$this->getTypeID(), |
44
|
|
|
$command->getID() |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
if(!class_exists($class)) |
48
|
|
|
{ |
49
|
|
|
throw new Mailcode_Translator_Exception( |
50
|
|
|
sprintf('Unknown command %s in translator', $command->getID()), |
51
|
|
|
sprintf( |
52
|
|
|
'The class [%s] does not exist.', |
53
|
|
|
$class |
54
|
|
|
), |
55
|
|
|
self::ERROR_UNKNOWN_COMMAND_TYPE |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$translator = new $class($this); |
60
|
|
|
|
61
|
|
|
if($translator instanceof BaseCommandTranslation) |
62
|
|
|
{ |
63
|
|
|
return $translator; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
throw new Mailcode_Translator_Exception( |
67
|
|
|
'Invalid translator command instance.', |
68
|
|
|
sprintf( |
69
|
|
|
'The class [%s] does not extend the base translator command class.', |
70
|
|
|
get_class($translator) |
71
|
|
|
), |
72
|
|
|
self::ERROR_INVALID_COMMAND_INSTANCE |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
* @throws Mailcode_Exception |
79
|
|
|
* @throws Mailcode_Translator_Exception |
80
|
|
|
*/ |
81
|
|
|
public function translateSafeguard(Mailcode_Parser_Safeguard $safeguard) : string |
82
|
|
|
{ |
83
|
|
|
$subject = $safeguard->makeSafe(); |
84
|
|
|
|
85
|
|
|
if(!$safeguard->hasPlaceholders()) |
86
|
|
|
{ |
87
|
|
|
return $subject; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$placeholders = $safeguard->getPlaceholdersCollection()->getAll(); |
91
|
|
|
|
92
|
|
|
$replaces = array(); |
93
|
|
|
|
94
|
|
|
foreach($placeholders as $placeholder) |
95
|
|
|
{ |
96
|
|
|
$command = $placeholder->getCommand(); |
97
|
|
|
|
98
|
|
|
$replaces[$placeholder->getReplacementText()] = $this->translateCommand($command); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return str_replace(array_keys($replaces), array_values($replaces), $subject); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|