1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2015 - 2018 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
|
|
|
* Useful 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
|
|
|
public $tCmnSession = null; |
43
|
|
|
public $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
|
|
|
*/ |
89
|
|
|
public function handleLanguageIntoSession() |
90
|
|
|
{ |
91
|
|
|
$this->settingsCommonLib(); |
92
|
|
|
$this->initializeSprGlbAndSession(); |
93
|
|
|
if (is_null($this->tCmnSuperGlobals->get('lang')) && is_null($this->tCmnSession->get('lang'))) { |
94
|
|
|
$this->tCmnSession->set('lang', $this->commonLibFlags['default_language']); |
95
|
|
|
} elseif (!is_null($this->tCmnSuperGlobals->get('lang'))) { |
96
|
|
|
$this->tCmnSession->set('lang', filter_var($this->tCmnSuperGlobals->get('lang'), FILTER_SANITIZE_STRING)); |
97
|
|
|
} |
98
|
|
|
$this->normalizeLocalizationIntoSession(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Takes care of instantiation of localization libraries |
103
|
|
|
* used within current module for multi-languages support |
104
|
|
|
* |
105
|
|
|
*/ |
106
|
|
|
private function handleLocalizationCommon() |
107
|
|
|
{ |
108
|
|
|
$this->handleLanguageIntoSession(); |
109
|
|
|
$localizationFile = $this->getCommonLocaleFolder() . '/locale/' |
110
|
|
|
. $this->tCmnSession->get('lang') . '/LC_MESSAGES/' |
111
|
|
|
. $this->commonLibFlags['localization_domain'] . '.mo'; |
112
|
|
|
$translations = new \Gettext\Translations(); |
113
|
|
|
$translations->addFromMoFile($localizationFile); |
114
|
|
|
$this->tCmnLb = new \Gettext\Translator(); |
115
|
|
|
$this->tCmnLb->loadTranslations($translations); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function initializeSprGlbAndSession() |
119
|
|
|
{ |
120
|
|
|
if (is_null($this->tCmnSuperGlobals)) { |
121
|
|
|
$this->tCmnRequest = new \Symfony\Component\HttpFoundation\Request(); |
122
|
|
|
$this->tCmnSuperGlobals = $this->tCmnRequest->createFromGlobals(); |
123
|
|
|
} |
124
|
|
|
if (is_null($this->tCmnSession)) { |
125
|
|
|
$sBridge = new \Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage(); |
126
|
|
|
$this->tCmnSession = new \Symfony\Component\HttpFoundation\Session\Session($sBridge); |
127
|
|
|
$this->tCmnSession->start(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function lclManagePrerequisites() |
132
|
|
|
{ |
133
|
|
|
if (is_null($this->tCmnLb)) { |
134
|
|
|
$this->settingsCommonLib(); |
135
|
|
|
$this->handleLocalizationCommon(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Central function to deal with multi-language messages |
141
|
|
|
* |
142
|
|
|
* @param string $localizedStringCode |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function lclMsgCmn($localizedStringCode) |
146
|
|
|
{ |
147
|
|
|
$this->lclManagePrerequisites(); |
148
|
|
|
return $this->tCmnLb->gettext($localizedStringCode); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function lclMsgCmnNumber($singularString, $pluralString, $numberToEvaluate) |
152
|
|
|
{ |
153
|
|
|
$this->lclManagePrerequisites(); |
154
|
|
|
return sprintf($this->tCmnLb->ngettext($singularString, $pluralString, $numberToEvaluate), 1); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function normalizeLocalizationIntoSession() |
158
|
|
|
{ |
159
|
|
|
if (!array_key_exists($this->tCmnSession->get('lang'), $this->commonLibFlags['available_languages'])) { |
160
|
|
|
$this->tCmnSession->set('lang', $this->commonLibFlags['default_language']); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns proper result from a mathematical division in order to avoid |
166
|
|
|
* Zero division error or Infinite results |
167
|
|
|
* |
168
|
|
|
* @param float $fAbove |
169
|
|
|
* @param float $fBelow |
170
|
|
|
* @param mixed $mArguments |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function setDividedResult($fAbove, $fBelow, $mArguments = null) |
174
|
|
|
{ |
175
|
|
|
if (($fAbove == 0) || ($fBelow == 0)) { // prevent infinite result AND division by 0 |
176
|
|
|
return 0; |
177
|
|
|
} |
178
|
|
|
$numberToFormat = ($fAbove / $fBelow); |
179
|
|
|
if (is_numeric($mArguments)) { |
180
|
|
|
$frMinMax = [ |
181
|
|
|
'MinFractionDigits' => $mArguments, |
182
|
|
|
'MaxFractionDigits' => $mArguments, |
183
|
|
|
]; |
184
|
|
|
return $this->setNumberFormat($numberToFormat, $frMinMax); |
185
|
|
|
} |
186
|
|
|
return $this->setNumberFormat(round($numberToFormat, $mArguments)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
protected function setNumberFormat($content, $ftrs = null) |
190
|
|
|
{ |
191
|
|
|
$features = $this->setNumberFormatFeatures($ftrs); |
192
|
|
|
$fmt = new \NumberFormatter($features['locale'], $features['style']); |
193
|
|
|
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $features['MinFractionDigits']); |
194
|
|
|
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $features['MaxFractionDigits']); |
195
|
|
|
return $fmt->format($content); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
private function setNumberFormatFeatures($features) |
199
|
|
|
{ |
200
|
|
|
$this->handleLanguageIntoSession(); |
201
|
|
|
if (is_null($features)) { |
202
|
|
|
$features = [ |
203
|
|
|
'locale' => $this->tCmnSession->get('lang'), |
204
|
|
|
'style' => \NumberFormatter::DECIMAL, |
205
|
|
|
'MinFractionDigits' => 0, |
206
|
|
|
'MaxFractionDigits' => 0, |
207
|
|
|
]; |
208
|
|
|
} |
209
|
|
|
if (!array_key_exists('locale', $features)) { |
210
|
|
|
$features['locale'] = $this->tCmnSession->get('lang'); |
211
|
|
|
} |
212
|
|
|
if (!array_key_exists('style', $features)) { |
213
|
|
|
$features['style'] = \NumberFormatter::DECIMAL; |
214
|
|
|
} |
215
|
|
|
return $features; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Settings |
220
|
|
|
* |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
private function settingsCommonLib() |
224
|
|
|
{ |
225
|
|
|
$this->commonLibFlags = [ |
226
|
|
|
'available_languages' => [ |
227
|
|
|
'en_US' => 'US English', |
228
|
|
|
'ro_RO' => 'Română', |
229
|
|
|
'it_IT' => 'Italiano', |
230
|
|
|
], |
231
|
|
|
'default_language' => 'en_US', |
232
|
|
|
'localization_domain' => 'common-locale' |
233
|
|
|
]; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|