1818
1919namespace PhpOffice \PhpWord \Shared ;
2020
21- use PhpOffice \PhpWord \Exception \Exception ;
21+ use PhpOffice \PhpWord \Exception \Exception as WordException ;
2222
23- /**
24- * Text.
25- */
2623class Text
2724{
2825 /**
@@ -37,6 +34,9 @@ class Text
3734 */
3835 private static function buildControlCharacters (): void
3936 {
37+ if (!empty (self ::$ controlCharacters )) {
38+ return ;
39+ }
4040 for ($ i = 0 ; $ i <= 19 ; ++$ i ) {
4141 if ($ i != 9 && $ i != 10 && $ i != 13 ) {
4242 $ find = '_x ' . sprintf ('%04s ' , strtoupper (dechex ($ i ))) . '_ ' ;
@@ -63,9 +63,7 @@ private static function buildControlCharacters(): void
6363 */
6464 public static function controlCharacterPHP2OOXML ($ value = '' )
6565 {
66- if (empty (self ::$ controlCharacters )) {
67- self ::buildControlCharacters ();
68- }
66+ self ::buildControlCharacters ();
6967
7068 return str_replace (array_values (self ::$ controlCharacters ), array_keys (self ::$ controlCharacters ), $ value );
7169 }
@@ -119,9 +117,7 @@ public static function chr($dec)
119117 */
120118 public static function controlCharacterOOXML2PHP ($ value = '' )
121119 {
122- if (empty (self ::$ controlCharacters )) {
123- self ::buildControlCharacters ();
124- }
120+ self ::buildControlCharacters ();
125121
126122 return str_replace (array_keys (self ::$ controlCharacters ), array_values (self ::$ controlCharacters ), $ value );
127123 }
@@ -149,15 +145,26 @@ public static function toUTF8($value = '')
149145 {
150146 if (null !== $ value && !self ::isUTF8 ($ value )) {
151147 // PHP8.2 : utf8_encode is deprecated, but mb_convert_encoding always usable
152- $ value = ( function_exists ( ' mb_convert_encoding ' )) ? mb_convert_encoding ( $ value , ' UTF-8 ' , ' ISO-8859-1 ' ) : utf8_encode ($ value );
148+ $ value = static :: mbConvertEncoding ($ value );
153149 if ($ value === false ) {
154- throw new Exception ('Unable to convert text to UTF-8 ' );
150+ throw new WordException ('Unable to convert text to UTF-8 ' );
155151 }
156152 }
157153
158154 return $ value ;
159155 }
160156
157+ /**
158+ * @param string $value
159+ *
160+ * @return false|string
161+ */
162+ protected static function mbConvertEncoding ($ value )
163+ {
164+ // PHP8.2 : utf8_encode is deprecated, but mb_convert_encoding always usable
165+ return (function_exists ('mb_convert_encoding ' )) ? mb_convert_encoding ($ value , 'UTF-8 ' , 'ISO-8859-1 ' ) : utf8_encode ($ value );
166+ }
167+
161168 /**
162169 * Returns unicode from UTF8 text.
163170 *
0 commit comments