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

StringUtil   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 37
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0

2 Methods

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