Completed
Pull Request — master (#363)
by Adrien
05:27 queued 01:48
created

ODS::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Box\Spout\Common\Escaper;
4
5
use Box\Spout\Common\Singleton;
6
7
/**
8
 * Class ODS
9
 * Provides functions to escape and unescape data for ODS files
10
 *
11
 * @package Box\Spout\Common\Escaper
12
 */
13
class ODS implements EscaperInterface
14
{
15
    use Singleton;
16
17
    /**
18
     * Escapes the given string to make it compatible with XLSX
19
     *
20
     * @param string $string The string to escape
21
     * @return string The escaped string
22
     */
23 111
    public function escape($string)
24
    {
25 111
        if (defined('ENT_DISALLOWED')) {
26
            // 'ENT_DISALLOWED' ensures that invalid characters in the given document type are replaced.
27
            // Otherwise control characters like a vertical tab "\v" will make the XML document unreadable by the XML processor
28
            // @link https://github.com/box/spout/issues/329
29 111
            $replacedString = htmlspecialchars($string, ENT_QUOTES | ENT_DISALLOWED);
30 111
        } else {
31
            // We are on hhvm or any other engine that does not support ENT_DISALLOWED
32
            $escapedString =  htmlspecialchars($string, ENT_QUOTES);
33
34
            // control characters values are from 0 to 1F (hex values) in the ASCII table
35
            // some characters should not be escaped though: "\t", "\r" and "\n".
36
            $regexPattern = '[\x00-\x08' .
37
                            // skipping "\t" (0x9) and "\n" (0xA)
38
                            '\x0B-\x0C' .
39
                            // skipping "\r" (0xD)
40
                            '\x0E-\x1F]';
41
            $replacedString = preg_replace("/$regexPattern/", '�', $escapedString);
42
        }
43
44 111
        return $replacedString;
45
    }
46
47
    /**
48
     * Unescapes the given string to make it compatible with XLSX
49
     *
50
     * @param string $string The string to unescape
51
     * @return string The unescaped string
52
     */
53 78
    public function unescape($string)
54
    {
55 78
        return htmlspecialchars_decode($string, ENT_QUOTES);
56
    }
57
}
58