Skip to content

Commit

Permalink
Fix formatting according to PSR2
Browse files Browse the repository at this point in the history
  • Loading branch information
Papoteur committed Mar 8, 2021
1 parent 55ab01e commit 848ed5f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
74 changes: 41 additions & 33 deletions src/PhpWord/TemplateProcessorOdt.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2018 PHPWord contributors
* @license http:https://www.gnu.org/licenses/lgpl.txt LGPL version 3
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2018 PHPWord contributors
* @license http:https://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord;
Expand Down Expand Up @@ -83,7 +83,7 @@ protected function readPartWithRels($fileName)
}

/**
* @param string $search
* @param string $search
* @param \PhpOffice\PhpWord\Element\AbstractElement $complexType
*/
public function setComplexValue($search, \PhpOffice\PhpWord\Element\AbstractElement $complexType)
Expand All @@ -93,7 +93,9 @@ public function setComplexValue($search, \PhpOffice\PhpWord\Element\AbstractElem

$xmlWriter = new XMLWriter();
$xmlWriter->setIndent(false);
/** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $elementWriter */
/**
* @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $elementWriter
*/
$elementWriter = new $objectClass($xmlWriter, $complexType, true);
$elementWriter->write();

Expand All @@ -107,7 +109,7 @@ public function setComplexValue($search, \PhpOffice\PhpWord\Element\AbstractElem
}

/**
* @param string $search
* @param string $search
* @param \PhpOffice\PhpWord\Element\AbstractElement $complexType
*/
public function setComplexBlock($search, \PhpOffice\PhpWord\Element\AbstractElement $complexType)
Expand All @@ -117,7 +119,9 @@ public function setComplexBlock($search, \PhpOffice\PhpWord\Element\AbstractElem

$xmlWriter = new XMLWriter();
$xmlWriter->setIndent(false);
/** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $elementWriter */
/**
* @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $elementWriter
*/
$elementWriter = new $objectClass($xmlWriter, $complexType, false);
$elementWriter->write();

Expand Down Expand Up @@ -154,14 +158,14 @@ private function addImage($partFileName, $rid, $imgPath, $imageMimeType)

// add image to manifest
$xmlImageRelation = str_replace(array('{NAME}', '{MIME}'), array($imgName, $imageMimeType), $manifestTpl);
$this->tempDocumentManifest = str_replace('</manifest:manifest>', $xmlImageRelation, $this->tempDocumentManifest ). '</manifest:manifest>';
$this->tempDocumentManifest = str_replace('</manifest:manifest>', $xmlImageRelation, $this->tempDocumentManifest). '</manifest:manifest>';
}
}

/**
* @param mixed $search
* @param mixed $replace Path to image, or array("path" => xx, "width" => yy, "height" => zz)
* @param int $limit
* @param int $limit
*/
public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT)
{
Expand Down Expand Up @@ -196,9 +200,12 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
$partVariables = $this->getVariablesForPart($partContent);

foreach ($searchReplace as $searchString => $replaceImage) {
$varsToReplace = array_filter($partVariables, function ($partVar) use ($searchString) {
return ($partVar == $searchString) || preg_match('/^' . preg_quote($searchString) . ':/', $partVar);
});
$varsToReplace = array_filter(
$partVariables,
function ($partVar) use ($searchString) {
return ($partVar == $searchString) || preg_match('/^' . preg_quote($searchString) . ':/', $partVar);
}
);

foreach ($varsToReplace as $varNameWithArgs) {
$varInlineArgs = $this->getImageArgs($varNameWithArgs);
Expand Down Expand Up @@ -235,7 +242,7 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
*
* @param mixed $search
* @param mixed $replace
* @param int $limit
* @param int $limit
*/
public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT)
{
Expand Down Expand Up @@ -276,9 +283,9 @@ public function getVariableCount()
{
$variables = $this->getVariablesForPart($this->tempDocumentMainPart);
$variables = array_merge(
$variables,
$this->getVariablesForPart($this->tempDocumentHeaders)
);
$variables,
$this->getVariablesForPart($this->tempDocumentHeaders)
);
return array_count_values($variables);
}

Expand All @@ -296,7 +303,7 @@ public function getVariables()
* Clone a table row in a template document.
*
* @param string $search
* @param int $numberOfClones
* @param int $numberOfClones
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
Expand Down Expand Up @@ -344,10 +351,10 @@ public function cloneRow($search, $numberOfClones)
* Clone a block.
*
* @param string $blockname
* @param int $clones How many time the block should be cloned
* @param bool $replace
* @param bool $indexVariables If true, any variables inside the block will be indexed (postfixed with #1, #2, ...)
* @param array $variableReplacements Array containing replacements for macros found inside the block to clone
* @param int $clones How many time the block should be cloned
* @param bool $replace
* @param bool $indexVariables If true, any variables inside the block will be indexed (postfixed with #1, #2, ...)
* @param array $variableReplacements Array containing replacements for macros found inside the block to clone
*
* @return string|null
*/
Expand Down Expand Up @@ -524,14 +531,16 @@ protected function findRowEnd($offset)
{
$endMarkup = '</table:table-row>';
$position = strpos($this->tempDocumentMainPart, $endMarkup, $offset);
if (!$position) throw new Exception("End of text before end of row.");
if (!$position) {
throw new Exception("End of text before end of row.");
}
return $position + strlen($endMarkup);
}

/**
* Splits a w:r/w:t into a list of w:r where each ${macro} is in a separate w:r
*
* @param string $text
* @param string $text
* @return string
*/
protected function splitTextIntoTexts($text)
Expand All @@ -550,15 +559,15 @@ protected function splitTextIntoTexts($text)
}

// Text between tags
preg_match('/[^<>]*>(.*)<\//',$text, $matches);
preg_match('/[^<>]*>(.*)<\//', $text, $matches);
$cdata = $matches[1];
$remainingText = $cdata;
preg_match_all('/(\${[^}]*})/',$cdata, $matches);
preg_match_all('/(\${[^}]*})/', $cdata, $matches);
if ($extractedTag == "text:p") {
$result = "<" . $extractedTag . $extractedStyle . ">";
foreach($matches[1] as $macro) {
$beforeText = stristr($remainingText, $macro, $before_needle=true);
if (strlen($beforeText) != 0){
foreach ($matches[1] as $macro) {
$beforeText = stristr($remainingText, $macro, $before_needle = true);
if (strlen($beforeText) != 0) {
$result .= "<text:span>". $beforeText . "</text:span>";
$result .= "<text:span>". $macro . "</text:span>";
}
Expand All @@ -568,12 +577,11 @@ protected function splitTextIntoTexts($text)
$result .= "<text:span>". $remainingText . "</text:span>";
}
$result .= "</" . $extractedTag . ">";
}
else {
} else {
$result = '';
foreach($matches[1] as $macro) {
$beforeText = stristr($remainingText, $macro, $before_needle=true);
if (strlen($beforeText) != 0){
foreach ($matches[1] as $macro) {
$beforeText = stristr($remainingText, $macro, $before_needle = true);
if (strlen($beforeText) != 0) {
$result .= "<text:span" .$extractedStyle . ">". $beforeText . "</text:span>";
}
$result .= "<text:span" .$extractedStyle . ">". $macro . "</text:span>";
Expand Down
3 changes: 1 addition & 2 deletions tests/PhpWord/TemplateProcessorOdtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public function testCloneBlockWithVariableReplacementsOdt()
$this->assertEquals('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:t>$</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve">15,000.00. </w:t></w:r><w:r w:rsidR="0056499B"><w:t>$</w:t></w:r><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>{</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>variable_name</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>}</w:t></w:r>');
$this->assertEquals('<w:t>$</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve">15,000.00. </w:t></w:r><w:r w:rsidR="0056499B"><w:t>${variable_name}</w:t></w:r>', $fixed);
$this->assertEquals('<w:t>$</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve">15,000.00. </w:t></w:r><w:r w:rsidR="0056499B"><w:t>${variable_name}</w:t></w:r>', $fixed);
}
*/

Expand Down Expand Up @@ -488,5 +488,4 @@ public function testFindXmlBlockStart()

$this->assertEquals($toFind, $templateProcessor->getSlice($position['start'], $position['end']));
}

}

0 comments on commit 848ed5f

Please sign in to comment.