1 | <?php |
||
30 | trait I18NAbleTrait |
||
31 | { |
||
32 | |||
33 | private $_lang = 'en'; |
||
34 | private $_rawI18N = []; |
||
35 | private $_languages = ['en']; |
||
36 | private $_defaultLanguage = 'en'; |
||
37 | |||
38 | /** |
||
39 | * Get current working language code |
||
40 | * @return string Language code |
||
41 | * @Ignored |
||
42 | */ |
||
43 | 15 | public function getLang() |
|
47 | |||
48 | /** |
||
49 | * Get available languages |
||
50 | * @return string[] |
||
51 | * @Ignored |
||
52 | */ |
||
53 | 66 | public function getLanguages() |
|
57 | |||
58 | /** |
||
59 | * Get i18n values with all languages. |
||
60 | * This returns all language values of all class fields. Class field names are |
||
61 | * keys for arrays of language values, with language codes as a keys. |
||
62 | * |
||
63 | * Example returned variable: |
||
64 | * ```php |
||
65 | * [ |
||
66 | * 'name' => [ |
||
67 | * 'en' => 'January', |
||
68 | * 'pl' => 'Styczeń' |
||
69 | * ], |
||
70 | * 'description' => [ |
||
71 | * 'en' => 'First mothn of a year' |
||
72 | * 'pl' => 'Pierwszy miesiąc roku' |
||
73 | * ] |
||
74 | * ] |
||
75 | * ``` |
||
76 | * @return mixed[] Associative array of language values |
||
77 | * @Ignored |
||
78 | */ |
||
79 | 15 | public function getRawI18N() |
|
80 | { |
||
81 | 15 | $meta = ManganMeta::create($this); |
|
82 | 15 | $fields = $meta->properties('i18n'); |
|
83 | |||
84 | // Set current model value for current language for each property |
||
85 | 15 | foreach ($fields as $name => $i18n) |
|
86 | { |
||
87 | // This is to keep rawi18n value in sync with current model value. |
||
88 | 15 | $this->_rawI18N[$name][$this->getLang()] = $this->$name; |
|
89 | } |
||
90 | 15 | return $this->_rawI18N; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Set language code. This changes current model language. |
||
95 | * After setting language model attributes will store values in different locale. |
||
96 | * |
||
97 | * Language code must be previously set by `setLanguages`. |
||
98 | * When trying to set undefined language code, method will do nothing. |
||
99 | * When setting already choosen language code, method will also ignore this call. |
||
100 | * Example method calls: |
||
101 | * ```php |
||
102 | * // Set available languages |
||
103 | * $model->setLanguages(['en', 'pl']); |
||
104 | * |
||
105 | * // Will ignore as en is by default |
||
106 | * $model->setLang('en'); |
||
107 | * |
||
108 | * // Will set pl as language |
||
109 | * $model->setLang('pl'); |
||
110 | * |
||
111 | * // Will ignore as ru is not available |
||
112 | * $model->setLang('ru') |
||
113 | * ``` |
||
114 | * @param string $code |
||
115 | * @Ignored |
||
116 | */ |
||
117 | 67 | public function setLang($code) |
|
118 | { |
||
119 | 67 | if ($this->_lang === $code) |
|
120 | { |
||
121 | 7 | return false; |
|
122 | } |
||
123 | 66 | if (!in_array($code, $this->getLanguages())) |
|
124 | { |
||
125 | 65 | $this->_languages[] = $code; |
|
126 | } |
||
127 | 66 | $event = new ModelEvent($this); |
|
128 | 66 | $event->data = $code; |
|
129 | 66 | if (!Event::valid($this, InternationalInterface::EventBeforeLangChange, $event)) |
|
130 | { |
||
131 | return false; |
||
132 | } |
||
133 | 66 | $this->_changeAttributesLang($this->_lang, $code); |
|
134 | 66 | $this->_lang = $code; |
|
135 | 66 | Event::trigger($this, InternationalInterface::EventAfterLangChange, $event); |
|
136 | 66 | return true; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Set available languages. This method accepts one parameter, |
||
141 | * array contaning language codes prefably in short ISO form. |
||
142 | * Example valid array and method calls: |
||
143 | * ```php |
||
144 | * $languages = ['en', 'pl', 'ru']; |
||
145 | * $model->setLanguages($languages); |
||
146 | * $model2->setLanguages(['en']); |
||
147 | * ``` |
||
148 | * @param string[] $languages |
||
149 | * @Ignored |
||
150 | */ |
||
151 | 7 | public function setLanguages($languages) |
|
162 | |||
163 | /** |
||
164 | * Set i18n values in all languages. |
||
165 | * This method must keep `$values` for further use, by method `getRawI18N`. |
||
166 | * @param mixed[] $values |
||
167 | * @Ignored |
||
168 | */ |
||
169 | 8 | public function setRawI18N($values) |
|
173 | |||
174 | /** |
||
175 | * Get default language used for I18N operations. |
||
176 | * |
||
177 | * If not previously set, will fall back to `en`. |
||
178 | * |
||
179 | * @return string |
||
180 | * @Ignored |
||
181 | */ |
||
182 | 2 | public function getDefaultLanguage() |
|
186 | |||
187 | /** |
||
188 | * Set default language used for I18N operations. This language |
||
189 | * will be used if the `setLang` method was not called. |
||
190 | * |
||
191 | * The value should be language code, for example `en` |
||
192 | * |
||
193 | * @param string $language |
||
194 | * @Ignored |
||
195 | */ |
||
196 | public function setDefaultLanguage($language) |
||
200 | |||
201 | /** |
||
202 | * Change i18n attributes values to appropriate language |
||
203 | * @param string $fromCode |
||
204 | * @param string $toCode |
||
205 | */ |
||
206 | 66 | private function _changeAttributesLang($fromCode, $toCode) |
|
252 | |||
253 | } |
||
254 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: