1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see \Mailcode\Mailcode_Translator} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Translator |
7
|
|
|
* @see \Mailcode\Mailcode_Translator |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
use AppUtils\ClassHelper; |
15
|
|
|
use AppUtils\FileHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Used to translate mailcode syntax to other syntaxes. |
19
|
|
|
* |
20
|
|
|
* @package Mailcode |
21
|
|
|
* @subpackage Translator |
22
|
|
|
* @author Sebastian Mordziol <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Mailcode_Translator |
25
|
|
|
{ |
26
|
|
|
public const ERROR_INVALID_SYNTAX_NAME = 73001; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Creates an instance of the specified syntax. |
30
|
|
|
* |
31
|
|
|
* @param string $name The name of the syntax, e.g. "ApacheVelocity" |
32
|
|
|
* @return Mailcode_Translator_Syntax |
33
|
|
|
*/ |
34
|
|
|
public function createSyntax(string $name) : Mailcode_Translator_Syntax |
35
|
|
|
{ |
36
|
|
|
if($this->syntaxExists($name)) |
37
|
|
|
{ |
38
|
|
|
return new Mailcode_Translator_Syntax($name); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
throw new Mailcode_Exception( |
42
|
|
|
'Invalid translation syntax', |
43
|
|
|
sprintf( |
44
|
|
|
'The syntax [%s] does not exist. Possible values are: [%s].', |
45
|
|
|
$name, |
46
|
|
|
implode(', ', $this->getSyntaxNames()) |
47
|
|
|
), |
48
|
|
|
self::ERROR_INVALID_SYNTAX_NAME |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function createApacheVelocity() : Mailcode_Translator_Syntax |
53
|
|
|
{ |
54
|
|
|
return $this->createSyntax(ClassHelper::getClassTypeName(Mailcode_Translator_Syntax_ApacheVelocity::class)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retrieves an instance for each syntax available |
59
|
|
|
* in the system. |
60
|
|
|
* |
61
|
|
|
* @return Mailcode_Translator_Syntax[] |
62
|
|
|
*/ |
63
|
|
|
public function getSyntaxes() : array |
64
|
|
|
{ |
65
|
|
|
$names = $this->getSyntaxNames(); |
66
|
|
|
$result = array(); |
67
|
|
|
|
68
|
|
|
foreach($names as $name) |
69
|
|
|
{ |
70
|
|
|
$result[] = $this->createSyntax($name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Retrieves a list of the names of all syntaxes supported |
78
|
|
|
* by the system. |
79
|
|
|
* |
80
|
|
|
* @return string[] |
81
|
|
|
*/ |
82
|
|
|
public function getSyntaxNames() : array |
83
|
|
|
{ |
84
|
|
|
return FileHelper::createFileFinder(__DIR__.'/Translator/Syntax') |
85
|
|
|
->getPHPClassNames(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Checks whether the specified syntax exists. |
90
|
|
|
* |
91
|
|
|
* @param string $name The syntax name, e.g. "ApacheVelocity" |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function syntaxExists(string $name) : bool |
95
|
|
|
{ |
96
|
|
|
$names = $this->getSyntaxNames(); |
97
|
|
|
|
98
|
|
|
return in_array($name, $names); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|