Passed
Push — master ( 41184f...2977bf )
by Fabio
14:32
created

TUtf8Converter::parseEncodingLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * TUtf8Converter class file
4
 *
5
 * @author Fabio Bas <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 */
9
10
namespace Prado\Util;
11
12
/**
13
 * TUtf8Converter class.
14
 *
15
 * TUtf8Converter is a simple wrapper around iconv functions to convert
16
 * strings from and to UTF-8.
17
 *
18
 * @author Fabio Bas <[email protected]>
19
 * @since 4.0.2
20
 */
21
class TUtf8Converter
22
{
23
	/**
24
	 * Convert strings to UTF-8 via iconv. NB, the result may not by UTF-8
25
	 * if the conversion failed.
26
	 * @param string $string string to convert to UTF-8
27
	 * @param string $from source encoding
28
	 * @param ?string $lang Language of the encoding as accepted by PHP setLocale
29
	 * @return string UTF-8 encoded string, original string if iconv failed.
30
	 * @see https://www.php.net/manual/en/function.setlocale.php
31
	 *   The $lang locale information is maintained per process, not per thread.
32
	 */
33
	public static function toUTF8($string, $from, $lang = null)
34
	{
35
		if ($from != 'UTF-8') {
36
			$locale = null;
37
			if ($lang === null) {
38
				self::parseEncodingLanguage($from, $lang);
39
			}
40
			if ($lang !== null) {
41
				$locale = setLocale(LC_CTYPE, '0');
42
				setLocale(LC_CTYPE, $lang);
43
			}
44
			$s = iconv($from, 'UTF-8', $string); //to UTF-8
45
			if ($lang !== null) {
46
				setLocale(LC_CTYPE, $locale);
0 ignored issues
show
Bug introduced by
It seems like $locale can also be of type null; however, parameter $locales of setlocale() does only seem to accept array|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
				setLocale(LC_CTYPE, /** @scrutinizer ignore-type */ $locale);
Loading history...
47
			}
48
			return $s !== false ? $s : $string; //it could return false
49
		}
50
		return $string;
51
	}
52
53
	/**
54
	 * Convert UTF-8 strings to a different encoding. NB. The result
55
	 * may not have been encoded if iconv fails.
56
	 * @param string $string the UTF-8 string for conversion
57
	 * @param string $to destination encoding
58
	 * @param ?string $lang Language of the encoding as accepted by PHP setLocale
59
	 * @return string encoded string.
60
	 * @see https://www.php.net/manual/en/function.setlocale.php
61
	 *   The $lang locale information is maintained per process, not per thread.
62
	 */
63
	public static function fromUTF8($string, $to, $lang = null)
64
	{
65
		if ($to != 'UTF-8') {
66
			$locale = null;
67
			if ($lang === null) {
68
				self::parseEncodingLanguage($to, $lang);
69
			}
70
			if ($lang !== null) {
71
				$locale = setLocale(LC_CTYPE, '0');
72
				setLocale(LC_CTYPE, $lang);
73
			}
74
			$s = iconv('UTF-8', $to, $string);
75
			if ($lang !== null) {
76
				setLocale(LC_CTYPE, $locale);
0 ignored issues
show
Bug introduced by
It seems like $locale can also be of type null; however, parameter $locales of setlocale() does only seem to accept array|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
				setLocale(LC_CTYPE, /** @scrutinizer ignore-type */ $locale);
Loading history...
77
			}
78
			return $s !== false ? $s : $string;
79
		}
80
		return $string;
81
	}
82
83
	/**
84
	 * This parses a Character Set Encoding for an appended/embedded language.
85
	 * eg "ASCII" can also be "ASCII.de" to designate German ASCII layout.
86
	 * In this example, at input $encoding is "ASCII.de" and on output $encoding
87
	 * is 'ASCII' with $lang is "de".
88
	 * @param string $encoding The character set encoding with optional period and
89
	 *   language appended.
90
	 * @param ?string &$lang The output language of the encoding.
91
	 */
92
	public static function parseEncodingLanguage(string &$encoding, &$lang)
93
	{
94
		if(strpos($encoding, '.') !== false) {
95
			$parts = explode($encoding, '.', 1);
96
			$encoding = $parts[0];
97
			$lang = $parts[1];
98
		}
99
	}
100
}
101