Completed
Push — develop ( 3a4c83...2cf724 )
by Franck
13s queued 10s
created

Text::toUTF8()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 12
1
<?php
2
/**
3
 * This file is part of PHPOffice Common
4
 *
5
 * PHPOffice Common is free software distributed under the terms of the GNU Lesser
6
 * General Public License version 3 as published by the Free Software Foundation.
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code. For the full list of
10
 * contributors, visit https://github.com/PHPOffice/Common/contributors.
11
 *
12
 * @link        https://github.com/PHPOffice/Common
13
 * @copyright   2009-2016 PHPOffice Common contributors
14
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15
 */
16
17
namespace PhpOffice\Common;
18
19
/**
20
 * Text
21
 */
22
class Text
23
{
24
    /**
25
     * Control characters array
26
     *
27
     * @var string[]
28
     */
29
    private static $controlCharacters = array();
30
31
    /**
32
     * Build control characters array
33
     */
34 1
    private static function buildControlCharacters()
35
    {
36 1
        for ($i = 0; $i <= 19; ++$i) {
37 1
            if ($i != 9 && $i != 10 && $i != 13) {
38 1
                $find                            = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
39 1
                $replace                         = chr($i);
40 1
                self::$controlCharacters[$find] = $replace;
41 1
            }
42 1
        }
43 1
    }
44
45
    /**
46
     * Convert from PHP control character to OpenXML escaped control character
47
     *
48
     * Excel 2007 team:
49
     * ----------------
50
     * That's correct, control characters are stored directly in the shared-strings table.
51
     * We do encode characters that cannot be represented in XML using the following escape sequence:
52
     * _xHHHH_ where H represents a hexadecimal character in the character's value...
53
     * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
54
     * element or in the shared string <t> element.
55
     *
56
     * @param  string $value Value to escape
57
     * @return string
58
     */
59 1
    public static function controlCharacterPHP2OOXML($value = '')
60
    {
61 1
        if (empty(self::$controlCharacters)) {
62 1
            self::buildControlCharacters();
63 1
        }
64
65 1
        return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value);
66
    }
67
68
    /**
69
     * Return a number formatted for being integrated in xml files
70
     * @param float $number
71
     * @param integer $decimals
72
     */
73 1
    public static function numberFormat($number, $decimals)
74
    {
75 1
        return number_format($number, $decimals, '.', '');
76
    }
77
78
    /**
79
     * @param int $dec
80
     * @link http://stackoverflow.com/a/7153133/2235790
81
     * @author velcrow
82
     */
83 1
    public static function chr($dec)
84
    {
85 1
        if ($dec<=0x7F) {
86 1
            return chr($dec);
87
        }
88 1
        if ($dec<=0x7FF) {
89 1
            return chr(($dec>>6)+192).chr(($dec&63)+128);
90
        }
91 1
        if ($dec<=0xFFFF) {
92 1
            return chr(($dec>>12)+224).chr((($dec>>6)&63)+128).chr(($dec&63)+128);
93
        }
94 1
        if ($dec<=0x1FFFFF) {
95 1
            return chr(($dec>>18)+240).chr((($dec>>12)&63)+128).chr((($dec>>6)&63)+128).chr(($dec&63)+128);
96
        }
97 1
        return '';
98
    }
99
100
    /**
101
     * Convert from OpenXML escaped control character to PHP control character
102
     *
103
     * @param string $value Value to unescape
104
     * @return string
105
     */
106 1
    public static function controlCharacterOOXML2PHP($value = '')
107
    {
108 1
        if (empty(self::$controlCharacters)) {
109
            self::buildControlCharacters();
110
        }
111
112 1
        return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
113
    }
114
115
    /**
116
     * Check if a string contains UTF-8 data
117
     *
118
     * @param string $value
119
     * @return boolean
120
     */
121 1
    public static function isUTF8($value = '')
122
    {
123 1
        return is_string($value) && ($value === '' || preg_match('/^./su', $value) == 1);
124
    }
125
126
    /**
127
     * Return UTF8 encoded value
128
     *
129
     * @param string $value
130
     * @return string
131
     */
132
    public static function toUTF8($value = '')
133
    {
134
        if (!is_null($value) && !self::isUTF8($value)) {
135
            $value = utf8_encode($value);
136
        }
137
138
        return $value;
139
    }
140
141
    /**
142
     * Returns unicode from UTF8 text
143
     *
144
     * The function is splitted to reduce cyclomatic complexity
145
     *
146
     * @param string $text UTF8 text
147
     * @return string Unicode text
148
     * @since 0.11.0
149
     */
150 1
    public static function toUnicode($text)
151
    {
152 1
        return self::unicodeToEntities(self::utf8ToUnicode($text));
153
    }
154
155
    /**
156
     * Returns unicode array from UTF8 text
157
     *
158
     * @param string $text UTF8 text
159
     * @return array
160
     * @since 0.11.0
161
     * @link http://www.randomchaos.com/documents/?source=php_and_unicode
162
     */
163 1
    public static function utf8ToUnicode($text)
164
    {
165 1
        $unicode = array();
166 1
        $values = array();
167 1
        $lookingFor = 1;
168
169
        // Gets unicode for each character
170 1
        for ($i = 0; $i < strlen($text); $i++) {
171 1
            $thisValue = ord($text[$i]);
172 1
            if ($thisValue < 128) {
173 1
                $unicode[] = $thisValue;
174 1
            } else {
175 1
                if (count($values) == 0) {
176 1
                    $lookingFor = $thisValue < 224 ? 2 : 3;
177 1
                }
178 1
                $values[] = $thisValue;
179 1
                if (count($values) == $lookingFor) {
180 1
                    if ($lookingFor == 3) {
181 1
                        $number = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
182 1
                    } else {
183 1
                        $number = (($values[0] % 32) * 64) + ($values[1] % 64);
184
                    }
185 1
                    $unicode[] = $number;
186 1
                    $values = array();
187 1
                    $lookingFor = 1;
188 1
                }
189
            }
190 1
        }
191
192 1
        return $unicode;
193
    }
194
195
    /**
196
     * Returns entites from unicode array
197
     *
198
     * @param array $unicode
199
     * @return string
200
     * @since 0.11.0
201
     * @link http://www.randomchaos.com/documents/?source=php_and_unicode
202
     */
203 1
    private static function unicodeToEntities($unicode)
204
    {
205 1
        $entities = '';
206
207 1
        foreach ($unicode as $value) {
208 1
            if ($value != 65279) {
209 1
                $entities .= $value > 127 ? '\uc0{\u' . $value . '}' : chr($value);
210 1
            }
211 1
        }
212
213 1
        return $entities;
214
    }
215
216
    /**
217
     * Return name without underscore for < 0.10.0 variable name compatibility
218
     *
219
     * @param string $value
220
     * @return string
221
     */
222 1
    public static function removeUnderscorePrefix($value)
223
    {
224 1
        if (!is_null($value)) {
225 1
            if (substr($value, 0, 1) == '_') {
226 1
                $value = substr($value, 1);
227 1
            }
228 1
        }
229
230 1
        return $value;
231
    }
232
}
233