|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Mailcode |
|
4
|
|
|
* @subpackage Translator |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace Mailcode; |
|
10
|
|
|
|
|
11
|
|
|
use AppUtils\ClassHelper; |
|
12
|
|
|
use Mailcode\Translator\Syntax\ApacheVelocitySyntax; |
|
13
|
|
|
use Mailcode\Translator\Syntax\HubLSyntax; |
|
14
|
|
|
use Mailcode\Translator\SyntaxInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Used to translate mailcode syntax to other syntaxes. |
|
18
|
|
|
* |
|
19
|
|
|
* @package Mailcode |
|
20
|
|
|
* @subpackage Translator |
|
21
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Mailcode_Translator |
|
24
|
|
|
{ |
|
25
|
|
|
public const ERROR_INVALID_SYNTAX_NAME = 73001; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array<string,SyntaxInterface> |
|
29
|
|
|
*/ |
|
30
|
|
|
private array $syntaxes = array(); |
|
31
|
|
|
|
|
32
|
|
|
private static ?Mailcode_Translator $instance = null; |
|
33
|
|
|
|
|
34
|
|
|
public static function create() : Mailcode_Translator |
|
35
|
|
|
{ |
|
36
|
|
|
if(is_null(self::$instance)) { |
|
37
|
|
|
self::$instance = new self(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return self::$instance; |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function __construct() |
|
44
|
|
|
{ |
|
45
|
|
|
foreach($this->resolveSyntaxClasses() as $class) { |
|
46
|
|
|
$syntax = new $class(); |
|
47
|
|
|
$this->syntaxes[$syntax->getTypeID()] = $syntax; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Creates an instance of the specified syntax. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $name The name of the syntax, e.g. {@see ApacheVelocitySyntax::SYNTAX_NAME}. |
|
55
|
|
|
* @return SyntaxInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function createSyntax(string $name) : SyntaxInterface |
|
58
|
|
|
{ |
|
59
|
|
|
if(isset($this->syntaxes[$name])) { |
|
60
|
|
|
return $this->syntaxes[$name]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
throw new Mailcode_Exception( |
|
64
|
|
|
'Invalid translation syntax', |
|
65
|
|
|
sprintf( |
|
66
|
|
|
'The syntax [%s] does not exist. Possible values are: [%s].', |
|
67
|
|
|
$name, |
|
68
|
|
|
implode(', ', $this->getSyntaxNames()) |
|
69
|
|
|
), |
|
70
|
|
|
self::ERROR_INVALID_SYNTAX_NAME |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function createApacheVelocity() : ApacheVelocitySyntax |
|
75
|
|
|
{ |
|
76
|
|
|
return ClassHelper::requireObjectInstanceOf( |
|
77
|
|
|
ApacheVelocitySyntax::class, |
|
78
|
|
|
$this->createSyntax(ApacheVelocitySyntax::SYNTAX_NAME) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function createHubL() : HubLSyntax |
|
83
|
|
|
{ |
|
84
|
|
|
return ClassHelper::requireObjectInstanceOf( |
|
85
|
|
|
HubLSyntax::class, |
|
86
|
|
|
$this->createSyntax(HubLSyntax::SYNTAX_NAME) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Retrieves an instance for each syntax available |
|
92
|
|
|
* in the system. |
|
93
|
|
|
* |
|
94
|
|
|
* @return SyntaxInterface[] |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getSyntaxes() : array |
|
97
|
|
|
{ |
|
98
|
|
|
return array_values($this->syntaxes); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Retrieves a list of names for all syntaxes supported |
|
103
|
|
|
* by the system. |
|
104
|
|
|
* |
|
105
|
|
|
* @return string[] |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getSyntaxNames() : array |
|
108
|
|
|
{ |
|
109
|
|
|
return array_keys($this->syntaxes); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return class-string<SyntaxInterface>[] |
|
|
|
|
|
|
114
|
|
|
*/ |
|
115
|
|
|
private function resolveSyntaxClasses() : array |
|
116
|
|
|
{ |
|
117
|
|
|
return ClassCache::findClassesInFolder( |
|
118
|
|
|
__DIR__.'/Translator/Syntax', |
|
119
|
|
|
false, |
|
120
|
|
|
SyntaxInterface::class |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Checks whether the specified syntax exists. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $name The syntax name, e.g. {@see ApacheVelocitySyntax::SYNTAX_NAME}. |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
|
|
public function syntaxExists(string $name) : bool |
|
131
|
|
|
{ |
|
132
|
|
|
return isset($this->syntaxes[$name]); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|