1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelLang\Entities; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelLang\Contracts\Entities\Locale as LocaleContract; |
8
|
|
|
use Illuminate\Support\{Arr, Str}; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Locale |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Locale implements LocaleContract |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $key = ''; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $path = ''; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
private $files = []; |
30
|
|
|
|
31
|
|
|
/* ----------------------------------------------------------------- |
32
|
|
|
| Constructor |
33
|
|
|
| ----------------------------------------------------------------- |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Locale constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $key |
40
|
|
|
* @param string $path |
41
|
|
|
* @param array $files |
42
|
|
|
*/ |
43
|
132 |
|
public function __construct(string $key, string $path, array $files = []) |
44
|
|
|
{ |
45
|
132 |
|
$this->key = $key; |
46
|
132 |
|
$this->path = $path; |
47
|
132 |
|
$this->files = $files; |
48
|
132 |
|
} |
49
|
|
|
|
50
|
|
|
/* ----------------------------------------------------------------- |
51
|
|
|
| Getters & Setters |
52
|
|
|
| ----------------------------------------------------------------- |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the locale key. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
132 |
|
public function getKey(): string |
61
|
|
|
{ |
62
|
132 |
|
return $this->key; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the locale translations path. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
54 |
|
public function getPath(): string |
71
|
|
|
{ |
72
|
54 |
|
return $this->path; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get locale translations. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
12 |
|
public function getTranslations(): array |
81
|
|
|
{ |
82
|
12 |
|
$translations = array_map(function ($file) { |
83
|
12 |
|
return $file['content']; |
84
|
12 |
|
}, $this->files); |
85
|
|
|
|
86
|
12 |
|
return Arr::dot($translations); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/* ----------------------------------------------------------------- |
90
|
|
|
| Main Methods |
91
|
|
|
| ----------------------------------------------------------------- |
92
|
|
|
*/ |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Merge translations. |
96
|
|
|
* |
97
|
|
|
* @param \Arcanedev\LaravelLang\Contracts\Entities\Locale|null $locale |
98
|
|
|
* @param array $ignored |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
6 |
|
public function mergeTranslations(LocaleContract $locale = null, array $ignored = []): array |
103
|
|
|
{ |
104
|
6 |
|
$merged = []; |
105
|
6 |
|
$translations = array_merge( |
106
|
6 |
|
is_null($locale) ? [] : $locale->getTranslations(), |
107
|
6 |
|
$this->getTranslations() |
108
|
|
|
); |
109
|
|
|
|
110
|
6 |
|
foreach ($translations as $key => $trans) { |
111
|
6 |
|
if ( ! Str::startsWith($key, $ignored)) { |
112
|
6 |
|
$merged[$key] = $trans; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
return $merged; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: