Bom::getBoms()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/*
4
 * This file is part of Bom.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/Bom
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\Bom;
11
12
/**
13
 * class Bom
14
 */
15
class Bom
16
{
17
    /**
18
     * UTF8 BOM & encoding
19
     */
20
    const BOM_UTF8 = "\xEF\xBB\xBF";
21
    const ENC_UTF8 = 'UTF-8';
22
23
    /**
24
     * UTF16_BE BOM & encoding
25
     */
26
    const BOM_UTF16_BE = "\xFE\xFF";
27
    const ENC_UTF16_BE = 'UTF-16BE';
28
29
    /**
30
     * UTF16_LE BOM & encoding
31
     */
32
    const BOM_UTF16_LE = "\xFF\xFE";
33
    const ENC_UTF16_LE = 'UTF-16LE';
34
35
    /**
36
     * UTF32_BE BOM & encoding
37
     */
38
    const BOM_UTF32_BE = "\x00\x00\xFE\xFF";
39
    const ENC_UTF32_BE = 'UTF-32BE';
40
41
    /**
42
     * UTF32_LE BOM & encoding
43
     */
44
    const BOM_UTF32_LE = "\xFF\xFE\x00\x00";
45
    const ENC_UTF32_LE = 'UTF-32LE';
46
47
    /**
48
     * UTF8 | UTF16_BE | UTF32_LE | UTF16_LE | UTF32_BE
49
     */
50
    const BOM_REGEX = '\xEF\xBB\xBF|\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE|\x00\x00\xFE\xFF';
51
52
    /**
53
     * @var string[]
54
     */
55
    protected static $boms = [
56
        self::ENC_UTF8     => self::BOM_UTF8,
57
        self::ENC_UTF16_BE => self::BOM_UTF16_BE,
58
        self::ENC_UTF16_LE => self::BOM_UTF16_LE,
59
        self::ENC_UTF32_BE => self::BOM_UTF32_BE,
60
        self::ENC_UTF32_LE => self::BOM_UTF32_LE,
61
    ];
62
63
    /**
64
     * @var string[]
65
     */
66
    protected static $smob = [
67
        self::BOM_UTF8     => self::ENC_UTF8,
68
        self::BOM_UTF16_BE => self::ENC_UTF16_BE,
69
        self::BOM_UTF16_LE => self::ENC_UTF16_LE,
70
        self::BOM_UTF32_BE => self::ENC_UTF32_BE,
71
        self::BOM_UTF32_LE => self::ENC_UTF32_LE,
72
    ];
73
74
    /**
75
     * @param string $string
76
     *
77
     * @return string|null
78
     */
79
    public static function extract(string $string): ? string
80
    {
81
        return preg_match('`^(' . static::BOM_REGEX . ')`', $string, $match) ? $match[1] : null;
82
    }
83
84
    /**
85
     * @param string $string
86
     *
87
     * @return string
88
     */
89
    public static function drop(string $string): string
90
    {
91
        return preg_replace('`^(' . static::BOM_REGEX . ')`', '', $string);
92
    }
93
94
    /**
95
     * @param string $bom
96
     *
97
     * @return string|null
98
     */
99
    public static function getBomEncoding(string $bom): ? string
100
    {
101
        return static::$smob[$bom] ?? null;
102
    }
103
104
    /**
105
     * @param string $encoding
106
     *
107
     * @return null|string
108
     */
109
    public static function getEncodingBom(string $encoding): ? string
110
    {
111
        return static::$boms[$encoding] ?? null;
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public static function getBoms(): array
118
    {
119
        return static::$boms;
120
    }
121
}
122