|
17 | 17 |
|
18 | 18 | namespace PhpOffice\PhpWord; |
19 | 19 |
|
| 20 | +use PhpOffice\PhpWord\Escaper\RegExp; |
20 | 21 | use PhpOffice\PhpWord\Exception\CopyFileException; |
21 | 22 | use PhpOffice\PhpWord\Exception\CreateTemporaryFileException; |
22 | 23 | use PhpOffice\PhpWord\Exception\Exception; |
@@ -136,31 +137,61 @@ public function applyXslStyleSheet($xslDOMDocument, $xslOptions = array(), $xslO |
136 | 137 | } |
137 | 138 |
|
138 | 139 | /** |
139 | | - * @param mixed $macro |
140 | | - * @param mixed $replace |
141 | | - * @param integer $limit |
| 140 | + * @param string $macro |
142 | 141 | * |
143 | | - * @return void |
| 142 | + * @return string |
144 | 143 | */ |
145 | | - public function setValue($macro, $replace, $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT) |
| 144 | + protected static function ensureMacroCompleted($macro) |
146 | 145 | { |
147 | 146 | if (substr($macro, 0, 2) !== '${' && substr($macro, -1) !== '}') { |
148 | 147 | $macro = '${' . $macro . '}'; |
149 | 148 | } |
150 | 149 |
|
151 | | - if (!StringUtils::isValidUtf8($replace)) { |
152 | | - $replace = utf8_encode($replace); |
153 | | - } |
| 150 | + return $macro; |
| 151 | + } |
154 | 152 |
|
155 | | - foreach ($this->tempDocumentHeaders as $index => $headerXML) { |
156 | | - $this->tempDocumentHeaders[$index] = $this->setValueForPart($this->tempDocumentHeaders[$index], $macro, $replace, $limit); |
| 153 | + /** |
| 154 | + * @param string $subject |
| 155 | + * |
| 156 | + * @return string |
| 157 | + */ |
| 158 | + protected static function ensureUtf8Encoded($subject) |
| 159 | + { |
| 160 | + if (!StringUtils::isValidUtf8($subject)) { |
| 161 | + $subject = utf8_encode($subject); |
157 | 162 | } |
158 | 163 |
|
159 | | - $this->tempDocumentMainPart = $this->setValueForPart($this->tempDocumentMainPart, $macro, $replace, $limit); |
| 164 | + return $subject; |
| 165 | + } |
160 | 166 |
|
161 | | - foreach ($this->tempDocumentFooters as $index => $headerXML) { |
162 | | - $this->tempDocumentFooters[$index] = $this->setValueForPart($this->tempDocumentFooters[$index], $macro, $replace, $limit); |
| 167 | + /** |
| 168 | + * @param mixed $search |
| 169 | + * @param mixed $replace |
| 170 | + * @param integer $limit |
| 171 | + * |
| 172 | + * @return void |
| 173 | + */ |
| 174 | + public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT) |
| 175 | + { |
| 176 | + if (is_array($search)) { |
| 177 | + foreach ($search as &$item) { |
| 178 | + $item = self::ensureMacroCompleted($item); |
| 179 | + } |
| 180 | + } else { |
| 181 | + $search = self::ensureMacroCompleted($search); |
163 | 182 | } |
| 183 | + |
| 184 | + if (is_array($replace)) { |
| 185 | + foreach ($replace as &$item) { |
| 186 | + $item = self::ensureUtf8Encoded($item); |
| 187 | + } |
| 188 | + } else { |
| 189 | + $replace = self::ensureUtf8Encoded($replace); |
| 190 | + } |
| 191 | + |
| 192 | + $this->tempDocumentHeaders = $this->setValueForPart($search, $replace, $this->tempDocumentHeaders, $limit); |
| 193 | + $this->tempDocumentMainPart = $this->setValueForPart($search, $replace, $this->tempDocumentMainPart, $limit); |
| 194 | + $this->tempDocumentFooters = $this->setValueForPart($search, $replace, $this->tempDocumentFooters, $limit); |
164 | 195 | } |
165 | 196 |
|
166 | 197 | /** |
@@ -398,22 +429,20 @@ function ($match) { |
398 | 429 | /** |
399 | 430 | * Find and replace macros in the given XML section. |
400 | 431 | * |
| 432 | + * @param mixed $search |
| 433 | + * @param mixed $replace |
401 | 434 | * @param string $documentPartXML |
402 | | - * @param string $search |
403 | | - * @param string $replace |
404 | 435 | * @param integer $limit |
405 | 436 | * |
406 | 437 | * @return string |
407 | 438 | */ |
408 | | - protected function setValueForPart($documentPartXML, $search, $replace, $limit) |
| 439 | + protected function setValueForPart($search, $replace, $documentPartXML, $limit) |
409 | 440 | { |
410 | 441 | // Note: we can't use the same function for both cases here, because of performance considerations. |
411 | 442 | if (self::MAXIMUM_REPLACEMENTS_DEFAULT === $limit) { |
412 | 443 | return str_replace($search, $replace, $documentPartXML); |
413 | 444 | } else { |
414 | | - $regExpDelim = '/'; |
415 | | - $escapedSearch = preg_quote($search, $regExpDelim); |
416 | | - return preg_replace("{$regExpDelim}{$escapedSearch}{$regExpDelim}u", $replace, $documentPartXML, $limit); |
| 445 | + return preg_replace(RegExp::escape($search), $replace, $documentPartXML, $limit); |
417 | 446 | } |
418 | 447 | } |
419 | 448 |
|
|
0 commit comments