|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* The MIT License (MIT) |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
|
8
|
|
|
* |
|
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
11
|
|
|
* in the Software without restriction, including without limitation the rights |
|
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
14
|
|
|
* furnished to do so, subject to the following conditions: |
|
15
|
|
|
* |
|
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
17
|
|
|
* copies or substantial portions of the Software. |
|
18
|
|
|
* |
|
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
25
|
|
|
* SOFTWARE. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace danielgp\common_lib; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Usefull functions to support multi-language feedback |
|
33
|
|
|
* |
|
34
|
|
|
* @author Daniel Popiniuc |
|
35
|
|
|
*/ |
|
36
|
|
|
trait CommonLibLocale |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
protected $commonLibFlags = null; |
|
40
|
|
|
protected $tCmnLb = null; |
|
41
|
|
|
protected $tCmnRequest = null; |
|
42
|
|
|
protected $tCmnSession = null; |
|
43
|
|
|
protected $tCmnSuperGlobals = null; |
|
44
|
|
|
|
|
45
|
|
|
private function getCommonLocaleFolder() |
|
46
|
|
|
{ |
|
47
|
|
|
$pathes = explode(DIRECTORY_SEPARATOR, __DIR__); |
|
48
|
|
|
$pathDepth = count($pathes); |
|
49
|
|
|
$localePath = []; |
|
50
|
|
|
foreach ($pathes as $key => $value) { |
|
51
|
|
|
if ($key < ($pathDepth - 1)) { |
|
52
|
|
|
$localePath[] = $value; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
return implode(DIRECTORY_SEPARATOR, $localePath); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function getTimestampArray($crtTime) |
|
59
|
|
|
{ |
|
60
|
|
|
return ['float' => $this->getTimestampFloat($crtTime), 'string' => $this->getTimestampString($crtTime)]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function getTimestampFloat($crtTime) |
|
64
|
|
|
{ |
|
65
|
|
|
return ($crtTime['sec'] + $crtTime['usec'] / pow(10, 6)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function getTimestampRaw($returnType) |
|
69
|
|
|
{ |
|
70
|
|
|
return call_user_func([$this, 'getTimestamp' . ucfirst($returnType)], gettimeofday()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function getTimestampString($crtTime) |
|
74
|
|
|
{ |
|
75
|
|
|
return implode('', [ |
|
76
|
|
|
'<span style="color:black!important;font-weight:bold;">[', |
|
77
|
|
|
date('Y-m-d H:i:s.', $crtTime['sec']), |
|
78
|
|
|
substr(round($crtTime['usec'], -3), 0, 3), |
|
79
|
|
|
']</span> ' |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Stores given language or default one into global session variable |
|
85
|
|
|
* (In order to avoid potential language injections from other applications session will revert |
|
86
|
|
|
* to the default language if application one is not among the one are not supported here) |
|
87
|
|
|
* |
|
88
|
|
|
* @return NOTHING |
|
89
|
|
|
*/ |
|
90
|
|
|
private function handleLanguageIntoSession() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->settingsCommonLib(); |
|
93
|
|
|
$this->initializeSprGlbAndSession(); |
|
94
|
|
|
if (is_null($this->tCmnSuperGlobals->get('lang'))) { |
|
95
|
|
|
if (!is_null($this->tCmnSession->get('lang'))) { |
|
96
|
|
|
$this->tCmnSession->set('lang', filter_var($this->tCmnSession->get('lang'), FILTER_SANITIZE_STRING)); |
|
97
|
|
|
} elseif (is_null($this->tCmnSession->get('lang'))) { |
|
98
|
|
|
$this->tCmnSession->set('lang', $this->commonLibFlags['default_language']); |
|
99
|
|
|
} |
|
100
|
|
|
} elseif (!is_null($this->tCmnSuperGlobals->get('lang'))) { |
|
101
|
|
|
$this->tCmnSession->set('lang', filter_var($this->tCmnSuperGlobals->get('lang'), FILTER_SANITIZE_STRING)); |
|
102
|
|
|
} |
|
103
|
|
|
$this->normalizeLocalizationIntoSession(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Takes care of instatiation of localization libraries |
|
108
|
|
|
* used within current module for multi-languages support |
|
109
|
|
|
* |
|
110
|
|
|
* @return NOTHING |
|
111
|
|
|
*/ |
|
112
|
|
|
private function handleLocalizationCommon() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->handleLanguageIntoSession(); |
|
115
|
|
|
$localizationFile = $this->getCommonLocaleFolder() . '/locale/' |
|
116
|
|
|
. $this->tCmnSession->get('lang') . '/LC_MESSAGES/' |
|
117
|
|
|
. $this->commonLibFlags['localization_domain'] |
|
118
|
|
|
. '.mo'; |
|
119
|
|
|
$extrClass = new \Gettext\Extractors\Mo(); |
|
120
|
|
|
$translations = $extrClass->fromFile($localizationFile); |
|
121
|
|
|
$this->tCmnLb = new \Gettext\Translator(); |
|
122
|
|
|
$this->tCmnLb->loadTranslations($translations); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function initializeSprGlbAndSession() |
|
126
|
|
|
{ |
|
127
|
|
|
if (is_null($this->tCmnSuperGlobals)) { |
|
128
|
|
|
$this->tCmnRequest = new \Symfony\Component\HttpFoundation\Request; |
|
129
|
|
|
$this->tCmnSuperGlobals = $this->tCmnRequest->createFromGlobals(); |
|
130
|
|
|
} |
|
131
|
|
|
if (is_null($this->tCmnSession)) { |
|
132
|
|
|
$sBridge = new \Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage(); |
|
133
|
|
|
$this->tCmnSession = new \Symfony\Component\HttpFoundation\Session\Session($sBridge); |
|
134
|
|
|
$this->tCmnSession->start(); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Central function to deal with multi-language messages |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $localizedStringCode |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function lclMsgCmn($localizedStringCode) |
|
145
|
|
|
{ |
|
146
|
|
|
if (is_null($this->tCmnLb)) { |
|
147
|
|
|
$this->settingsCommonLib(); |
|
148
|
|
|
$this->handleLocalizationCommon(); |
|
149
|
|
|
} |
|
150
|
|
|
return $this->tCmnLb->gettext($localizedStringCode); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
protected function lclMsgCmnNumber($singularString, $pluralString, $numberToEvaluate) |
|
154
|
|
|
{ |
|
155
|
|
|
if (is_null($this->tCmnLb)) { |
|
156
|
|
|
$this->settingsCommonLib(); |
|
157
|
|
|
$this->handleLocalizationCommon(); |
|
158
|
|
|
} |
|
159
|
|
|
return $this->tCmnLb->ngettext($singularString, $pluralString, $numberToEvaluate); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
private function normalizeLocalizationIntoSession() |
|
163
|
|
|
{ |
|
164
|
|
|
if (!array_key_exists($this->tCmnSession->get('lang'), $this->commonLibFlags['available_languages'])) { |
|
165
|
|
|
$this->tCmnSession->set('lang', $this->commonLibFlags['default_language']); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns proper result from a mathematical division in order to avoid |
|
171
|
|
|
* Zero division erorr or Infinite results |
|
172
|
|
|
* |
|
173
|
|
|
* @param float $fAbove |
|
174
|
|
|
* @param float $fBelow |
|
175
|
|
|
* @param mixed $mArguments |
|
176
|
|
|
* @return decimal |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function setDividedResult($fAbove, $fBelow, $mArguments = 0) |
|
179
|
|
|
{ |
|
180
|
|
|
if (($fAbove == 0) || ($fBelow == 0)) { // prevent infinite result AND division by 0 |
|
181
|
|
|
return 0; |
|
182
|
|
|
} |
|
183
|
|
|
if (is_array($mArguments)) { |
|
184
|
|
|
$frMinMax = [ |
|
185
|
|
|
'MinFractionDigits' => $mArguments[1], |
|
186
|
|
|
'MaxFractionDigits' => $mArguments[1], |
|
187
|
|
|
]; |
|
188
|
|
|
return $this->setNumberFormat(($fAbove / $fBelow), $frMinMax); |
|
189
|
|
|
} |
|
190
|
|
|
return $this->setNumberFormat(round(($fAbove / $fBelow), $mArguments)); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
protected function setNumberFormat($content, $features = null) |
|
194
|
|
|
{ |
|
195
|
|
|
$features = $this->setNumberFormatFeatures($features); |
|
196
|
|
|
$fmt = new \NumberFormatter($features['locale'], $features['style']); |
|
197
|
|
|
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $features['MinFractionDigits']); |
|
198
|
|
|
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $features['MaxFractionDigits']); |
|
199
|
|
|
return $fmt->format($content); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
private function setNumberFormatFeatures($features) |
|
203
|
|
|
{ |
|
204
|
|
|
$this->handleLanguageIntoSession(); |
|
205
|
|
|
if (is_null($features)) { |
|
206
|
|
|
$features = [ |
|
207
|
|
|
'locale' => $this->tCmnSession->get('lang'), |
|
208
|
|
|
'style' => \NumberFormatter::DECIMAL, |
|
209
|
|
|
'MinFractionDigits' => 0, |
|
210
|
|
|
'MaxFractionDigits' => 0, |
|
211
|
|
|
]; |
|
212
|
|
|
} |
|
213
|
|
|
if (!array_key_exists('locale', $features)) { |
|
214
|
|
|
$features['locale'] = $this->tCmnSession->get('lang'); |
|
215
|
|
|
} |
|
216
|
|
|
if (!array_key_exists('style', $features)) { |
|
217
|
|
|
$features['style'] = \NumberFormatter::DECIMAL; |
|
218
|
|
|
} |
|
219
|
|
|
return $features; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Settings |
|
224
|
|
|
* |
|
225
|
|
|
* @return NOTHING |
|
226
|
|
|
*/ |
|
227
|
|
|
private function settingsCommonLib() |
|
228
|
|
|
{ |
|
229
|
|
|
$this->commonLibFlags = [ |
|
230
|
|
|
'available_languages' => [ |
|
231
|
|
|
'en_US' => 'US English', |
|
232
|
|
|
'ro_RO' => 'Română', |
|
233
|
|
|
'it_IT' => 'Italiano', |
|
234
|
|
|
], |
|
235
|
|
|
'default_language' => 'en_US', |
|
236
|
|
|
'localization_domain' => 'common-locale' |
|
237
|
|
|
]; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|