diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 77018f3d..e33ef682 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -35,28 +35,28 @@ class TemplateProcessor /** * @var string Temporary document filename (with path). */ - protected $temporaryDocumentFilename; + protected $tempDocumentFilename; /** * Content of main document part (in XML format) of the temporary document. * * @var string */ - protected $temporaryDocumentMainPart; + protected $tempDocumentMainPart; /** * Content of headers (in XML format) of the temporary document. * * @var string[] */ - protected $temporaryDocumentHeaders = array(); + protected $tempDocumentHeaders = array(); /** * Content of footers (in XML format) of the temporary document. * * @var string[] */ - protected $temporaryDocumentFooters = array(); + protected $tempDocumentFooters = array(); /** * @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception. @@ -68,34 +68,34 @@ class TemplateProcessor public function __construct($documentTemplate) { // Temporary document filename initialization - $this->temporaryDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord'); - if (false === $this->temporaryDocumentFilename) { + $this->tempDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord'); + if (false === $this->tempDocumentFilename) { throw new CreateTemporaryFileException(); } // Template file cloning - if (false === copy($documentTemplate, $this->temporaryDocumentFilename)) { - throw new CopyFileException($documentTemplate, $this->temporaryDocumentFilename); + if (false === copy($documentTemplate, $this->tempDocumentFilename)) { + throw new CopyFileException($documentTemplate, $this->tempDocumentFilename); } // Temporary document content extraction $this->zipClass = new ZipArchive(); - $this->zipClass->open($this->temporaryDocumentFilename); + $this->zipClass->open($this->tempDocumentFilename); $index = 1; while (false !== $this->zipClass->locateName($this->getHeaderName($index))) { - $this->temporaryDocumentHeaders[$index] = $this->fixBrokenMacros( + $this->tempDocumentHeaders[$index] = $this->fixBrokenMacros( $this->zipClass->getFromName($this->getHeaderName($index)) ); $index++; } $index = 1; while (false !== $this->zipClass->locateName($this->getFooterName($index))) { - $this->temporaryDocumentFooters[$index] = $this->fixBrokenMacros( + $this->tempDocumentFooters[$index] = $this->fixBrokenMacros( $this->zipClass->getFromName($this->getFooterName($index)) ); $index++; } - $this->temporaryDocumentMainPart = $this->fixBrokenMacros($this->zipClass->getFromName('word/document.xml')); + $this->tempDocumentMainPart = $this->fixBrokenMacros($this->zipClass->getFromName('word/document.xml')); } /** @@ -118,7 +118,7 @@ class TemplateProcessor } $xmlDOMDocument = new \DOMDocument(); - if (false === $xmlDOMDocument->loadXML($this->temporaryDocumentMainPart)) { + if (false === $xmlDOMDocument->loadXML($this->tempDocumentMainPart)) { throw new Exception('Could not load XML from the given template.'); } @@ -127,7 +127,7 @@ class TemplateProcessor throw new Exception('Could not transform the given XML document.'); } - $this->temporaryDocumentMainPart = $xmlTransformed; + $this->tempDocumentMainPart = $xmlTransformed; } /** @@ -138,14 +138,14 @@ class TemplateProcessor */ public function setValue($search, $replace, $limit = -1) { - foreach ($this->temporaryDocumentHeaders as $index => $headerXML) { - $this->temporaryDocumentHeaders[$index] = $this->setValueForPart($this->temporaryDocumentHeaders[$index], $search, $replace, $limit); + foreach ($this->tempDocumentHeaders as $index => $headerXML) { + $this->tempDocumentHeaders[$index] = $this->setValueForPart($this->tempDocumentHeaders[$index], $search, $replace, $limit); } - $this->temporaryDocumentMainPart = $this->setValueForPart($this->temporaryDocumentMainPart, $search, $replace, $limit); + $this->tempDocumentMainPart = $this->setValueForPart($this->tempDocumentMainPart, $search, $replace, $limit); - foreach ($this->temporaryDocumentFooters as $index => $headerXML) { - $this->temporaryDocumentFooters[$index] = $this->setValueForPart($this->temporaryDocumentFooters[$index], $search, $replace, $limit); + foreach ($this->tempDocumentFooters as $index => $headerXML) { + $this->tempDocumentFooters[$index] = $this->setValueForPart($this->tempDocumentFooters[$index], $search, $replace, $limit); } } @@ -156,13 +156,13 @@ class TemplateProcessor */ public function getVariables() { - $variables = $this->getVariablesForPart($this->temporaryDocumentMainPart); + $variables = $this->getVariablesForPart($this->tempDocumentMainPart); - foreach ($this->temporaryDocumentHeaders as $headerXML) { + foreach ($this->tempDocumentHeaders as $headerXML) { $variables = array_merge($variables, $this->getVariablesForPart($headerXML)); } - foreach ($this->temporaryDocumentFooters as $footerXML) { + foreach ($this->tempDocumentFooters as $footerXML) { $variables = array_merge($variables, $this->getVariablesForPart($footerXML)); } @@ -183,7 +183,7 @@ class TemplateProcessor $search = '${' . $search . '}'; } - $tagPos = strpos($this->temporaryDocumentMainPart, $search); + $tagPos = strpos($this->tempDocumentMainPart, $search); if (!$tagPos) { throw new Exception("Can not clone row, template variable not found or variable contains markup."); } @@ -223,7 +223,7 @@ class TemplateProcessor } $result .= $this->getSlice($rowEnd); - $this->temporaryDocumentMainPart = $result; + $this->tempDocumentMainPart = $result; } /** @@ -239,7 +239,7 @@ class TemplateProcessor $xmlBlock = null; preg_match( '/(<\?xml.*)(\${' . $blockname . '}<\/w:.*?p>)(.*)()/is', - $this->temporaryDocumentMainPart, + $this->tempDocumentMainPart, $matches ); @@ -251,10 +251,10 @@ class TemplateProcessor } if ($replace) { - $this->temporaryDocumentMainPart = str_replace( + $this->tempDocumentMainPart = str_replace( $matches[2] . $matches[3] . $matches[4], implode('', $cloned), - $this->temporaryDocumentMainPart + $this->tempDocumentMainPart ); } } @@ -273,15 +273,15 @@ class TemplateProcessor { preg_match( '/(<\?xml.*)(\${' . $blockname . '}<\/w:.*?p>)(.*)()/is', - $this->temporaryDocumentMainPart, + $this->tempDocumentMainPart, $matches ); if (isset($matches[3])) { - $this->temporaryDocumentMainPart = str_replace( + $this->tempDocumentMainPart = str_replace( $matches[2] . $matches[3] . $matches[4], $replacement, - $this->temporaryDocumentMainPart + $this->tempDocumentMainPart ); } } @@ -305,14 +305,14 @@ class TemplateProcessor */ public function save() { - foreach ($this->temporaryDocumentHeaders as $index => $headerXML) { - $this->zipClass->addFromString($this->getHeaderName($index), $this->temporaryDocumentHeaders[$index]); + foreach ($this->tempDocumentHeaders as $index => $headerXML) { + $this->zipClass->addFromString($this->getHeaderName($index), $this->tempDocumentHeaders[$index]); } - $this->zipClass->addFromString('word/document.xml', $this->temporaryDocumentMainPart); + $this->zipClass->addFromString('word/document.xml', $this->tempDocumentMainPart); - foreach ($this->temporaryDocumentFooters as $index => $headerXML) { - $this->zipClass->addFromString($this->getFooterName($index), $this->temporaryDocumentFooters[$index]); + foreach ($this->tempDocumentFooters as $index => $headerXML) { + $this->zipClass->addFromString($this->getFooterName($index), $this->tempDocumentFooters[$index]); } // Close zip file @@ -320,7 +320,7 @@ class TemplateProcessor throw new Exception('Could not close zip file.'); } - return $this->temporaryDocumentFilename; + return $this->tempDocumentFilename; } /** @@ -435,10 +435,10 @@ class TemplateProcessor */ protected function findRowStart($offset) { - $rowStart = strrpos($this->temporaryDocumentMainPart, 'temporaryDocumentMainPart) - $offset) * -1)); + $rowStart = strrpos($this->tempDocumentMainPart, 'tempDocumentMainPart) - $offset) * -1)); if (!$rowStart) { - $rowStart = strrpos($this->temporaryDocumentMainPart, '', ((strlen($this->temporaryDocumentMainPart) - $offset) * -1)); + $rowStart = strrpos($this->tempDocumentMainPart, '', ((strlen($this->tempDocumentMainPart) - $offset) * -1)); } if (!$rowStart) { throw new Exception('Can not find the start position of the row to clone.'); @@ -455,7 +455,7 @@ class TemplateProcessor */ protected function findRowEnd($offset) { - return strpos($this->temporaryDocumentMainPart, '', $offset) + 7; + return strpos($this->tempDocumentMainPart, '', $offset) + 7; } /** @@ -468,9 +468,9 @@ class TemplateProcessor protected function getSlice($startPosition, $endPosition = 0) { if (!$endPosition) { - $endPosition = strlen($this->temporaryDocumentMainPart); + $endPosition = strlen($this->tempDocumentMainPart); } - return substr($this->temporaryDocumentMainPart, $startPosition, ($endPosition - $startPosition)); + return substr($this->tempDocumentMainPart, $startPosition, ($endPosition - $startPosition)); } }