function ByteString::toCodePointString
Overrides AbstractString::toCodePointString
File
-
vendor/
symfony/ string/ ByteString.php, line 415
Class
- ByteString
- Represents a binary-safe string of bytes.
Namespace
Symfony\Component\StringCode
public function toCodePointString(?string $fromEncoding = null) : CodePointString {
$u = new CodePointString();
if (\in_array($fromEncoding, [
null,
'utf8',
'utf-8',
'UTF8',
'UTF-8',
], true) && preg_match('//u', $this->string)) {
$u->string = $this->string;
return $u;
}
set_error_handler(static fn($t, $m) => throw new InvalidArgumentException($m));
try {
try {
$validEncoding = false !== mb_detect_encoding($this->string, $fromEncoding ?? 'Windows-1252', true);
} catch (InvalidArgumentException $e) {
if (!\function_exists('iconv')) {
throw $e;
}
$u->string = iconv($fromEncoding ?? 'Windows-1252', 'UTF-8', $this->string);
return $u;
}
} finally {
restore_error_handler();
}
if (!$validEncoding) {
throw new InvalidArgumentException(\sprintf('Invalid "%s" string.', $fromEncoding ?? 'Windows-1252'));
}
$u->string = mb_convert_encoding($this->string, 'UTF-8', $fromEncoding ?? 'Windows-1252');
return $u;
}