1 | <?php |
||
13 | class XLSX implements EscaperInterface |
||
14 | { |
||
15 | use Singleton; |
||
16 | |||
17 | /** @var string Regex pattern to detect control characters that need to be escaped */ |
||
18 | protected $escapableControlCharactersPattern; |
||
19 | |||
20 | /** @var string[] Map containing control characters to be escaped (key) and their escaped value (value) */ |
||
21 | protected $controlCharactersEscapingMap; |
||
22 | |||
23 | /** @var string[] Map containing control characters to be escaped (value) and their escaped value (key) */ |
||
24 | protected $controlCharactersEscapingReverseMap; |
||
25 | |||
26 | /** |
||
27 | * Initializes the singleton instance |
||
28 | */ |
||
1 ignored issue
–
show
|
|||
29 | 3 | protected function init() |
|
35 | |||
36 | /** |
||
37 | * Escapes the given string to make it compatible with XLSX |
||
38 | * |
||
39 | * @param string $string The string to escape |
||
40 | * @return string The escaped string |
||
41 | */ |
||
42 | 117 | public function escape($string) |
|
49 | |||
50 | /** |
||
51 | * Unescapes the given string to make it compatible with XLSX |
||
52 | * |
||
53 | * @param string $string The string to unescape |
||
54 | * @return string The unescaped string |
||
55 | */ |
||
56 | 150 | public function unescape($string) |
|
63 | |||
64 | /** |
||
65 | * @return string Regex pattern containing all escapable control characters |
||
66 | */ |
||
67 | 3 | protected function getEscapableControlCharactersPattern() |
|
77 | |||
78 | /** |
||
79 | * Builds the map containing control characters to be escaped |
||
80 | * mapped to their escaped values. |
||
81 | * "\t", "\r" and "\n" don't need to be escaped. |
||
82 | * |
||
83 | * NOTE: the logic has been adapted from the XlsxWriter library (BSD License) |
||
84 | * @link https://github.com/jmcnamara/XlsxWriter/blob/f1e610f29/xlsxwriter/sharedstrings.py#L89 |
||
85 | * |
||
86 | * @return string[] |
||
87 | */ |
||
88 | 3 | protected function getControlCharactersEscapingMap() |
|
104 | |||
105 | /** |
||
106 | * Converts PHP control characters from the given string to OpenXML escaped control characters |
||
107 | * |
||
108 | * Excel escapes control characters with _xHHHH_ and also escapes any |
||
109 | * literal strings of that type by encoding the leading underscore. |
||
110 | * So "\0" -> _x0000_ and "_x0000_" -> _x005F_x0000_. |
||
111 | * |
||
112 | * NOTE: the logic has been adapted from the XlsxWriter library (BSD License) |
||
113 | * @link https://github.com/jmcnamara/XlsxWriter/blob/f1e610f29/xlsxwriter/sharedstrings.py#L89 |
||
114 | * |
||
115 | * @param string $string String to escape |
||
116 | * @return string |
||
117 | */ |
||
118 | 117 | protected function escapeControlCharacters($string) |
|
131 | |||
132 | /** |
||
133 | * Escapes the escape character: "_x0000_" -> "_x005F_x0000_" |
||
134 | * |
||
135 | * @param string $string String to escape |
||
136 | * @return string The escaped string |
||
137 | */ |
||
138 | 117 | protected function escapeEscapeCharacter($string) |
|
142 | |||
143 | /** |
||
144 | * Converts OpenXML escaped control characters from the given string to PHP control characters |
||
145 | * |
||
146 | * Excel escapes control characters with _xHHHH_ and also escapes any |
||
147 | * literal strings of that type by encoding the leading underscore. |
||
148 | * So "_x0000_" -> "\0" and "_x005F_x0000_" -> "_x0000_" |
||
149 | * |
||
150 | * NOTE: the logic has been adapted from the XlsxWriter library (BSD License) |
||
151 | * @link https://github.com/jmcnamara/XlsxWriter/blob/f1e610f29/xlsxwriter/sharedstrings.py#L89 |
||
152 | * |
||
153 | * @param string $string String to unescape |
||
154 | * @return string |
||
155 | */ |
||
156 | 150 | protected function unescapeControlCharacters($string) |
|
167 | |||
168 | /** |
||
169 | * Unecapes the escape character: "_x005F_x0000_" => "_x0000_" |
||
170 | * |
||
171 | * @param string $string String to unescape |
||
172 | * @return string The unescaped string |
||
173 | */ |
||
174 | 150 | protected function unescapeEscapeCharacter($string) |
|
178 | } |
||
179 |