1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Helldar\LaravelLangPublisher\Comparators; |
6
|
|
|
|
7
|
|
|
use Helldar\Contracts\LangPublisher\Comparator; |
8
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Has; |
9
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Paths; |
10
|
|
|
use Helldar\LaravelLangPublisher\Facades\Helpers\Config; |
11
|
|
|
use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; |
12
|
|
|
use Helldar\Support\Facades\Helpers\Arr; |
13
|
|
|
|
14
|
|
|
abstract class Base implements Comparator |
15
|
|
|
{ |
16
|
|
|
use Has; |
17
|
|
|
use Paths; |
18
|
|
|
|
19
|
|
|
protected $keys = []; |
20
|
|
|
|
21
|
|
|
protected $translations = []; |
22
|
|
|
|
23
|
|
|
protected $result = []; |
24
|
|
|
|
25
|
|
|
protected $exclude = ['attributes', 'custom']; |
26
|
|
|
|
27
|
|
|
public function __construct(array $keys, array $translations) |
28
|
|
|
{ |
29
|
|
|
$this->keys = $keys; |
30
|
|
|
|
31
|
|
|
$this->translations = $translations; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; |
35
|
|
|
|
36
|
|
|
public function get(): array |
37
|
|
|
{ |
38
|
|
|
foreach ($this->filenames() as $filename) { |
39
|
|
|
foreach ($this->locales($filename) as $locale) { |
40
|
|
|
$result = $this->compare($filename, $locale); |
41
|
|
|
|
42
|
|
|
$path = $this->resolvePath($filename, $locale); |
43
|
|
|
|
44
|
|
|
$this->putResult($path, $result); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->getResult(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function compare(string $filename, string $locale): array |
52
|
|
|
{ |
53
|
|
|
$local = $this->resource($filename, $locale); |
54
|
|
|
$translated = $this->translated($filename, $locale); |
55
|
|
|
|
56
|
|
|
return $this->merge( |
57
|
|
|
$this->extract($filename, $local), |
58
|
|
|
$this->extract($filename, $translated), |
59
|
|
|
$this->excludes($filename, $local), |
60
|
|
|
$this->extra($filename, $local), |
61
|
|
|
$this->extra($filename, $translated), |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function resource(string $filename, string $locale): array |
66
|
|
|
{ |
67
|
|
|
$filename = $this->resolvePath($filename, $locale); |
68
|
|
|
|
69
|
|
|
$path = $this->resourcesPath($filename); |
70
|
|
|
|
71
|
|
|
return Filesystem::load($path); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function translated(string $filename, string $locale): array |
75
|
|
|
{ |
76
|
|
|
$values = $this->translations[$filename][$locale]; |
77
|
|
|
|
78
|
|
|
$keys = $this->keys[$filename]; |
79
|
|
|
|
80
|
|
|
return Arr::only($values, $keys); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function excludes(string $filename, array $user): array |
84
|
|
|
{ |
85
|
|
|
$excludes = Config::excludes(); |
86
|
|
|
|
87
|
|
|
$key = $this->filename($filename); |
88
|
|
|
|
89
|
|
|
$values = Arr::get($excludes, $key, []); |
90
|
|
|
|
91
|
|
|
return Arr::only($user, $values); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function extract(string $filename, array $array): array |
95
|
|
|
{ |
96
|
|
|
if ($this->hasValidation($filename)) { |
97
|
|
|
return Arr::except($array, $this->exclude); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $array; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function extra(string $filename, array $array): array |
104
|
|
|
{ |
105
|
|
|
if ($this->hasValidation($filename)) { |
106
|
|
|
return Arr::only($array, $this->exclude); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return []; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function putResult(string $filename, array $array): void |
113
|
|
|
{ |
114
|
|
|
$this->result[$filename] = $array; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function getResult(): array |
118
|
|
|
{ |
119
|
|
|
return $this->result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function filenames(): array |
123
|
|
|
{ |
124
|
|
|
return array_keys($this->keys); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function locales(string $filename): array |
128
|
|
|
{ |
129
|
|
|
return array_keys($this->translations[$filename]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|