1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Translator_Syntax} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Translator |
7
|
|
|
* @see Mailcode_Translator_Syntax |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Abstract base class for a translator syntax, allowing the translation |
16
|
|
|
* of mailcode commands into this syntax. |
17
|
|
|
* |
18
|
|
|
* @package Mailcode |
19
|
|
|
* @subpackage Translator |
20
|
|
|
* @author Sebastian Mordziol <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Mailcode_Translator_Syntax |
23
|
|
|
{ |
24
|
|
|
const ERROR_UNKNOWN_COMMAND_TYPE = 50401; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $typeID; |
30
|
|
|
|
31
|
|
|
public function __construct(string $typeID) |
32
|
|
|
{ |
33
|
|
|
$this->typeID = $typeID; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Retrieves the syntax' type ID, e.g. "ApacheVelocity". |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getTypeID() : string |
41
|
|
|
{ |
42
|
|
|
return $this->typeID; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Translates a single command to the target syntax. |
47
|
|
|
* |
48
|
|
|
* @param Mailcode_Commands_Command $command |
49
|
|
|
* @throws Mailcode_Translator_Exception |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function translateCommand(Mailcode_Commands_Command $command) : string |
53
|
|
|
{ |
54
|
|
|
$translator = $this->createTranslator($command); |
55
|
|
|
|
56
|
|
|
return $translator->translate($command); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function createTranslator(Mailcode_Commands_Command $command) : Mailcode_Translator_Command |
60
|
|
|
{ |
61
|
|
|
$class = sprintf( |
62
|
|
|
'Mailcode\Mailcode_Translator_Syntax_%s_%s', |
63
|
|
|
$this->getTypeID(), |
64
|
|
|
$command->getID() |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
if(!class_exists($class)) |
68
|
|
|
{ |
69
|
|
|
throw new Mailcode_Translator_Exception( |
70
|
|
|
sprintf('Unknown command %s in translator', $command->getID()), |
71
|
|
|
sprintf( |
72
|
|
|
'The class [%s] does not exist.', |
73
|
|
|
$class |
74
|
|
|
), |
75
|
|
|
self::ERROR_UNKNOWN_COMMAND_TYPE |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$translator = new $class($command); |
80
|
|
|
|
81
|
|
|
return $translator; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Translates all safeguarded commands in the subject string to the |
86
|
|
|
* target syntax in one go. |
87
|
|
|
* |
88
|
|
|
* @param Mailcode_Parser_Safeguard $safeguard |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function translateSafeguard(Mailcode_Parser_Safeguard $safeguard) : string |
92
|
|
|
{ |
93
|
|
|
$subject = $safeguard->makeSafe(); |
94
|
|
|
|
95
|
|
|
if(!$safeguard->hasPlaceholders()) |
96
|
|
|
{ |
97
|
|
|
return $subject; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$placeholders = $safeguard->getPlaceholders(); |
101
|
|
|
|
102
|
|
|
$replaces = array(); |
103
|
|
|
|
104
|
|
|
foreach($placeholders as $placeholder) |
105
|
|
|
{ |
106
|
|
|
$replaces[$placeholder->getReplacementText()] = $this->translateCommand($placeholder->getCommand()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return str_replace(array_keys($replaces), array_values($replaces), $subject); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|