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

StringUtil   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 10
c 1
b 0
f 0
dl 0
loc 36
ccs 6
cts 6
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A stripBom() 0 7 2
A hasBom() 0 3 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