|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ReliqArts\DirectTranslator\Translation; |
|
6
|
|
|
|
|
7
|
|
|
use ReliqArts\DirectTranslator\Exception as ExceptionContract; |
|
8
|
|
|
use ReliqArts\DirectTranslator\Translation\Exception\TranslationFailed; |
|
9
|
|
|
use ReliqArts\DirectTranslator\Translator; |
|
10
|
|
|
use ReliqArts\DirectTranslator\Vocabulary; |
|
11
|
|
|
use ReliqArts\DirectTranslator\VocabularyLoader; |
|
12
|
|
|
|
|
13
|
|
|
final class Executor implements Translator |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var VocabularyLoader |
|
17
|
|
|
*/ |
|
18
|
|
|
private $vocabularyLoader; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Formatters |
|
22
|
|
|
*/ |
|
23
|
|
|
private $formatters; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Replacers |
|
27
|
|
|
*/ |
|
28
|
|
|
private $replacers; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Translator constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param VocabularyLoader $vocabularyLoader |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(VocabularyLoader $vocabularyLoader) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->vocabularyLoader = $vocabularyLoader; |
|
38
|
|
|
$this->formatters = new Formatters(); |
|
39
|
|
|
$this->replacers = new Replacers(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
* |
|
45
|
|
|
* @throws TranslationFailed |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function translate(string $text, string $vocabularyKey): string |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
|
|
$vocabulary = $this->vocabularyLoader->load($vocabularyKey); |
|
53
|
|
|
|
|
54
|
|
|
return $this->format($this->replace($text, $vocabulary)); |
|
55
|
|
|
} catch (ExceptionContract $exception) { |
|
56
|
|
|
throw new TranslationFailed( |
|
57
|
|
|
sprintf('Translation failed. %s', $exception->getMessage()), |
|
58
|
|
|
$exception->getCode(), |
|
59
|
|
|
$exception |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param Formatter $formatter |
|
66
|
|
|
*/ |
|
67
|
|
|
public function addFormatter(Formatter $formatter): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->formatters->add($formatter); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Replacer $replacer |
|
74
|
|
|
*/ |
|
75
|
|
|
public function addReplacer(Replacer $replacer): void |
|
76
|
|
|
{ |
|
77
|
|
|
$this->replacers->add($replacer); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $inputText |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
private function format(string $inputText): string |
|
86
|
|
|
{ |
|
87
|
|
|
$result = $inputText; |
|
88
|
|
|
|
|
89
|
|
|
foreach ($this->formatters as $formatter) { |
|
90
|
|
|
$result = $formatter->apply($result); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $inputText |
|
98
|
|
|
* @param Vocabulary $vocabulary |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
private function replace(string $inputText, Vocabulary $vocabulary): string |
|
103
|
|
|
{ |
|
104
|
|
|
$result = $inputText; |
|
105
|
|
|
|
|
106
|
|
|
foreach ($this->replacers as $replacer) { |
|
107
|
|
|
$result = $replacer->replace($result, $vocabulary); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $result; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|