StringUtils   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 10
Bugs 2 Features 1
Metric Value
wmc 21
c 10
b 2
f 1
lcom 1
cbo 0
dl 0
loc 180
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A isMbstringEnabled() 0 4 1
A isIconvEnabled() 0 4 1
A isMbstringOrIconvEnabled() 0 4 2
A toBiff8UnicodeShort() 0 4 1
A toBiff8UnicodeLong() 0 4 1
A toBiff8Unicode() 0 13 3
A toBiff8UnicodeLongWoLenInfo() 0 4 1
A convertEncoding() 0 12 3
A countCharacters() 0 13 3
A substr() 0 13 3
A toUtf16Le() 0 4 1
A toNullTerminatedWchar() 0 7 1
1
<?php
2
namespace Xls;
3
4
class StringUtils
5
{
6
    const STRING_REGEXP_FRACTION = '(-?)(\d+)\s+(\d+\/\d+)';
7
8
    /**
9
     * Get whether mbstring extension is available
10
     *
11
     * @return boolean
12
     */
13
    public static function isMbstringEnabled()
14
    {
15
        return function_exists('mb_convert_encoding');
16
    }
17
18
    /**
19
     * Get whether iconv extension is available
20
     *
21
     * @return boolean
22
     */
23
    public static function isIconvEnabled()
24
    {
25
        return function_exists('iconv');
26
    }
27
28
    /**
29
     * @return bool
30
     */
31
    public static function isMbstringOrIconvEnabled()
32
    {
33
        return self::isMbstringEnabled() || self::isIconvEnabled();
34
    }
35
36
    /**
37
     * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
38
     * Writes the string using uncompressed notation, no rich text, no Asian phonetics
39
     * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
40
     * although this will give wrong results for non-ASCII strings
41
     * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
42
     *
43
     * @param string  $value    UTF-8 encoded string
44
     * @return string
45
     */
46
    public static function toBiff8UnicodeShort($value)
47
    {
48
        return self::toBiff8Unicode($value, 8);
49
    }
50
51
    /**
52
     * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
53
     * Writes the string using uncompressed notation, no rich text, no Asian phonetics
54
     * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
55
     * although this will give wrong results for non-ASCII strings
56
     * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
57
     *
58
     * @param string $value UTF-8 encoded string
59
     * @return string
60
     */
61
    public static function toBiff8UnicodeLong($value)
62
    {
63
        return self::toBiff8Unicode($value, 16);
64
    }
65
66
    /**
67
     * @param string $value
68
     * @param int $lengthSize 8 or 16
69
     *
70
     * @return string
71
     */
72
    public static function toBiff8Unicode($value, $lengthSize)
73
    {
74
        $ln = self::countCharacters($value);
75
        $lengthFormat = ($lengthSize == 8) ? 'C' : 'v';
76
        $data = pack($lengthFormat, $ln);
77
78
        $opt = (self::isMbstringOrIconvEnabled()) ? 0x01 : 0x00;
79
        $data .= pack('C', $opt);
80
81
        $data .= self::toUtf16Le($value);
82
83
        return $data;
84
    }
85
86
    /**
87
     * @param $value
88
     *
89
     * @return string
90
     */
91
    public static function toBiff8UnicodeLongWoLenInfo($value)
92
    {
93
        return substr(self::toBiff8UnicodeLong($value), 2);
94
    }
95
96
    /**
97
     * Convert string from one encoding to another. First try mbstring, then iconv, finally strlen
98
     *
99
     * @param string $value
100
     * @param string $from Encoding to convert from, e.g. 'UTF-16LE'
101
     * @param string $to Encoding to convert to, e.g. 'UTF-8'
102
     * @return string
103
     */
104
    public static function convertEncoding($value, $from, $to)
105
    {
106
        if (self::isMbstringEnabled()) {
107
            return mb_convert_encoding($value, $to, $from);
108
        }
109
110
        if (self::isIconvEnabled()) {
111
            return iconv($from, $to, $value);
112
        }
113
114
        return $value;
115
    }
116
117
    /**
118
     * Get character count. First try mbstring, then iconv, finally strlen
119
     *
120
     * @param string $value
121
     * @param string $enc Encoding
122
     * @return int Character count
123
     */
124
    public static function countCharacters($value, $enc = 'UTF-8')
125
    {
126
        if (self::isMbstringEnabled()) {
127
            return mb_strlen($value, $enc);
128
        }
129
130
        if (self::isIconvEnabled()) {
131
            return iconv_strlen($value, $enc);
132
        }
133
134
        // else strlen
135
        return strlen($value);
136
    }
137
138
    /**
139
     * Get a substring of a UTF-8 encoded string. First try mbstring, then iconv, finally strlen
140
     *
141
     * @param string $str UTF-8 encoded string
142
     * @param int $start Start offset
143
     * @param int $length Maximum number of characters in substring
144
     *
145
     * @return string
146
     */
147
    public static function substr($str = '', $start = 0, $length = 0)
148
    {
149
        if (self::isMbstringEnabled()) {
150
            return mb_substr($str, $start, $length, 'UTF-8');
151
        }
152
153
        if (self::isIconvEnabled()) {
154
            return iconv_substr($str, $start, $length, 'UTF-8');
155
        }
156
157
        // else substr
158
        return substr($str, $start, $length);
159
    }
160
161
    /**
162
     * @param $value
163
     *
164
     * @return string
165
     */
166
    public static function toUtf16Le($value)
167
    {
168
        return self::convertEncoding($value, 'UTF-8', 'UTF-16LE');
169
    }
170
171
    /**
172
     * @param $str
173
     *
174
     * @return string
175
     */
176
    public static function toNullTerminatedWchar($str)
177
    {
178
        $str = join("\0", preg_split("''", $str, -1, PREG_SPLIT_NO_EMPTY));
179
        $str = $str . "\0\0\0";
180
181
        return $str;
182
    }
183
}
184