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

PhpTranslator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 74
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getTranslations() 0 18 3
A getMissingTranslations() 0 12 1
A fillLanguageFiles() 0 12 2
A getSourceTranslations() 0 8 1
A undotArray() 0 8 2
1
<?php
2
3
namespace Ben182\AutoTranslate\AutoTranslators;
4
5
use Illuminate\Support\Arr;
6
use Themsaid\Langman\Manager as Langman;
7
use Ben182\AutoTranslate\AutoTranslate;
8
use Ben182\AutoTranslate\Translators\TranslatorInterface;
9
10
class PhpTranslator extends AutoTranslate
11
{
12
    protected $manager;
13
14
    public function __construct(TranslatorInterface $translator, Langman $manager)
15
    {
16
        $this->manager = $manager;
17
18
        parent::__construct($translator);
19
    }
20
21
    public function getTranslations(string $lang)
22
    {
23
        $aReturn = [];
24
25
        $files = $this->manager->files();
26
27
        foreach ($files as $fileKeyName => $languagesFile) {
28
            if (! isset($languagesFile[$lang])) {
29
                continue;
30
            }
31
32
            $allTranslations = $this->manager->getFileContent($languagesFile[$lang]);
33
34
            $aReturn[$fileKeyName] = $allTranslations;
35
        }
36
37
        return $aReturn;
38
    }
39
40
    public function getMissingTranslations(string $lang)
41
    {
42
        $source = $this->getSourceTranslations();
43
        $lang = $this->getTranslations($lang);
44
45
        $dottedSource = Arr::dot($source);
46
        $dottedlang = Arr::dot($lang);
47
48
        $diff = array_diff(array_keys($dottedSource), array_keys($dottedlang));
49
50
        return collect($dottedSource)->only($diff)->toArray();
51
    }
52
53
    public function fillLanguageFiles(string $language, array $data)
54
    {
55
        $data = $this->undotArray($data);
56
57
        foreach ($data as $languageFileKey => $translations) {
58
            $translations = array_map(function ($item) use ($language) {
59
                return [$language => $item];
60
            }, $translations);
61
62
            $this->manager->fillKeys($languageFileKey, $translations);
63
        }
64
    }
65
66
    public function getSourceTranslations()
67
    {
68
        return Arr::dot(
69
            $this->getTranslations(
70
                config('auto-translate.source_language')
71
            )
72
        );
73
    }
74
75
    public function undotArray(array $dottedArray, array $initialArray = []) : array
76
    {
77
        foreach ($dottedArray as $key => $value) {
78
            Arr::set($initialArray, $key, $value);
79
        }
80
81
        return $initialArray;
82
    }
83
}
84