Completed
Push — master ( 1cc7be...c5c5ef )
by Andy
04:04
created

StringUtil::hasBom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
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 the given Byte Order Mark.
15
     *
16
     * @param string $input
17
     * @param string $bom
18
     *
19
     * @return bool
20
     */
21 4
    public static function hasBom($input, $bom)
22
    {
23 4
        return \strpos($input, $bom) === 0;
24
    }
25
26
    /**
27
     * Strips a Byte Order Mark from the beginning of a string if it is present.
28
     *
29
     * @param string $input Data to be stripped of its BOM.
30
     * @param string $bom
31
     *
32
     * @return string The stripped input string.
33
     */
34 4
    public static function stripBom($input, $bom)
35
    {
36 4
        if (self::hasBom($input, $bom)) {
37 1
            return \substr($input, \strlen($bom));
38
        }
39
40 3
        return $input;
41
    }
42
}
43