|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @filesource |
|
4
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Miroslav Fedeleš <[email protected]> |
|
7
|
|
|
* @since 0.26 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Core\I18n; |
|
11
|
|
|
|
|
12
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
13
|
|
|
use Zend\Http\Request; |
|
14
|
|
|
use Auth\Entity\UserInterface as User; |
|
15
|
|
|
|
|
16
|
|
|
class Locale |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $defaultLanguage; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $supportedLanguages; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $supportedLanguages |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $supportedLanguages) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->supportedLanguages = $supportedLanguages; |
|
35
|
|
|
$languages = array_keys($supportedLanguages); |
|
36
|
|
|
$this->defaultLanguage = reset($languages); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Request $request |
|
41
|
|
|
* @param User $user |
|
|
|
|
|
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function detectLanguage(Request $request, User $user = null) |
|
45
|
|
|
{ |
|
46
|
|
|
if (isset($user)) |
|
47
|
|
|
{ |
|
48
|
|
|
$settings = $user->getSettings('Core'); |
|
49
|
|
|
|
|
50
|
|
|
if (isset($settings->localization) |
|
51
|
|
|
&& isset($settings->localization->language) |
|
52
|
|
|
&& $settings->localization->language != '') |
|
53
|
|
|
{ |
|
54
|
|
|
// return language by user's settings |
|
55
|
|
|
return $settings->localization->language; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$headers = $request->getHeaders(); |
|
60
|
|
|
|
|
61
|
|
|
if ($headers->has('Accept-Language')) { |
|
62
|
|
|
$locales = $headers->get('Accept-Language')->getPrioritized(); |
|
63
|
|
|
foreach ($locales as $locale) { |
|
64
|
|
|
$language = $locale->type; |
|
65
|
|
|
|
|
66
|
|
|
if (isset($this->supportedLanguages[$language])) { |
|
67
|
|
|
// return language by browser's accept language |
|
68
|
|
|
return $language; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// no match, therefore return default language |
|
74
|
|
|
return $this->defaultLanguage; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $lang |
|
79
|
|
|
* @throws \InvalidArgumentException |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getLocaleByLanguage($lang) |
|
83
|
|
|
{ |
|
84
|
|
|
if (!isset($this->supportedLanguages[$lang])) |
|
85
|
|
|
{ |
|
86
|
|
|
throw new \InvalidArgumentException(sprintf('Unsupported language: "%s"', $lang)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->supportedLanguages[$lang]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $lang |
|
94
|
|
|
* @return boolean |
|
95
|
|
|
*/ |
|
96
|
|
|
public function isLanguageSupported($lang) |
|
97
|
|
|
{ |
|
98
|
|
|
return isset($this->supportedLanguages[$lang]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getDefaultLanguage() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->defaultLanguage; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.