Completed
Pull Request — master (#7)
by Harry
02:53
created

Bom::getEncoding()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Graze\CsvToken\Csv;
4
5
use InvalidArgumentException;
6
7
class Bom
8
{
9
    /** UTF-8 BOM sequence */
10
    const BOM_UTF8 = "\xEF\xBB\xBF";
11
    /** UTF-16 BE BOM sequence */
12
    const BOM_UTF16_BE = "\xFE\xFF";
13
    /** UTF-16 LE BOM sequence */
14
    const BOM_UTF16_LE = "\xFF\xFE";
15
    /** UTF-32 BE BOM sequence */
16
    const BOM_UTF32_BE = "\x00\x00\xFE\xFF";
17
    /** UTF-32 LE BOM sequence */
18
    const BOM_UTF32_LE = "\x00\x00\xFF\xFE";
19
20
    /** @var string[] */
21
    static protected $encodingMap = [
22
        self::BOM_UTF8     => 'UTF-8',
23
        self::BOM_UTF16_BE => 'UTF-16BE',
24
        self::BOM_UTF16_LE => 'UTF-16LE',
25
        self::BOM_UTF32_BE => 'UTF-32BE',
26
        self::BOM_UTF32_LE => 'UTF-32LE',
27
    ];
28
29
    /**
30
     * @param string $bom
31
     *
32
     * @return string
33
     */
34 9
    public static function getEncoding($bom)
35
    {
36 9
        if (array_key_exists($bom, static::$encodingMap)) {
37 8
            return static::$encodingMap[$bom];
38
        } else {
39 1
            throw new InvalidArgumentException("Could not determine encoding from Byte Order Mark: " . $bom);
40
        }
41
    }
42
}
43