Skip to content

Commit

Permalink
Fix formatting with php-cs-fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
Papoteur committed Mar 8, 2021
1 parent 848ed5f commit fe36ed0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 64 deletions.
4 changes: 0 additions & 4 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@

namespace PhpOffice\PhpWord;

use PhpOffice\PhpWord\Escaper\RegExp;
use PhpOffice\PhpWord\Escaper\Xml;
use PhpOffice\PhpWord\Exception\CopyFileException;
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\Text;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Shared\ZipArchive;
use PhpOffice\PhpWord\TemplateProcessorCommon;

class TemplateProcessor extends TemplateProcessorCommon
{

/**
* @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception
*
Expand Down
4 changes: 0 additions & 4 deletions src/PhpWord/TemplateProcessorCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@

use PhpOffice\PhpWord\Escaper\RegExp;
use PhpOffice\PhpWord\Escaper\Xml;
use PhpOffice\PhpWord\Exception\CopyFileException;
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\Text;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Shared\ZipArchive;

class TemplateProcessorCommon
Expand Down Expand Up @@ -207,7 +204,6 @@ protected static function ensureUtf8Encoded($subject)
return $subject;
}


/**
* Set values from a one-dimensional array of "variable => value"-pairs.
*
Expand Down
65 changes: 32 additions & 33 deletions src/PhpWord/TemplateProcessorOdt.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

namespace PhpOffice\PhpWord;

use PhpOffice\PhpWord\Escaper\RegExp;
use PhpOffice\PhpWord\Escaper\Xml;
use PhpOffice\PhpWord\Exception\CopyFileException;
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\Text;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Shared\ZipArchive;
use PhpOffice\PhpWord\TemplateProcessorCommon;

class TemplateProcessorOdt extends TemplateProcessorCommon
{
Expand All @@ -43,7 +41,7 @@ class TemplateProcessorOdt extends TemplateProcessorCommon
*
* @var string[]
*/
protected $tempDocumentManifest = "";
protected $tempDocumentManifest = '';

public function __construct($documentTemplate)
{
Expand All @@ -69,7 +67,6 @@ public function __construct($documentTemplate)
//$this->tempDocumentContentTypes = $this->zipClass->getFromName($this->getDocumentContentTypesName());
}


/**
* @param string $fileName
*
Expand All @@ -95,7 +92,7 @@ public function setComplexValue($search, \PhpOffice\PhpWord\Element\AbstractElem
$xmlWriter->setIndent(false);
/**
* @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $elementWriter
*/
*/
$elementWriter = new $objectClass($xmlWriter, $complexType, true);
$elementWriter->write();

Expand All @@ -104,7 +101,7 @@ public function setComplexValue($search, \PhpOffice\PhpWord\Element\AbstractElem
$textParts = $this->splitTextIntoTexts($block);
$search = static::ensureMacroCompleted($search);
$this->replaceXmlBlock($search, $textParts, 'text:p');
$data = str_replace("\n", '', $xmlWriter->getData());
$data = str_replace("\n", '', $xmlWriter->getData());
$this->replaceXmlBlock($search, $data, 'text:span');
}

Expand All @@ -120,13 +117,13 @@ 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();

$search = static::ensureMacroCompleted($search);
$data = str_replace("\n", '', $xmlWriter->getData());
$data = str_replace("\n", '', $xmlWriter->getData());
$this->replaceXmlBlock($search, $data, 'text:p');
}

Expand Down Expand Up @@ -158,7 +155,7 @@ 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>';
}
}

Expand Down Expand Up @@ -286,6 +283,7 @@ public function getVariableCount()
$variables,
$this->getVariablesForPart($this->tempDocumentHeaders)
);

return array_count_values($variables);
}

Expand Down Expand Up @@ -324,9 +322,9 @@ public function cloneRow($search, $numberOfClones)
if (preg_match('/table:number-rows-spanned="([0-9]+)"/', $xmlRow, $matches)) {
// $extraRowStart = $rowEnd;
// Number of spanned rows
$num = intval($matches[1]);
$num = (int) ($matches[1]);
$extraRowEnd = $rowEnd;
for ($i = 0; $i < $num-1; $i++) {
for ($i = 0; $i < $num - 1; $i++) {
$extraRowStart = $this->findRowStart($extraRowEnd + 1);
$extraRowEnd = $this->findRowEnd($extraRowEnd + 1);

Expand Down Expand Up @@ -363,11 +361,11 @@ public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVaria
$xmlBlock = null;
$matches = array();
preg_match(
'/(<\?xml.*)(<[^<>]+>\${' . $blockname .'}<\/[^<>]+>)(.*)(<[^<>]+>\${\/'. $blockname . '}<\/[^<>]+>)/is',
'/(<\?xml.*)(<[^<>]+>\${' . $blockname . '}<\/[^<>]+>)(.*)(<[^<>]+>\${\/' . $blockname . '}<\/[^<>]+>)/is',
$this->tempDocumentMainPart,
$matches
);

if (isset($matches[3])) {
$xmlBlock = $matches[3];
if ($indexVariables) {
Expand All @@ -389,6 +387,7 @@ public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVaria
);
}
}

return $xmlBlock;
}

Expand All @@ -402,7 +401,7 @@ public function replaceBlock($blockname, $replacement)
{
$matches = array();
preg_match(
'/(<\?xml.*)(<[^<>]+>\${' . $blockname .'}<\/[^<>]+>)(.*)(<[^<>]+>\${\/'. $blockname . '}<\/[^<>]+>)/is',
'/(<\?xml.*)(<[^<>]+>\${' . $blockname . '}<\/[^<>]+>)(.*)(<[^<>]+>\${\/' . $blockname . '}<\/[^<>]+>)/is',
$this->tempDocumentMainPart,
$matches
);
Expand All @@ -423,7 +422,7 @@ public function replaceBlock($blockname, $replacement)
*/
public function setUpdateFields($update = true)
{
return "Not implemented";
return 'Not implemented';
}

/**
Expand Down Expand Up @@ -460,7 +459,6 @@ protected function savePart($fileName, $xml)
}

/**
*
* @return string
*/
protected function getMainPartName()
Expand All @@ -469,7 +467,6 @@ protected function getMainPartName()
}

/**
*
* @return string
*/
protected function getManifestName()
Expand Down Expand Up @@ -532,8 +529,9 @@ 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.");
throw new Exception('End of text before end of row.');
}

return $position + strlen($endMarkup);
}

Expand All @@ -550,47 +548,48 @@ protected function splitTextIntoTexts($text)
}
$matches = array();
if (preg_match('/([^<>]*)(\s[^<>]+)>/', $text, $matches)) {
$extractedTag = $matches[1];
$extractedTag = $matches[1];
$extractedStyle = $matches[2];
} else {
$extractedStyle = '';
preg_match('/([^<>]*)>/', $text, $matches);
$extractedTag = $matches[1];
$extractedTag = $matches[1];
}

// Text between tags
preg_match('/[^<>]*>(.*)<\//', $text, $matches);
$cdata = $matches[1];
$remainingText = $cdata;
preg_match_all('/(\${[^}]*})/', $cdata, $matches);
if ($extractedTag == "text:p") {
$result = "<" . $extractedTag . $extractedStyle . ">";
if ($extractedTag == 'text:p') {
$result = '<' . $extractedTag . $extractedStyle . '>';
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>";
$result .= '<text:span>' . $beforeText . '</text:span>';
$result .= '<text:span>' . $macro . '</text:span>';
}
$remainingText = substr($remainingText, strlen($beforeText) + strlen($macro));
}
if ($remainingText != "") {
$result .= "<text:span>". $remainingText . "</text:span>";
if ($remainingText != '') {
$result .= '<text:span>' . $remainingText . '</text:span>';
}
$result .= "</" . $extractedTag . ">";
$result .= '</' . $extractedTag . '>';
} else {
$result = '';
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 . '>' . $beforeText . '</text:span>';
}
$result .= "<text:span" .$extractedStyle . ">". $macro . "</text:span>";
$result .= '<text:span' . $extractedStyle . '>' . $macro . '</text:span>';
$remainingText = substr($remainingText, strlen($beforeText) + strlen($macro));
}
if ($remainingText != "") {
$result .= "<text:span" .$extractedStyle . ">". $remainingText . "</text:span>";
if ($remainingText != '') {
$result .= '<text:span' . $extractedStyle . '>' . $remainingText . '</text:span>';
}
}

return $result;
}
}
43 changes: 21 additions & 22 deletions tests/PhpWord/TemplateProcessorOdtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testTheConstructOdt()
$this->assertInstanceOf('PhpOffice\\PhpWord\\TemplateProcessorOdt', $object);
$this->assertEquals(array(), $object->getVariables());
}

/**
* @covers ::setValue
* @covers ::cloneRow
Expand Down Expand Up @@ -90,7 +90,6 @@ public function testCloneRowAndSetValuesOdt()
$this->assertContains('Superman</text:p>', $templateProcessor->getMainPart());
$this->assertContains('Metropolis</text:p>', $templateProcessor->getMainPart());
}


/**
* @expectedException \Exception
Expand Down Expand Up @@ -391,33 +390,33 @@ public function testCloneBlockWithVariableReplacementsOdt()
* @covers ::fixBrokenMacros
* @test
*/
/* public function testFixBrokenMacros()
{
$templateProcessor = new TestableTemplateProcesorOdt();
// TODO
/* public function testFixBrokenMacros()
{
$templateProcessor = new TestableTemplateProcesorOdt();
// TODO
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>normal text</w:t></w:r>');
$this->assertEquals('<w:r><w:t>normal text</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>normal text</w:t></w:r>');
$this->assertEquals('<w:r><w:t>normal text</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>${documentContent}</w:t></w:r>');
$this->assertEquals('<w:r><w:t>${documentContent}</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>${documentContent}</w:t></w:r>');
$this->assertEquals('<w:r><w:t>${documentContent}</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$</w:t><w:t>{documentContent}</w:t></w:r>');
$this->assertEquals('<w:r><w:t>${documentContent}</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$</w:t><w:t>{documentContent}</w:t></w:r>');
$this->assertEquals('<w:r><w:t>${documentContent}</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>');
$this->assertEquals('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>');
$this->assertEquals('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$1500</w:t><w:t>$</w:t><w:t>{documentContent}</w:t></w:r>');
$this->assertEquals('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$1500</w:t><w:t>$</w:t><w:t>{documentContent}</w:t></w:r>');
$this->assertEquals('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>');
$this->assertEquals('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>', $fixed);
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>');
$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);
}
*/
$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);
}
*/

/**
* @covers ::getMainPartName
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpWord/_includes/TestableTemplateProcesorOdt.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct($mainPart = null, $settingsPart = null)
{
$this->tempDocumentMainPart = $mainPart;
$this->tempDocumentSettingsPart = $settingsPart;
$this->tempDocumentHeaders = "";
$this->tempDocumentHeaders = '';
}

public function fixBrokenMacros($documentPart)
Expand Down

0 comments on commit fe36ed0

Please sign in to comment.