1 | <?php |
||
9 | class XLSX implements EscaperInterface |
||
10 | { |
||
11 | /** @var bool Whether the escaper has already been initialized */ |
||
12 | private $isAlreadyInitialized = false; |
||
13 | |||
14 | /** @var string Regex pattern to detect control characters that need to be escaped */ |
||
15 | private $escapableControlCharactersPattern; |
||
16 | |||
17 | /** @var string[] Map containing control characters to be escaped (key) and their escaped value (value) */ |
||
18 | private $controlCharactersEscapingMap; |
||
19 | |||
20 | /** @var string[] Map containing control characters to be escaped (value) and their escaped value (key) */ |
||
21 | private $controlCharactersEscapingReverseMap; |
||
22 | |||
23 | /** |
||
24 | * Initializes the control characters if not already done |
||
25 | */ |
||
26 | 106 | protected function initIfNeeded() |
|
36 | |||
37 | /** |
||
38 | * Escapes the given string to make it compatible with XLSX |
||
39 | * |
||
40 | * @param string $string The string to escape |
||
41 | * @return string The escaped string |
||
42 | */ |
||
43 | 50 | public function escape($string) |
|
54 | |||
55 | /** |
||
56 | * Unescapes the given string to make it compatible with XLSX |
||
57 | * |
||
58 | * @param string $string The string to unescape |
||
59 | * @return string The unescaped string |
||
60 | */ |
||
61 | 56 | public function unescape($string) |
|
75 | |||
76 | /** |
||
77 | * @return string Regex pattern containing all escapable control characters |
||
78 | */ |
||
79 | 106 | protected function getEscapableControlCharactersPattern() |
|
89 | |||
90 | /** |
||
91 | * Builds the map containing control characters to be escaped |
||
92 | * mapped to their escaped values. |
||
93 | * "\t", "\r" and "\n" don't need to be escaped. |
||
94 | * |
||
95 | * NOTE: the logic has been adapted from the XlsxWriter library (BSD License) |
||
96 | * @see https://github.com/jmcnamara/XlsxWriter/blob/f1e610f29/xlsxwriter/sharedstrings.py#L89 |
||
97 | * |
||
98 | * @return string[] |
||
99 | */ |
||
100 | 106 | protected function getControlCharactersEscapingMap() |
|
116 | |||
117 | /** |
||
118 | * Converts PHP control characters from the given string to OpenXML escaped control characters |
||
119 | * |
||
120 | * Excel escapes control characters with _xHHHH_ and also escapes any |
||
121 | * literal strings of that type by encoding the leading underscore. |
||
122 | * So "\0" -> _x0000_ and "_x0000_" -> _x005F_x0000_. |
||
123 | * |
||
124 | * NOTE: the logic has been adapted from the XlsxWriter library (BSD License) |
||
125 | * @see https://github.com/jmcnamara/XlsxWriter/blob/f1e610f29/xlsxwriter/sharedstrings.py#L89 |
||
126 | * |
||
127 | * @param string $string String to escape |
||
128 | * @return string |
||
129 | */ |
||
130 | 50 | protected function escapeControlCharacters($string) |
|
143 | |||
144 | /** |
||
145 | * Escapes the escape character: "_x0000_" -> "_x005F_x0000_" |
||
146 | * |
||
147 | * @param string $string String to escape |
||
148 | * @return string The escaped string |
||
149 | */ |
||
150 | 50 | protected function escapeEscapeCharacter($string) |
|
154 | |||
155 | /** |
||
156 | * Converts OpenXML escaped control characters from the given string to PHP control characters |
||
157 | * |
||
158 | * Excel escapes control characters with _xHHHH_ and also escapes any |
||
159 | * literal strings of that type by encoding the leading underscore. |
||
160 | * So "_x0000_" -> "\0" and "_x005F_x0000_" -> "_x0000_" |
||
161 | * |
||
162 | * NOTE: the logic has been adapted from the XlsxWriter library (BSD License) |
||
163 | * @see https://github.com/jmcnamara/XlsxWriter/blob/f1e610f29/xlsxwriter/sharedstrings.py#L89 |
||
164 | * |
||
165 | * @param string $string String to unescape |
||
166 | * @return string |
||
167 | */ |
||
168 | 56 | protected function unescapeControlCharacters($string) |
|
179 | |||
180 | /** |
||
181 | * Unecapes the escape character: "_x005F_x0000_" => "_x0000_" |
||
182 | * |
||
183 | * @param string $string String to unescape |
||
184 | * @return string The unescaped string |
||
185 | */ |
||
186 | 56 | protected function unescapeEscapeCharacter($string) |
|
190 | } |
||
191 |