Skip to content

Commit

Permalink
Apply modifications issued from previous mergingto report in Template…
Browse files Browse the repository at this point in the history
…ProcessorCommon.php
  • Loading branch information
Papoteur committed Mar 8, 2021
1 parent 161a353 commit e0fb47a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 35 deletions.
32 changes: 0 additions & 32 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,38 +206,6 @@ public function setChart($search, \PhpOffice\PhpWord\Element\AbstractElement $ch
$this->replaceXmlBlock($search, '<w:p>' . $xmlWriter->getData() . '</w:p>', 'w:p');
}

private function getImageArgs($varNameWithArgs)
{
$varElements = explode(':', $varNameWithArgs);
array_shift($varElements); // first element is name of variable => remove it

$varInlineArgs = array();
// size format documentation: https://msdn.microsoft.com/en-us/library/documentformat.openxml.vml.shape%28v=office.14%29.aspx?f=255&MSPPError=-2147217396
foreach ($varElements as $argIdx => $varArg) {
if (strpos($varArg, '=')) { // arg=value
list($argName, $argValue) = explode('=', $varArg, 2);
$argName = strtolower($argName);
if ($argName == 'size') {
list($varInlineArgs['width'], $varInlineArgs['height']) = explode('x', $argValue, 2);
} else {
$varInlineArgs[strtolower($argName)] = $argValue;
}
} elseif (preg_match('/^([0-9]*[a-z%]{0,2}|auto)x([0-9]*[a-z%]{0,2}|auto)$/i', $varArg)) { // 60x40
list($varInlineArgs['width'], $varInlineArgs['height']) = explode('x', $varArg, 2);
} else { // :60:40:f
switch ($argIdx) {
case 0:
$varInlineArgs['width'] = $varArg;
break;
case 1:
$varInlineArgs['height'] = $varArg;
break;
case 2:
$varInlineArgs['ratio'] = $varArg;
break;
}
}

/**
* Returns count of all variables in template.
*
Expand Down
14 changes: 11 additions & 3 deletions src/PhpWord/TemplateProcessorCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,19 @@ private function fixImageWidthHeightRatio(&$width, &$height, $actualWidth, $actu
}
}

protected function prepareImageAttrs($replaceImage, $varInlineArgs)
private function prepareImageAttrs($replaceImage, $varInlineArgs)
{
// get image path and size
$width = null;
$height = null;
$ratio = null;

// a closure can be passed as replacement value which after resolving, can contain the replacement info for the image
// use case: only when a image if found, the replacement tags can be generated
if (is_callable($replaceImage)) {
$replaceImage = $replaceImage();
}

if (is_array($replaceImage) && isset($replaceImage['path'])) {
$imgPath = $replaceImage['path'];
if (isset($replaceImage['width'])) {
Expand Down Expand Up @@ -514,7 +521,7 @@ protected function indexClonedVariables($count, $xmlBlock)
{
$results = array();
for ($i = 1; $i <= $count; $i++) {
$results[] = preg_replace('/\$\{(.*?)\}/', '\${\\1#' . $i . '}', $xmlBlock);
$results[] = preg_replace('/\$\{([^:]*?)(:.*?)?\}/', '\${\1#' . $i . '\2}', $xmlBlock);
}

return $results;
Expand Down Expand Up @@ -550,7 +557,7 @@ protected function replaceClonedVariables($variableReplacements, $xmlBlock)
* @param string $blockType XML tag type of block
* @return \PhpOffice\PhpWord\TemplateProcessor Fluent interface
*/
protected function replaceXmlBlock($macro, $block, $blockType = 'w:p')
public function replaceXmlBlock($macro, $block, $blockType = 'w:p')
{
$where = $this->findContainingXmlBlockForMacro($macro, $blockType);
if (is_array($where)) {
Expand Down Expand Up @@ -585,6 +592,7 @@ protected function findContainingXmlBlockForMacro($macro, $blockType = 'w:p')
if (0 > $end || strstr($this->getSlice($start, $end), $macro) === false) {
return false;
}

return array('start' => $start, 'end' => $end);
}

Expand Down

0 comments on commit e0fb47a

Please sign in to comment.