Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| FromFileTranslator | |
100.00% |
10 / 10 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createFallback | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getGeneralTranslation | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Translator; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Translator\Enums\FromFileLanguage; |
| 6 | use Apie\Core\Translator\Lists\TranslationStringSet; |
| 7 | use Apie\Core\Translator\ValueObjects\TranslationString; |
| 8 | |
| 9 | class FromFileTranslator implements ApieTranslatorInterface |
| 10 | { |
| 11 | public function __construct(private readonly string $translationPath) |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | public static function createFallback(): self |
| 16 | { |
| 17 | return new self(__DIR__ . '/../../lang/'); |
| 18 | } |
| 19 | |
| 20 | public function getGeneralTranslation(ApieContext $context, TranslationString|TranslationStringSet $translations): ?string |
| 21 | { |
| 22 | if ($translations instanceof TranslationString) { |
| 23 | $translations = new TranslationStringSet([$translations]); |
| 24 | } |
| 25 | $language = FromFileLanguage::fromContext($context); |
| 26 | foreach ($translations as $translation) { |
| 27 | $fullPath = $translation->toPath(rtrim($this->translationPath, '/') . '/' . $language->value) . '.php'; |
| 28 | if (file_exists($fullPath)) { |
| 29 | return include $fullPath; |
| 30 | } |
| 31 | } |
| 32 | return null; |
| 33 | } |
| 34 | } |