Completed
Push — master ( 434cc0...3d33db )
by Andy
04:18
created

StringUtil::hasBom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Palmtree\Csv\Util;
4
5
class StringUtil
6
{
7
    const BOM_UTF8 = "\xEF\xBB\xBF";
8
    const BOM_UTF16_BE = "\xFE\xFF";
9
    const BOM_UTF16_LE = "\xFF\xFE";
10
    const BOM_UTF32_BE = "\x00\x00\xFE\xFF";
11
    const BOM_UTF32_LE = "\xFF\xFE\x00\x00";
12
13
    /**
14
     * Returns whether the given string starts with a UTF-8 Byte Order Mark.
15
     *
16
     * @param string $input
17
     *
18
     * @return bool
19
     */
20 2
    public static function hasBom($input, $bom)
21
    {
22 2
        return substr($input, 0, strlen($bom)) === $bom;
23
    }
24
25
    /**
26
     * Strips the UTF-8 Byte Order Mark from the beginning of a string
27
     * if it is present.
28
     *
29
     * @param string $input Data to be stripped of its BOM.
30
     *
31
     * @return string The stripped input string.
32
     */
33 1
    public static function stripBom($input, $bom)
34
    {
35 1
        if (self::hasBom($input, $bom)) {
36 1
            return substr($input, strlen($bom));
37
        }
38
39
        return $input;
40
    }
41
}
42