Completed
Push — master ( 157598...de0654 )
by ignace nyamagana
06:10 queued 04:06
created

BOM   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 74
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 9 2
A match() 0 10 3
A isValid() 0 4 1
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
declare(strict_types=1);
14
15
namespace League\Csv;
16
17
use ReflectionClass;
18
19
/**
20
 *  Defines constants for common BOM sequences
21
 *
22
 * @package League.csv
23
 * @since  9.0.0
24
 * @author  Ignace Nyamagana Butera <[email protected]>
25
 *
26
 */
27
final class BOM
28
{
29
    /**
30
     *  UTF-8 BOM sequence
31
     */
32
    const UTF8 = "\xEF\xBB\xBF";
33
34
    /**
35
     * UTF-16 BE BOM sequence
36
     */
37
    const UTF16_BE = "\xFE\xFF";
38
39
    /**
40
     * UTF-16 LE BOM sequence
41
     */
42
    const UTF16_LE = "\xFF\xFE";
43
44
    /**
45
     * UTF-32 BE BOM sequence
46
     */
47
    const UTF32_BE = "\x00\x00\xFE\xFF";
48
49
    /**
50
     * UTF-32 LE BOM sequence
51
     */
52
    const UTF32_LE = "\xFF\xFE\x00\x00";
53
54
    /**
55
     * Returns all possible BOM sequences as an array
56
     *
57
     * @return string[]
58
     */
59 158
    private static function toArray(): array
60
    {
61 158
        static $cache;
62 158
        if (null === $cache) {
63 6
            $cache = (new ReflectionClass(BOM::class))->getConstants();
64
        }
65
66 158
        return $cache;
67
    }
68
69
    /**
70
     * Returns the BOM sequence found at the start of the string
71
     *
72
     * If no valid BOM sequence is found an empty string is returned
73
     *
74
     * @param string $str
75
     *
76
     * @return string
77
     */
78 152
    public static function match(string $str): string
79
    {
80 152
        foreach (self::toArray() as $sequence) {
81 152
            if (strpos($str, $sequence) === 0) {
82 87
                return $sequence;
83
            }
84
        }
85
86 130
        return '';
87
    }
88
89
    /**
90
     * Tell whether the submitted sequence is a valid BOM sequence
91
     *
92
     * @param string $sequence
93
     *
94
     * @return bool
95
     */
96 6
    public static function isValid(string $sequence): bool
97
    {
98 6
        return in_array($sequence, self::toArray(), true);
99
    }
100
}
101