Lines 20.00% 2 / 10
Methods 0.00% 0 / 1
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 isUTF8 20.00% 2 / 10 0.00% 0 / 1 4.05
27abstract class AbstractString
28{
29    /**
30     * Default encoding.
31     *
32     * @var string
33     */
34    public const DEFAULT_ENCODING = 'utf-8';
35
36    /**
37     * Default language.
38     */
39    public const string DEFAULT_LANGUAGE = 'en';
40
41    /**
42     * Encoding.
43     */
44    protected string $encoding = self::DEFAULT_ENCODING;
45
46    /**
47     * Language.
48     */
49    protected string $language = self::DEFAULT_LANGUAGE;
50
51    /**
52     * Check if the string is a Unicode string.
53     *
54     * @param string $str String
55     */
56    public static function isUTF8(string $str): bool
57    {
58        if (function_exists('mb_detect_encoding')) {
59            return (bool) mb_detect_encoding($str, self::DEFAULT_ENCODING, strict: true);
60        }
61
62        $regex =
63            '/^([\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec][\x80-\xbf]{2}|'
64            . '\xed[\x80-\x9f][\x80-\xbf]|'
65            . '[\xee-\xef][\x80-\xbf]{2}|'
66            . '\xf0[\x90-\xbf][\x80-\xbf]{2}|'
67            . '[\xf1-\xf3][\x80-\xbf]{3}|'
68            . '\xf4[\x80-\x8f][\x80-\xbf]{2})*$/';
69
70        return preg_match($regex, $str) === 1;
71    }
72}