Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 93 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
tao_helpers_translation_POUtils | |
0.00% |
0 / 93 |
|
0.00% |
0 / 5 |
1190 | |
0.00% |
0 / 1 |
sanitize | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
unserializeAnnotations | |
0.00% |
0 / 50 |
|
0.00% |
0 / 1 |
306 | |||
serializeAnnotations | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
132 | |||
addFlag | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getApplicationHelper | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
19 | * (under the project TAO-TRANSFER); |
20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
22 | * |
23 | */ |
24 | |
25 | use oat\tao\model\service\ApplicationService; |
26 | use oat\oatbox\service\ServiceManager; |
27 | |
28 | /** |
29 | * Short description of class tao_helpers_translation_POUtils |
30 | * |
31 | * @access public |
32 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
33 | * @package tao |
34 | |
35 | */ |
36 | class tao_helpers_translation_POUtils |
37 | { |
38 | // --- ASSOCIATIONS --- |
39 | |
40 | |
41 | // --- ATTRIBUTES --- |
42 | |
43 | // --- OPERATIONS --- |
44 | |
45 | /** |
46 | * Short description of method sanitize |
47 | * |
48 | * @access public |
49 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
50 | * @param string string |
51 | * @param boolean reverse |
52 | * @return string |
53 | */ |
54 | public static function sanitize($string, $reverse = false) |
55 | { |
56 | $returnValue = (string) ''; |
57 | |
58 | |
59 | if ($reverse) { |
60 | $smap = ['"', "\n", "\t", "\r"]; |
61 | $rmap = ['\\"', '\\n"' . "\n" . '"', '\\t', '\\r']; |
62 | $returnValue = (string) str_replace($smap, $rmap, $string); |
63 | } else { |
64 | $smap = ['/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\"/']; |
65 | $rmap = ['', "\n", "\r", "\t", '"']; |
66 | $returnValue = (string) preg_replace($smap, $rmap, $string); |
67 | } |
68 | |
69 | |
70 | return (string) $returnValue; |
71 | } |
72 | |
73 | /** |
74 | * Unserialize PO message comments. |
75 | * |
76 | * @access public |
77 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
78 | * @param string annotations The PO message comments. |
79 | * @return array |
80 | */ |
81 | public static function unserializeAnnotations($annotations) |
82 | { |
83 | $returnValue = []; |
84 | |
85 | |
86 | $matches = []; |
87 | $encoding = self::getApplicationHelper()->getDefaultEncoding(); |
88 | |
89 | if (preg_match_all('/(#[\.\:,\|]{0,1}\s+(?:[^\\n]*))/', $annotations, $matches) !== false) { |
90 | if (isset($matches[1]) && count($matches[1]) > 0) { |
91 | foreach ($matches[1] as $match) { |
92 | $match = trim($match); |
93 | $matchLen = mb_strlen($match, $encoding); |
94 | $annotationId = null; |
95 | $annotationValue = null; |
96 | |
97 | switch (mb_substr($match, 1, 1, $encoding)) { |
98 | case "\t": |
99 | case ' ': |
100 | // Translator comment. |
101 | $annotationId = tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS; |
102 | $annotationValue = mb_substr($match, 2, $matchLen - 2, $encoding); |
103 | break; |
104 | |
105 | case '.': |
106 | $annotationId = tao_helpers_translation_POTranslationUnit::EXTRACTED_COMMENTS; |
107 | $annotationValue = mb_substr($match, 3, $matchLen - 3, $encoding); |
108 | break; |
109 | |
110 | case ':': |
111 | $annotationId = tao_helpers_translation_POTranslationUnit::REFERENCE; |
112 | $annotationValue = mb_substr($match, 3, $matchLen - 3, $encoding); |
113 | break; |
114 | |
115 | case ',': |
116 | $annotationId = tao_helpers_translation_POTranslationUnit::FLAGS; |
117 | $annotationValue = mb_substr($match, 3, $matchLen - 3, $encoding); |
118 | break; |
119 | |
120 | case '|': |
121 | if (($pos = mb_strpos($match, 'msgid_plural', 0, $encoding)) !== false) { |
122 | $pos += mb_strlen('msgid_plural', $encoding) + 1; |
123 | $annotationId = tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID_PLURAL; |
124 | $annotationValue = mb_substr($match, $pos, $matchLen - $pos, $encoding); |
125 | } elseif (($pos = mb_strpos($match, 'msgid', 0, $encoding)) !== false) { |
126 | $pos += mb_strlen('msgid', $encoding) + 1; |
127 | $annotationId = tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID; |
128 | $annotationValue = mb_substr($match, $pos, $matchLen - $pos, $encoding); |
129 | } elseif (($pos = mb_strpos($match, 'msgctxt', 0, $encoding)) !== false) { |
130 | $pos += mb_strlen('msgctxt', $encoding) + 1; |
131 | $annotationId = tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGCTXT; |
132 | $annotationValue = mb_substr($match, $pos, $matchLen - $pos, $encoding); |
133 | } |
134 | break; |
135 | } |
136 | |
137 | if ($annotationId != null && $annotationValue != null) { |
138 | if (!isset($returnValue[$annotationId])) { |
139 | $returnValue[$annotationId] = $annotationValue; |
140 | } else { |
141 | $returnValue[$annotationId] .= "\n${annotationValue}"; |
142 | } |
143 | } |
144 | } |
145 | } |
146 | } else { |
147 | throw new tao_helpers_translation_TranslationException( |
148 | "An error occured while unserializing annotations '${annotations}'." |
149 | ); |
150 | } |
151 | |
152 | |
153 | return (array) $returnValue; |
154 | } |
155 | |
156 | /** |
157 | * Serialize an array of annotations in a PO compliant comments format. |
158 | * |
159 | * @access public |
160 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
161 | * @param array $annotations An array of annotations where keys are annotation identifiers and values are annotation |
162 | * values. |
163 | * @return string |
164 | */ |
165 | public static function serializeAnnotations($annotations) |
166 | { |
167 | $returnValue = (string) ''; |
168 | |
169 | |
170 | // Buffer will contain each line of the serialized PO comment block. |
171 | $buffer = []; |
172 | |
173 | foreach ($annotations as $name => $value) { |
174 | $prefix = null; |
175 | |
176 | switch ($name) { |
177 | case tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS: |
178 | $prefix = '#'; |
179 | break; |
180 | |
181 | case tao_helpers_translation_POTranslationUnit::EXTRACTED_COMMENTS: |
182 | $prefix = '#.'; |
183 | break; |
184 | |
185 | case tao_helpers_translation_POTranslationUnit::REFERENCE: |
186 | $prefix = '#:'; |
187 | break; |
188 | |
189 | case tao_helpers_translation_POTranslationUnit::FLAGS: |
190 | $prefix = '#,'; |
191 | break; |
192 | |
193 | case tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID: |
194 | case tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID_PLURAL: |
195 | case tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGCTXT: |
196 | $prefix = '#|'; |
197 | break; |
198 | } |
199 | |
200 | if ($prefix !== null) { |
201 | // We have a PO compliant annotation that we have to serialize. |
202 | foreach (explode("\n", $value) as $v) { |
203 | $buffer[] = "${prefix} ${v}"; |
204 | } |
205 | } |
206 | } |
207 | |
208 | // Glue the annotation lines in a single PO comment block. |
209 | $returnValue = implode("\n", $buffer); |
210 | |
211 | |
212 | return (string) $returnValue; |
213 | } |
214 | |
215 | /** |
216 | * Append a flag to an existing PO comment flag value. |
217 | * |
218 | * @access public |
219 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
220 | * @param string comment A PO flag comment value in which you have to add the new flag. |
221 | * @param string flag The flag to add to the existing $comment. |
222 | * @return string |
223 | */ |
224 | public static function addFlag($comment, $flag) |
225 | { |
226 | $returnValue = (string) ''; |
227 | |
228 | |
229 | $returnValue = $comment; |
230 | $flag = trim($flag); |
231 | $encoding = self::getApplicationHelper()->getDefaultEncoding(); |
232 | if (mb_strpos($returnValue, $flag, 0, $encoding) === false) { |
233 | $returnValue .= ((mb_strlen($returnValue, $encoding) > 0) ? " ${flag}" : $flag); |
234 | } |
235 | |
236 | |
237 | return (string) $returnValue; |
238 | } |
239 | |
240 | // @TODO: Required to be able to mock tao extension constants in tests. |
241 | // Remove after injecting ApplicationService as a dependency. |
242 | private static function getApplicationHelper() |
243 | { |
244 | return ServiceManager::getServiceManager()->get(ApplicationService::SERVICE_ID); |
245 | } |
246 | } |