Conditions | 88 |
Paths | > 20000 |
Total Lines | 408 |
Code Lines | 262 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
91 | public static function spanLtrRtl($inputText, $direction = 'BOTH', $class = '') |
||
92 | { |
||
93 | if ($inputText == '') { |
||
94 | // Nothing to do |
||
95 | return ''; |
||
96 | } |
||
97 | |||
98 | $workingText = str_replace("\n", '<br>', $inputText); |
||
99 | $workingText = str_replace(array('<span class="starredname"><br>', '<span<br>class="starredname">'), '<br><span class="starredname">', $workingText); // Reposition some incorrectly placed line breaks |
||
100 | $workingText = self::stripLrmRlm($workingText); // Get rid of any existing UTF8 control codes |
||
101 | |||
102 | // $nothing = '‌'; // Zero Width Non-Joiner (not sure whether this is still needed to work around a TCPDF bug) |
||
103 | $nothing = ''; |
||
104 | |||
105 | self::$startLTR = '<LTR>'; // This will become '<span dir="ltr">' at the end |
||
106 | self::$endLTR = '</LTR>'; // This will become '</span>' at the end |
||
107 | self::$startRTL = '<RTL>'; // This will become '<span dir="rtl">' at the end |
||
108 | self::$endRTL = '</RTL>'; // This will become '</span>' at the end |
||
109 | self::$lenStart = strlen(self::$startLTR); // RTL version MUST have same length |
||
110 | self::$lenEnd = strlen(self::$endLTR); // RTL version MUST have same length |
||
111 | |||
112 | self::$previousState = ''; |
||
113 | self::$currentState = strtoupper(I18N::direction()); |
||
114 | $numberState = false; // Set when we're inside a numeric string |
||
115 | $result = ''; |
||
116 | self::$waitingText = ''; |
||
117 | $openParDirection = array(); |
||
118 | |||
119 | self::beginCurrentSpan($result); |
||
120 | |||
121 | while ($workingText != '') { |
||
122 | $charArray = self::getChar($workingText, 0); // Get the next ASCII or UTF-8 character |
||
123 | $currentLetter = $charArray['letter']; |
||
124 | $currentLen = $charArray['length']; |
||
125 | |||
126 | $openParIndex = strpos(self::OPEN_PARENTHESES, $currentLetter); // Which opening parenthesis is this? |
||
127 | $closeParIndex = strpos(self::CLOSE_PARENTHESES, $currentLetter); // Which closing parenthesis is this? |
||
128 | |||
129 | switch ($currentLetter) { |
||
130 | case '<': |
||
131 | // Assume this '<' starts an HTML element |
||
132 | $endPos = strpos($workingText, '>'); // look for the terminating '>' |
||
133 | if ($endPos === false) { |
||
134 | $endPos = 0; |
||
135 | } |
||
136 | $currentLen += $endPos; |
||
137 | $element = substr($workingText, 0, $currentLen); |
||
138 | $temp = strtolower(substr($element, 0, 3)); |
||
139 | if (strlen($element) < 7 && $temp == '<br') { |
||
140 | if ($numberState) { |
||
141 | $numberState = false; |
||
142 | if (self::$currentState == 'RTL') { |
||
143 | self::$waitingText .= WT_UTF8_PDF; |
||
144 | } |
||
145 | } |
||
146 | self::breakCurrentSpan($result); |
||
147 | } elseif (self::$waitingText == '') { |
||
148 | $result .= $element; |
||
149 | } else { |
||
150 | self::$waitingText .= $element; |
||
151 | } |
||
152 | $workingText = substr($workingText, $currentLen); |
||
153 | break; |
||
154 | case '&': |
||
155 | // Assume this '&' starts an HTML entity |
||
156 | $endPos = strpos($workingText, ';'); // look for the terminating ';' |
||
157 | if ($endPos === false) { |
||
158 | $endPos = 0; |
||
159 | } |
||
160 | $currentLen += $endPos; |
||
161 | $entity = substr($workingText, 0, $currentLen); |
||
162 | if (strtolower($entity) == ' ') { |
||
163 | $entity .= ' '; // Ensure consistent case for this entity |
||
164 | } |
||
165 | if (self::$waitingText == '') { |
||
166 | $result .= $entity; |
||
167 | } else { |
||
168 | self::$waitingText .= $entity; |
||
169 | } |
||
170 | $workingText = substr($workingText, $currentLen); |
||
171 | break; |
||
172 | case '{': |
||
173 | if (substr($workingText, 1, 1) == '{') { |
||
174 | // Assume this '{{' starts a TCPDF directive |
||
175 | $endPos = strpos($workingText, '}}'); // look for the terminating '}}' |
||
176 | if ($endPos === false) { |
||
177 | $endPos = 0; |
||
178 | } |
||
179 | $currentLen = $endPos + 2; |
||
180 | $directive = substr($workingText, 0, $currentLen); |
||
181 | $workingText = substr($workingText, $currentLen); |
||
182 | $result = $result . self::$waitingText . $directive; |
||
183 | self::$waitingText = ''; |
||
184 | break; |
||
185 | } |
||
186 | default: |
||
187 | // Look for strings of numbers with optional leading or trailing + or - |
||
188 | // and with optional embedded numeric punctuation |
||
189 | if ($numberState) { |
||
190 | // If we're inside a numeric string, look for reasons to end it |
||
191 | $offset = 0; // Be sure to look at the current character first |
||
192 | $charArray = self::getChar($workingText . "\n", $offset); |
||
193 | if (strpos(self::NUMBERS, $charArray['letter']) === false) { |
||
194 | // This is not a digit. Is it numeric punctuation? |
||
195 | if (substr($workingText . "\n", $offset, 6) == ' ') { |
||
196 | $offset += 6; // This could be numeric punctuation |
||
197 | } elseif (strpos(self::NUMBER_PUNCTUATION, $charArray['letter']) !== false) { |
||
198 | $offset += $charArray['length']; // This could be numeric punctuation |
||
199 | } |
||
200 | // If the next character is a digit, the current character is numeric punctuation |
||
201 | $charArray = self::getChar($workingText . "\n", $offset); |
||
202 | if (strpos(self::NUMBERS, $charArray['letter']) === false) { |
||
203 | // This is not a digit. End the run of digits and punctuation. |
||
204 | $numberState = false; |
||
205 | if (self::$currentState == 'RTL') { |
||
206 | if (strpos(self::NUMBER_PREFIX, $currentLetter) === false) { |
||
207 | $currentLetter = WT_UTF8_PDF . $currentLetter; |
||
208 | } else { |
||
209 | $currentLetter = $currentLetter . WT_UTF8_PDF; // Include a trailing + or - in the run |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | } else { |
||
215 | // If we're outside a numeric string, look for reasons to start it |
||
216 | if (strpos(self::NUMBER_PREFIX, $currentLetter) !== false) { |
||
217 | // This might be a number lead-in |
||
218 | $offset = $currentLen; |
||
219 | $nextChar = substr($workingText . "\n", $offset, 1); |
||
220 | if (strpos(self::NUMBERS, $nextChar) !== false) { |
||
221 | $numberState = true; // We found a digit: the lead-in is therefore numeric |
||
222 | if (self::$currentState == 'RTL') { |
||
223 | $currentLetter = WT_UTF8_LRE . $currentLetter; |
||
224 | } |
||
225 | } |
||
226 | } elseif (strpos(self::NUMBERS, $currentLetter) !== false) { |
||
227 | $numberState = true; // The current letter is a digit |
||
228 | if (self::$currentState == 'RTL') { |
||
229 | $currentLetter = WT_UTF8_LRE . $currentLetter; |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | |||
234 | // Determine the directionality of the current UTF-8 character |
||
235 | $newState = self::$currentState; |
||
236 | while (true) { |
||
237 | if (I18N::scriptDirection(I18N::textScript($currentLetter)) === 'rtl') { |
||
238 | if (self::$currentState == '') { |
||
239 | $newState = 'RTL'; |
||
240 | break; |
||
241 | } |
||
242 | |||
243 | if (self::$currentState == 'RTL') { |
||
244 | break; |
||
245 | } |
||
246 | // Switch to RTL only if this isn't a solitary RTL letter |
||
247 | $tempText = substr($workingText, $currentLen); |
||
248 | while ($tempText != '') { |
||
249 | $nextCharArray = self::getChar($tempText, 0); |
||
250 | $nextLetter = $nextCharArray['letter']; |
||
251 | $nextLen = $nextCharArray['length']; |
||
252 | $tempText = substr($tempText, $nextLen); |
||
253 | |||
254 | if (I18N::scriptDirection(I18N::textScript($nextLetter)) === 'rtl') { |
||
255 | $newState = 'RTL'; |
||
256 | break 2; |
||
257 | } |
||
258 | |||
259 | if (strpos(self::PUNCTUATION, $nextLetter) !== false || strpos(self::OPEN_PARENTHESES, $nextLetter) !== false) { |
||
260 | $newState = 'RTL'; |
||
261 | break 2; |
||
262 | } |
||
263 | |||
264 | if ($nextLetter === ' ') { |
||
265 | break; |
||
266 | } |
||
267 | $nextLetter .= substr($tempText . "\n", 0, 5); |
||
268 | if ($nextLetter === ' ') { |
||
269 | break; |
||
270 | } |
||
271 | } |
||
272 | // This is a solitary RTL letter : wrap it in UTF8 control codes to force LTR directionality |
||
273 | $currentLetter = WT_UTF8_LRO . $currentLetter . WT_UTF8_PDF; |
||
274 | $newState = 'LTR'; |
||
275 | break; |
||
276 | } |
||
277 | if (($currentLen != 1) || ($currentLetter >= 'A' && $currentLetter <= 'Z') || ($currentLetter >= 'a' && $currentLetter <= 'z')) { |
||
278 | // Since it’s neither Hebrew nor Arabic, this UTF-8 character or ASCII letter must be LTR |
||
279 | $newState = 'LTR'; |
||
280 | break; |
||
281 | } |
||
282 | if ($closeParIndex !== false) { |
||
283 | // This closing parenthesis has to inherit the matching opening parenthesis' directionality |
||
284 | if (!empty($openParDirection[$closeParIndex]) && $openParDirection[$closeParIndex] != '?') { |
||
285 | $newState = $openParDirection[$closeParIndex]; |
||
286 | } |
||
287 | $openParDirection[$closeParIndex] = ''; |
||
288 | break; |
||
289 | } |
||
290 | if ($openParIndex !== false) { |
||
291 | // Opening parentheses always inherit the following directionality |
||
292 | self::$waitingText .= $currentLetter; |
||
293 | $workingText = substr($workingText, $currentLen); |
||
294 | while (true) { |
||
295 | if ($workingText === '') { |
||
296 | break; |
||
297 | } |
||
298 | if (substr($workingText, 0, 1) === ' ') { |
||
299 | // Spaces following this left parenthesis inherit the following directionality too |
||
300 | self::$waitingText .= ' '; |
||
301 | $workingText = substr($workingText, 1); |
||
302 | continue; |
||
303 | } |
||
304 | if (substr($workingText, 0, 6) === ' ') { |
||
305 | // Spaces following this left parenthesis inherit the following directionality too |
||
306 | self::$waitingText .= ' '; |
||
307 | $workingText = substr($workingText, 6); |
||
308 | continue; |
||
309 | } |
||
310 | break; |
||
311 | } |
||
312 | $openParDirection[$openParIndex] = '?'; |
||
313 | break 2; // double break because we're waiting for more information |
||
314 | } |
||
315 | |||
316 | // We have a digit or a "normal" special character. |
||
317 | // |
||
318 | // When this character is not at the start of the input string, it inherits the preceding directionality; |
||
319 | // at the start of the input string, it assumes the following directionality. |
||
320 | // |
||
321 | // Exceptions to this rule will be handled later during final clean-up. |
||
322 | // |
||
323 | self::$waitingText .= $currentLetter; |
||
324 | $workingText = substr($workingText, $currentLen); |
||
325 | if (self::$currentState != '') { |
||
326 | $result .= self::$waitingText; |
||
327 | self::$waitingText = ''; |
||
328 | } |
||
329 | break 2; // double break because we're waiting for more information |
||
330 | } |
||
331 | if ($newState != self::$currentState) { |
||
332 | // A direction change has occurred |
||
333 | self::finishCurrentSpan($result, false); |
||
334 | self::$previousState = self::$currentState; |
||
335 | self::$currentState = $newState; |
||
336 | self::beginCurrentSpan($result); |
||
337 | } |
||
338 | self::$waitingText .= $currentLetter; |
||
339 | $workingText = substr($workingText, $currentLen); |
||
340 | $result .= self::$waitingText; |
||
341 | self::$waitingText = ''; |
||
342 | |||
343 | foreach ($openParDirection as $index => $value) { |
||
344 | // Since we now know the proper direction, remember it for all waiting opening parentheses |
||
345 | if ($value === '?') { |
||
346 | $openParDirection[$index] = self::$currentState; |
||
347 | } |
||
348 | } |
||
349 | |||
350 | break; |
||
351 | } |
||
352 | } |
||
353 | |||
354 | // We're done. Finish last <span> if necessary |
||
355 | if ($numberState) { |
||
356 | if (self::$waitingText === '') { |
||
357 | if (self::$currentState === 'RTL') { |
||
358 | $result .= WT_UTF8_PDF; |
||
359 | } |
||
360 | } else { |
||
361 | if (self::$currentState === 'RTL') { |
||
362 | self::$waitingText .= WT_UTF8_PDF; |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | self::finishCurrentSpan($result, true); |
||
367 | |||
368 | // Get rid of any waiting text |
||
369 | if (self::$waitingText != '') { |
||
370 | if (I18N::direction() === 'rtl' && self::$currentState === 'LTR') { |
||
371 | $result .= self::$startRTL; |
||
372 | $result .= self::$waitingText; |
||
373 | $result .= self::$endRTL; |
||
374 | } else { |
||
375 | $result .= self::$startLTR; |
||
376 | $result .= self::$waitingText; |
||
377 | $result .= self::$endLTR; |
||
378 | } |
||
379 | self::$waitingText = ''; |
||
380 | } |
||
381 | |||
382 | // Lastly, do some more cleanups |
||
383 | |||
384 | // Move leading RTL numeric strings to following LTR text |
||
385 | // (this happens when the page direction is RTL and the original text begins with a number and is followed by LTR text) |
||
386 | while (substr($result, 0, self::$lenStart + 3) === self::$startRTL . WT_UTF8_LRE) { |
||
387 | $spanEnd = strpos($result, self::$endRTL . self::$startLTR); |
||
388 | if ($spanEnd === false) { |
||
389 | break; |
||
390 | } |
||
391 | $textSpan = self::stripLrmRlm(substr($result, self::$lenStart + 3, $spanEnd - self::$lenStart - 3)); |
||
392 | if (I18N::scriptDirection(I18N::textScript($textSpan)) === 'rtl') { |
||
393 | break; |
||
394 | } |
||
395 | $result = self::$startLTR . substr($result, self::$lenStart, $spanEnd - self::$lenStart) . substr($result, $spanEnd + self::$lenStart + self::$lenEnd); |
||
396 | break; |
||
397 | } |
||
398 | |||
399 | // On RTL pages, put trailing "." in RTL numeric strings into its own RTL span |
||
400 | if (I18N::direction() === 'rtl') { |
||
401 | $result = str_replace(WT_UTF8_PDF . '.' . self::$endRTL, WT_UTF8_PDF . self::$endRTL . self::$startRTL . '.' . self::$endRTL, $result); |
||
402 | } |
||
403 | |||
404 | // Trim trailing blanks preceding <br> in LTR text |
||
405 | while (self::$previousState != 'RTL') { |
||
406 | if (strpos($result, ' <LTRbr>') !== false) { |
||
407 | $result = str_replace(' <LTRbr>', '<LTRbr>', $result); |
||
408 | continue; |
||
409 | } |
||
410 | if (strpos($result, ' <LTRbr>') !== false) { |
||
411 | $result = str_replace(' <LTRbr>', '<LTRbr>', $result); |
||
412 | continue; |
||
413 | } |
||
414 | if (strpos($result, ' <br>') !== false) { |
||
415 | $result = str_replace(' <br>', '<br>', $result); |
||
416 | continue; |
||
417 | } |
||
418 | if (strpos($result, ' <br>') !== false) { |
||
419 | $result = str_replace(' <br>', '<br>', $result); |
||
420 | continue; |
||
421 | } |
||
422 | break; // Neither space nor : we're done |
||
423 | } |
||
424 | |||
425 | // Trim trailing blanks preceding <br> in RTL text |
||
426 | while (true) { |
||
427 | if (strpos($result, ' <RTLbr>') !== false) { |
||
428 | $result = str_replace(' <RTLbr>', '<RTLbr>', $result); |
||
429 | continue; |
||
430 | } |
||
431 | if (strpos($result, ' <RTLbr>') !== false) { |
||
432 | $result = str_replace(' <RTLbr>', '<RTLbr>', $result); |
||
433 | continue; |
||
434 | } |
||
435 | break; // Neither space nor : we're done |
||
436 | } |
||
437 | |||
438 | // Convert '<LTRbr>' and '<RTLbr /' |
||
439 | $result = str_replace(array('<LTRbr>', '<RTLbr>'), array(self::$endLTR . '<br>' . self::$startLTR, self::$endRTL . '<br>' . self::$startRTL), $result); |
||
440 | |||
441 | // Include leading indeterminate directional text in whatever follows |
||
442 | if (substr($result . "\n", 0, self::$lenStart) != self::$startLTR && substr($result . "\n", 0, self::$lenStart) != self::$startRTL && substr($result . "\n", 0, 6) != '<br>') { |
||
443 | $leadingText = ''; |
||
444 | while (true) { |
||
445 | if ($result == '') { |
||
446 | $result = $leadingText; |
||
447 | break; |
||
448 | } |
||
449 | if (substr($result . "\n", 0, self::$lenStart) != self::$startLTR && substr($result . "\n", 0, self::$lenStart) != self::$startRTL) { |
||
450 | $leadingText .= substr($result, 0, 1); |
||
451 | $result = substr($result, 1); |
||
452 | continue; |
||
453 | } |
||
454 | $result = substr($result, 0, self::$lenStart) . $leadingText . substr($result, self::$lenStart); |
||
455 | break; |
||
456 | } |
||
457 | } |
||
458 | |||
459 | // Include solitary "-" and "+" in surrounding RTL text |
||
460 | $result = str_replace(array(self::$endRTL . self::$startLTR . '-' . self::$endLTR . self::$startRTL, self::$endRTL . self::$startLTR . '-' . self::$endLTR . self::$startRTL), array('-', '+'), $result); |
||
461 | |||
462 | // Remove empty spans |
||
463 | $result = str_replace(array(self::$startLTR . self::$endLTR, self::$startRTL . self::$endRTL), '', $result); |
||
464 | |||
465 | // Finally, correct '<LTR>', '</LTR>', '<RTL>', and '</RTL>' |
||
466 | switch ($direction) { |
||
467 | case 'BOTH': |
||
468 | case 'both': |
||
469 | // LTR text: <span dir="ltr"> text </span> |
||
470 | // RTL text: <span dir="rtl"> text </span> |
||
471 | $sLTR = '<span dir="ltr" ' . $class . '>' . $nothing; |
||
472 | $eLTR = $nothing . '</span>'; |
||
473 | $sRTL = '<span dir="rtl" ' . $class . '>' . $nothing; |
||
474 | $eRTL = $nothing . '</span>'; |
||
475 | break; |
||
476 | case 'LTR': |
||
477 | case 'ltr': |
||
478 | // LTR text: <span dir="ltr"> text </span> |
||
479 | // RTL text: text |
||
480 | $sLTR = '<span dir="ltr" ' . $class . '>' . $nothing; |
||
481 | $eLTR = $nothing . '</span>'; |
||
482 | $sRTL = ''; |
||
483 | $eRTL = ''; |
||
484 | break; |
||
485 | case 'RTL': |
||
486 | case 'rtl': |
||
487 | default: |
||
488 | // LTR text: text |
||
489 | // RTL text: <span dir="rtl"> text </span> |
||
490 | $sLTR = ''; |
||
491 | $eLTR = ''; |
||
492 | $sRTL = '<span dir="rtl" ' . $class . '>' . $nothing; |
||
493 | $eRTL = $nothing . '</span>'; |
||
494 | break; |
||
495 | } |
||
496 | $result = str_replace(array(self::$startLTR, self::$endLTR, self::$startRTL, self::$endRTL), array($sLTR, $eLTR, $sRTL, $eRTL), $result); |
||
497 | |||
498 | return $result; |
||
499 | } |
||
1169 |