Completed
Pull Request — develop_3.0 (#457)
by Adrien
02:34
created

ODS::unescape()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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