Completed
Push — master ( d19c7e...b4b8ba )
by Michal
02:01
created

NeonFileStorage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 8 2
A shiftArrayKey() 0 7 2
A save() 0 11 2
A addToTranslations() 0 13 4
A arrayToFlat() 0 10 3
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\Bridge\KdybyTranslation\Storage;
4
5
use Efabrica\TranslationsAutomatization\Storage\StorageInterface;
6
use Nette\Neon\Encoder;
7
use Nette\Neon\Neon;
8
9
class NeonFileStorage implements StorageInterface
10
{
11
    private $filePath;
12
13
    private $indent;
14
15 14
    public function __construct(string $filePath, string $indent = "\t")
16
    {
17 14
        $this->filePath = $filePath;
18 14
        $this->indent = $indent;
19 14
    }
20
21 14
    public function load(): array
22
    {
23 14
        if (!file_exists($this->filePath)) {
24 8
            return [];
25
        }
26
27 6
        $texts = Neon::decode(file_get_contents($this->filePath));
28 6
        return $this->arrayToFlat($texts);
29
    }
30
31 14
    public function save(array $texts): bool
32
    {
33 14
        $texts = array_merge($this->load(), $texts);
34 14
        ksort($texts);
35
36 14
        $translations = [];
37 14
        foreach ($texts as $key => $value) {
38 12
            $translationKeyParts = explode('.', $key);
39 12
            $translations = $this->addToTranslations($translations, $translationKeyParts, $value);
40
        }
41 14
        return (bool) file_put_contents($this->filePath, str_replace("\t", $this->indent, Neon::encode($translations, Encoder::BLOCK)));
42
    }
43
44 6
    private function arrayToFlat(array $texts, array $translations = []): array
45
    {
46 6
        foreach ($texts as $key => $value) {
47 6
            if (!is_array($value)) {
48 6
                $translations[$key] = $value;
49 6
                continue;
50
            }
51 2
            $translations = $this->arrayToFlat($this->shiftArrayKey($value, $key), $translations);
52
        }
53 6
        return $translations;
54
    }
55
56 2
    private function shiftArrayKey(array $texts, string $parentKey)
57
    {
58 2
        $newTexts = [];
59 2
        foreach ($texts as $key => $value) {
60 2
            $newTexts[$parentKey . '.' . $key] = $value;
61
        }
62 2
        return $newTexts;
63
    }
64
65 12
    private function addToTranslations(array $translations, array $translationKeyParts, string $text): array
66
    {
67 12
        $keyPart = array_shift($translationKeyParts);
68 12
        if (count($translationKeyParts) === 0) {
69 12
            $translations[$keyPart] = $text;
70 12
            return $translations;
71
        }
72 6
        if (isset($translations[$keyPart]) && is_string($translations[$keyPart])) {
73 2
            $translations[$keyPart . '.' . implode('.', $translationKeyParts)] = $text;
74 2
            return $translations;
75
        }
76 6
        $translations[$keyPart] = $this->addToTranslations($translations[$keyPart] ?? [], $translationKeyParts, $text);
77 6
        return $translations;
78
    }
79
}
80