1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the "andrey-helldar/laravel-lang-publisher" project. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @author Andrey Helldar <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @copyright 2021 Andrey Helldar |
12
|
|
|
* |
13
|
|
|
* @license MIT |
14
|
|
|
* |
15
|
|
|
* @see https://github.com/andrey-helldar/laravel-lang-publisher |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Helldar\LaravelLangPublisher\Comparators; |
21
|
|
|
|
22
|
|
|
use Helldar\Contracts\LangPublisher\Comparator; |
23
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Arrayable; |
24
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Has; |
25
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Paths; |
26
|
|
|
use Helldar\LaravelLangPublisher\Facades\Helpers\Config; |
27
|
|
|
use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; |
28
|
|
|
use Helldar\Support\Facades\Helpers\Arr; |
29
|
|
|
|
30
|
|
|
abstract class Base implements Comparator |
31
|
|
|
{ |
32
|
|
|
use Arrayable; |
33
|
|
|
use Has; |
34
|
|
|
use Paths; |
35
|
|
|
|
36
|
|
|
protected $full; |
37
|
|
|
|
38
|
|
|
protected $keys = []; |
39
|
|
|
|
40
|
|
|
protected $translations = []; |
41
|
|
|
|
42
|
|
|
protected $result = []; |
43
|
|
|
|
44
|
|
|
protected $exclude = ['attributes', 'custom']; |
45
|
|
|
|
46
|
85 |
|
public function __construct(array $keys, array $translations, bool $full = false) |
47
|
|
|
{ |
48
|
85 |
|
$this->keys = $keys; |
49
|
|
|
|
50
|
85 |
|
$this->translations = $translations; |
51
|
|
|
|
52
|
85 |
|
$this->full = $full; |
53
|
85 |
|
} |
54
|
|
|
|
55
|
85 |
|
public function get(): array |
56
|
|
|
{ |
57
|
85 |
|
foreach ($this->filenames() as $filename) { |
58
|
85 |
|
foreach ($this->locales($filename) as $locale) { |
59
|
85 |
|
$result = $this->compare($filename, $locale); |
60
|
|
|
|
61
|
85 |
|
$path = $this->resolvePath($filename, $locale); |
62
|
|
|
|
63
|
85 |
|
$this->putResult($path, $result); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
85 |
|
return $this->getResult(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
abstract protected function merge(array $local, array $translated, array $excluded): array; |
71
|
|
|
|
72
|
85 |
|
protected function compare(string $filename, string $locale): array |
73
|
|
|
{ |
74
|
85 |
|
$local = $this->resource($filename, $locale); |
75
|
85 |
|
$translated = $this->translated($filename, $locale); |
76
|
|
|
|
77
|
85 |
|
$main = $this->merge( |
78
|
85 |
|
$this->extract($filename, $local), |
79
|
85 |
|
$this->extract($filename, $translated), |
80
|
85 |
|
$this->excludes($filename, $local), |
81
|
|
|
); |
82
|
|
|
|
83
|
85 |
|
$extra = $this->sortAndMerge( |
84
|
85 |
|
$this->extra($filename, $local), |
85
|
85 |
|
$this->extra($filename, $translated), |
86
|
|
|
); |
87
|
|
|
|
88
|
85 |
|
return $this->mergeArray($main, $extra); |
89
|
|
|
} |
90
|
|
|
|
91
|
85 |
|
protected function resource(string $filename, string $locale): array |
92
|
|
|
{ |
93
|
85 |
|
$filename = $this->resolvePath($filename, $locale); |
94
|
|
|
|
95
|
85 |
|
$path = $this->resourcesPath($filename); |
96
|
|
|
|
97
|
85 |
|
return Filesystem::load($path); |
98
|
|
|
} |
99
|
|
|
|
100
|
85 |
|
protected function translated(string $filename, string $locale): array |
101
|
|
|
{ |
102
|
85 |
|
$values = $this->translations[$filename][$locale]; |
103
|
|
|
|
104
|
85 |
|
$keys = $this->keys[$filename]; |
105
|
|
|
|
106
|
85 |
|
return Arr::only($values, $keys); |
107
|
|
|
} |
108
|
|
|
|
109
|
85 |
|
protected function excludes(string $filename, array $user): array |
110
|
|
|
{ |
111
|
85 |
|
$excludes = Config::excludes(); |
112
|
|
|
|
113
|
85 |
|
$key = $this->filename($filename); |
114
|
|
|
|
115
|
85 |
|
$values = Arr::get($excludes, $key, []); |
116
|
|
|
|
117
|
85 |
|
return Arr::only($user, $values); |
118
|
|
|
} |
119
|
|
|
|
120
|
85 |
|
protected function extract(string $filename, array $array): array |
121
|
|
|
{ |
122
|
85 |
|
if ($this->hasValidation($filename)) { |
123
|
85 |
|
return Arr::except($array, $this->exclude); |
124
|
|
|
} |
125
|
|
|
|
126
|
85 |
|
return $array; |
127
|
|
|
} |
128
|
|
|
|
129
|
85 |
|
protected function extra(string $filename, array $array): array |
130
|
|
|
{ |
131
|
85 |
|
if ($this->hasValidation($filename)) { |
132
|
85 |
|
return Arr::only($array, $this->exclude); |
133
|
|
|
} |
134
|
|
|
|
135
|
85 |
|
return []; |
136
|
|
|
} |
137
|
|
|
|
138
|
85 |
|
protected function putResult(string $filename, array $array): void |
139
|
|
|
{ |
140
|
85 |
|
$this->result[$filename] = $array; |
141
|
85 |
|
} |
142
|
|
|
|
143
|
85 |
|
protected function getResult(): array |
144
|
|
|
{ |
145
|
85 |
|
return $this->result; |
146
|
|
|
} |
147
|
|
|
|
148
|
85 |
|
protected function filenames(): array |
149
|
|
|
{ |
150
|
85 |
|
return array_keys($this->keys); |
151
|
|
|
} |
152
|
|
|
|
153
|
85 |
|
protected function locales(string $filename): array |
154
|
|
|
{ |
155
|
85 |
|
return array_keys($this->translations[$filename]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|