|
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 $prefix; |
|
14
|
|
|
|
|
15
|
|
|
private $indent; |
|
16
|
|
|
|
|
17
|
14 |
|
public function __construct(string $filePath, string $prefix, string $indent = ' ') |
|
18
|
|
|
{ |
|
19
|
14 |
|
$this->filePath = $filePath; |
|
20
|
14 |
|
$this->prefix = $prefix; |
|
21
|
14 |
|
$this->indent = $indent; |
|
22
|
14 |
|
} |
|
23
|
|
|
|
|
24
|
14 |
|
public function load(): array |
|
25
|
|
|
{ |
|
26
|
14 |
|
if (!file_exists($this->filePath)) { |
|
27
|
8 |
|
return []; |
|
28
|
|
|
} |
|
29
|
6 |
|
$content = trim(file_get_contents($this->filePath) ?: ''); |
|
30
|
6 |
|
if ($content === '') { |
|
31
|
|
|
return []; |
|
32
|
|
|
} |
|
33
|
6 |
|
$texts = Neon::decode($content); |
|
34
|
6 |
|
return $this->arrayToFlat($texts); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
14 |
|
public function save(array $texts): bool |
|
38
|
|
|
{ |
|
39
|
14 |
|
$texts = array_merge($this->load(), $texts); |
|
40
|
14 |
|
ksort($texts); |
|
41
|
|
|
|
|
42
|
14 |
|
$translations = []; |
|
43
|
14 |
|
foreach ($texts as $key => $value) { |
|
44
|
12 |
|
$key = strpos($key, $this->prefix) === 0 ? substr($key, strlen($this->prefix)) : $key; |
|
45
|
12 |
|
$translationKeyParts = explode('.', (string) $key); |
|
46
|
12 |
|
$translations = $this->addToTranslations($translations, $translationKeyParts, $value); |
|
47
|
|
|
} |
|
48
|
14 |
|
$dirname = pathinfo($this->filePath, PATHINFO_DIRNAME); |
|
49
|
14 |
|
if (!is_dir($dirname)) { |
|
|
|
|
|
|
50
|
|
|
mkdir($dirname, 0777, true); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
14 |
|
return (bool) file_put_contents($this->filePath, str_replace("\t", $this->indent, Neon::encode($translations, Encoder::BLOCK))); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
6 |
|
private function arrayToFlat(array $texts, array $translations = []): array |
|
56
|
|
|
{ |
|
57
|
6 |
|
foreach ($texts as $key => $value) { |
|
58
|
6 |
|
if (!is_array($value)) { |
|
59
|
6 |
|
$translations[$this->prefix . $key] = $value; |
|
60
|
6 |
|
continue; |
|
61
|
|
|
} |
|
62
|
2 |
|
$translations = $this->arrayToFlat($this->shiftArrayKey($value, $key), $translations); |
|
63
|
|
|
} |
|
64
|
6 |
|
return $translations; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
private function shiftArrayKey(array $texts, string $parentKey) |
|
68
|
|
|
{ |
|
69
|
2 |
|
$newTexts = []; |
|
70
|
2 |
|
foreach ($texts as $key => $value) { |
|
71
|
2 |
|
$newTexts[$parentKey . '.' . $key] = $value; |
|
72
|
|
|
} |
|
73
|
2 |
|
return $newTexts; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
12 |
|
private function addToTranslations(array $translations, array $translationKeyParts, string $text): array |
|
77
|
|
|
{ |
|
78
|
12 |
|
$keyPart = array_shift($translationKeyParts); |
|
79
|
12 |
|
if (count($translationKeyParts) === 0) { |
|
80
|
12 |
|
$translations[$keyPart] = $text; |
|
81
|
12 |
|
return $translations; |
|
82
|
|
|
} |
|
83
|
6 |
|
if (isset($translations[$keyPart]) && is_string($translations[$keyPart])) { |
|
84
|
2 |
|
$translations[$keyPart . '.' . implode('.', $translationKeyParts)] = $text; |
|
85
|
2 |
|
return $translations; |
|
86
|
|
|
} |
|
87
|
6 |
|
$translations[$keyPart] = $this->addToTranslations($translations[$keyPart] ?? [], $translationKeyParts, $text); |
|
88
|
6 |
|
return $translations; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|