Completed
Pull Request — master (#29)
by
unknown
12:01
created

AutoTranslate::translateAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Ben182\AutoTranslate;
4
5
use Illuminate\Support\Arr;
6
use Ben182\AutoTranslate\Translators\TranslatorInterface;
7
8
abstract class AutoTranslate
9
{
10
    public $translator;
11
12
    public function __construct(TranslatorInterface $translator)
13
    {
14 21
        $this->translator = $translator;
15
        $this->translator->setSource(config('auto-translate.source_language'));
16 21
    }
17 21
18 21
    public function translateAll(array $targetLanguages, $callbackAfterEachTranslation = null)
19 21
    {
20
        $sourceTranslations = $this->getSourceTranslations();
21 12
22
        foreach ($targetLanguages as $targetLanguage) {
23 12
            $translated = $this->translate(
24
                $targetLanguage,
25
                $sourceTranslations,
26 15
                $callbackAfterEachTranslation
27
            );
28 15
29
            $this->fillLanguageFiles($targetLanguage, $translated);
30 15
        }
31
    }
32 15
33 15
    public function translateMissing(array $targetLanguages, $callbackAfterEachTranslation = null)
34 3
    {
35
        foreach ($targetLanguages as $targetLanguage) {
36
            $missingTranslations = $this->getMissingTranslations($targetLanguage);
37 15
38
            $translated = $this->translate(
39 15
                $targetLanguage,
40
                $missingTranslations,
41
                $callbackAfterEachTranslation
42 15
            );
43
44
            $this->fillLanguageFiles($targetLanguage, $translated);
45 9
        }
46
    }
47 9
48 9
    public function translate(string $targetLanguage, $sourceTranslation, $callbackAfterEachTranslation = null)
49
    {
50 9
        $this->translator->setTarget($targetLanguage);
51 9
52
        foreach ($sourceTranslation as $key => $value) {
53 9
            $variables = $this->findVariables($value);
54
55 9
            $sourceTranslation[$key] = is_string($value) ? $this->translator->translate($value) : $value;
56
57
            $sourceTranslation[$key] = $this->replaceTranslatedVariablesWithOld($variables, $sourceTranslation[$key]);
58 3
59
            if ($callbackAfterEachTranslation) {
60 3
                $callbackAfterEachTranslation();
61
            }
62 3
        }
63
64 3
        return $sourceTranslation;
65 3
    }
66
67 3
    public function findVariables($string)
68
    {
69 3
        $m = null;
70
71 3
        if (is_string($string)) {
72
            preg_match_all('/:\S+/', $string, $m);
73
        }
74
75
        return $m;
76 3
    }
77
78
    public function replaceTranslatedVariablesWithOld($variables, $string)
79 3
    {
80
        if (isset($variables[0])) {
81 3
            $replacements = $variables[0];
82
83 3
            return preg_replace_callback('/:\S+/', function ($matches) use (&$replacements) {
0 ignored issues
show
Unused Code introduced by
The parameter $matches is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84 3
                return array_shift($replacements);
85
            }, $string);
86
        }
87 3
    }
88
89
    abstract public function getSourceTranslations();
90 3
    abstract public function getTranslations(string $lang);
91
    abstract public function getMissingTranslations(string $lang);
92 3
    abstract public function fillLanguageFiles(string $language, array $data);
93
}
94