1
|
|
|
<?php namespace Mascame\Artificer; |
2
|
|
|
|
3
|
|
|
use Mascame\Artificer\Options\AdminOption; |
4
|
|
|
use Str; |
5
|
|
|
|
6
|
|
|
class Localization |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var \Closure |
11
|
|
|
*/ |
12
|
|
|
protected $lang_closure; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $locales = array(); |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$closure = AdminOption::get('localization.lang_detection'); |
23
|
|
|
$this->locales = $this->getConfigLocales(); |
|
|
|
|
24
|
|
|
|
25
|
|
|
if ($closure && Artificer::isClosure($closure)) { |
26
|
|
|
$this->lang_closure = $closure; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getConfigLocales() |
31
|
|
|
{ |
32
|
|
|
return (!empty($this->locales)) ? $this->locales : $this->locales = AdminOption::get('localization.locales'); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function getLocales() |
39
|
|
|
{ |
40
|
|
|
$this->locales = $this->getConfigLocales(); |
|
|
|
|
41
|
|
|
|
42
|
|
|
if (is_array($this->locales)) { |
43
|
|
|
return array_keys($this->locales); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return array(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getLocaleNative($locale) |
50
|
|
|
{ |
51
|
|
|
$this->locales = $this->getConfigLocales(); |
|
|
|
|
52
|
|
|
|
53
|
|
|
if (is_array($this->locales[$locale])) { |
54
|
|
|
return $this->locales[$locale]['native']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return array(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $column |
62
|
|
|
* @return bool|int|string |
63
|
|
|
*/ |
64
|
|
|
public function parseColumnLang($column) |
65
|
|
|
{ |
66
|
|
|
if ($this->lang_closure) { |
67
|
|
|
return $this->lang_closure($column); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->detectColumnLang($column); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $column |
75
|
|
|
* @return bool|int|string |
76
|
|
|
*/ |
77
|
|
|
protected function detectColumnLang($column) |
78
|
|
|
{ |
79
|
|
|
foreach ($this->getLanguageEndings() as $locale => $ending) { |
80
|
|
|
if (Str::endsWith($column, $ending)) { |
81
|
|
|
return $locale; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function getLanguageEndings() |
92
|
|
|
{ |
93
|
|
|
$endings = array(); |
94
|
|
|
|
95
|
|
|
foreach ($this->getLocales() as $locale) { |
96
|
|
|
$endings[$locale] = '_' . $locale; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $endings; |
100
|
|
|
} |
101
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..