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