|
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 |
|
$this->flatten($texts); |
|
29
|
6 |
|
return $texts; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
14 |
|
public function save(array $texts): bool |
|
33
|
|
|
{ |
|
34
|
14 |
|
$texts = array_merge($this->load(), $texts); |
|
35
|
14 |
|
ksort($texts); |
|
36
|
|
|
|
|
37
|
14 |
|
$translations = []; |
|
38
|
14 |
|
foreach ($texts as $key => $value) { |
|
39
|
12 |
|
$translationKeyParts = explode('.', $key); |
|
40
|
12 |
|
$translations = $this->addToTranslations($translations, $translationKeyParts, $value); |
|
41
|
|
|
} |
|
42
|
14 |
|
return (bool) file_put_contents($this->filePath, str_replace("\t", $this->indent, Neon::encode($translations, Encoder::BLOCK))); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
6 |
|
private function flatten(array &$translations, array $subnode = null, $path = null) |
|
46
|
|
|
{ |
|
47
|
6 |
|
if ($subnode === null) { |
|
48
|
6 |
|
$subnode = &$translations; |
|
49
|
|
|
} |
|
50
|
6 |
|
foreach ($subnode as $key => $value) { |
|
51
|
6 |
|
if (is_array($value)) { |
|
52
|
2 |
|
$nodePath = $path ? $path . '.' . $key : $key; |
|
53
|
2 |
|
$this->flatten($translations, $value, $nodePath); |
|
54
|
2 |
|
if ($path === null) { |
|
55
|
2 |
|
unset($translations[$key]); |
|
56
|
|
|
} |
|
57
|
6 |
|
} elseif ($path !== null) { |
|
58
|
6 |
|
$translations[$path . '.' . $key] = $value; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
6 |
|
} |
|
62
|
|
|
|
|
63
|
12 |
|
private function addToTranslations(array $translations, array $translationKeyParts, string $text): array |
|
64
|
|
|
{ |
|
65
|
12 |
|
$keyPart = array_shift($translationKeyParts); |
|
66
|
12 |
|
if (count($translationKeyParts) === 0) { |
|
67
|
12 |
|
$translations[$keyPart] = $text; |
|
68
|
12 |
|
return $translations; |
|
69
|
|
|
} |
|
70
|
6 |
|
if (isset($translations[$keyPart]) && is_string($translations[$keyPart])) { |
|
71
|
2 |
|
$translations[$keyPart . '.' . implode('.', $translationKeyParts)] = $text; |
|
72
|
2 |
|
return $translations; |
|
73
|
|
|
} |
|
74
|
6 |
|
if (!isset($translations[$keyPart])) { |
|
75
|
6 |
|
$translations[$keyPart] = []; |
|
76
|
|
|
} |
|
77
|
6 |
|
$translations[$keyPart] = $this->addToTranslations($translations[$keyPart], $translationKeyParts, $text); |
|
78
|
6 |
|
return $translations; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|