| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\IconvException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Retrieve internal configuration variables of iconv extension. |
| 9 |
* |
| 10 |
* @param string $type The value of the optional type can be: |
| 11 |
* |
| 12 |
* all |
| 13 |
* input_encoding |
| 14 |
* output_encoding |
| 15 |
* internal_encoding |
| 16 |
* |
| 17 |
* @return mixed Returns the current value of the internal configuration variable if |
| 18 |
* successful. |
| 19 |
* |
| 20 |
* If type is omitted or set to "all", |
| 21 |
* iconv_get_encoding returns an array that |
| 22 |
* stores all these variables. |
| 23 |
* @throws IconvException |
| 24 |
* |
| 25 |
*/ |
| 26 |
function iconv_get_encoding(string $type = "all") |
| 27 |
{ |
| 28 |
error_clear_last(); |
| 29 |
$result = \iconv_get_encoding($type); |
| 30 |
if ($result === false) { |
| 31 |
throw IconvException::createFromPhpError(); |
| 32 |
} |
| 33 |
return $result; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Changes the value of the internal configuration variable specified by |
| 39 |
* type to charset. |
| 40 |
* |
| 41 |
* @param string $type The value of type can be any one of these: |
| 42 |
* |
| 43 |
* input_encoding |
| 44 |
* output_encoding |
| 45 |
* internal_encoding |
| 46 |
* |
| 47 |
* @param string $charset The character set. |
| 48 |
* @throws IconvException |
| 49 |
* |
| 50 |
*/ |
| 51 |
function iconv_set_encoding(string $type, string $charset): void |
| 52 |
{ |
| 53 |
error_clear_last(); |
| 54 |
$result = \iconv_set_encoding($type, $charset); |
| 55 |
if ($result === false) { |
| 56 |
throw IconvException::createFromPhpError(); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Performs a character set conversion on the string |
| 63 |
* str from in_charset |
| 64 |
* to out_charset. |
| 65 |
* |
| 66 |
* @param string $in_charset The input charset. |
| 67 |
* @param string $out_charset The output charset. |
| 68 |
* |
| 69 |
* If you append the string //TRANSLIT to |
| 70 |
* out_charset transliteration is activated. This |
| 71 |
* means that when a character can't be represented in the target charset, |
| 72 |
* it can be approximated through one or several similarly looking |
| 73 |
* characters. If you append the string //IGNORE, |
| 74 |
* characters that cannot be represented in the target charset are silently |
| 75 |
* discarded. Otherwise, E_NOTICE is generated and the function |
| 76 |
* will return FALSE. |
| 77 |
* |
| 78 |
* If and how //TRANSLIT works exactly depends on the |
| 79 |
* system's iconv() implementation (cf. ICONV_IMPL). |
| 80 |
* Some implementations are known to ignore //TRANSLIT, |
| 81 |
* so the conversion is likely to fail for characters which are illegal for |
| 82 |
* the out_charset. |
| 83 |
* @param string $str The string to be converted. |
| 84 |
* @return string Returns the converted string. |
| 85 |
* @throws IconvException |
| 86 |
* |
| 87 |
*/ |
| 88 |
function iconv(string $in_charset, string $out_charset, string $str): string |
| 89 |
{ |
| 90 |
error_clear_last(); |
| 91 |
$result = \iconv($in_charset, $out_charset, $str); |
| 92 |
if ($result === false) { |
| 93 |
throw IconvException::createFromPhpError(); |
| 94 |
} |
| 95 |
return $result; |
| 96 |
} |
| 97 |
|