Bom::getEncoding()   A
last analyzed

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
 * This file is part of graze/csv-token
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/csv-token/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/csv-token
12
 */
13
14
namespace Graze\CsvToken\Csv;
15
16
use InvalidArgumentException;
17
18
class Bom
19
{
20
    /** UTF-8 BOM sequence */
21
    const BOM_UTF8 = "\xEF\xBB\xBF";
22
    /** UTF-16 BE BOM sequence */
23
    const BOM_UTF16_BE = "\xFE\xFF";
24
    /** UTF-16 LE BOM sequence */
25
    const BOM_UTF16_LE = "\xFF\xFE";
26
    /** UTF-32 BE BOM sequence */
27
    const BOM_UTF32_BE = "\x00\x00\xFE\xFF";
28
    /** UTF-32 LE BOM sequence */
29
    const BOM_UTF32_LE = "\x00\x00\xFF\xFE";
30
31
    /** @var string[] */
32
    static protected $encodingMap = [
33
        self::BOM_UTF8     => 'UTF-8',
34
        self::BOM_UTF16_BE => 'UTF-16BE',
35
        self::BOM_UTF16_LE => 'UTF-16LE',
36
        self::BOM_UTF32_BE => 'UTF-32BE',
37
        self::BOM_UTF32_LE => 'UTF-32LE',
38
    ];
39
40
    /**
41
     * @param string $bom
42
     *
43
     * @return string
44
     */
45 9
    public static function getEncoding($bom)
46
    {
47 9
        if (array_key_exists($bom, static::$encodingMap)) {
48 8
            return static::$encodingMap[$bom];
49
        } else {
50 1
            throw new InvalidArgumentException("Could not determine encoding from Byte Order Mark: " . $bom);
51
        }
52
    }
53
}
54