Skip to content

Commit

Permalink
cell revisited
Browse files Browse the repository at this point in the history
  • Loading branch information
shadz3rg committed Jul 27, 2019
1 parent a7845ea commit 6ed663e
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/PHPStamp/Document/WordDocument/Extension/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,41 @@
class Cell extends Extension
{
/**
* @inherit
* @param array $arguments
* @return array
* @throws ExtensionException
*/
protected function prepareArguments(array $arguments)
{
if (count($arguments) !== 1) {
throw new ExtensionException('Wrong arguments number, 1 needed, got ' . count($arguments));
if (count($arguments) === 0) {
throw new ExtensionException('At least 1 argument required.');
}

return $arguments;
}

/**
* @inherit
* @param array $arguments
* @param \DOMElement $node
* @throws ExtensionException
* @throws \PHPStamp\Exception\ParsingException
*/
protected function insertTemplateLogic(array $arguments, \DOMElement $node)
{
list($rowName) = $arguments;
$rowName = $arguments[0];

$explicitName = $rowName;
if (count($arguments) === 2) {
$explicitName = $arguments[1];
}

$template = $node->ownerDocument;

// find existing or initiate new table row template
if ($this->isRowTemplateExist($rowName, $template) === false) {
if ($this->isRowTemplateExist($explicitName, $template) === false) {

$rowTemplate = $template->createElementNS(Processor::XSL_NS, 'xsl:template');
$rowTemplate->setAttribute('name', $rowName);
$rowTemplate->setAttribute('name', $explicitName);

// find row node
$rowNode = XMLHelper::parentUntil('w:tr', $node);
Expand All @@ -43,7 +53,7 @@ protected function insertTemplateLogic(array $arguments, \DOMElement $node)
$foreachNode = $template->createElementNS(Processor::XSL_NS, 'xsl:for-each');
$foreachNode->setAttribute('select', '/' . Processor::VALUE_NODE . '/' . $rowName . '/item');
$callTemplateNode = $template->createElementNS(Processor::XSL_NS, 'xsl:call-template');
$callTemplateNode->setAttribute('name', $rowName);
$callTemplateNode->setAttribute('name', $explicitName);
$foreachNode->appendChild($callTemplateNode);

// insert call-template before moving
Expand All @@ -58,6 +68,12 @@ protected function insertTemplateLogic(array $arguments, \DOMElement $node)
Processor::insertTemplateLogic($this->tag->getTextContent(), $relativePath, $node);
}

/**
* @param $rowName
* @param \DOMDocument $template
* @return bool
* @throws ExtensionException
*/
private function isRowTemplateExist($rowName, \DOMDocument $template)
{
$xpath = new \DOMXPath($template);
Expand All @@ -67,6 +83,6 @@ private function isRowTemplateExist($rowName, \DOMDocument $template)
throw new ExtensionException('Unexpected template count.');
}

return ($nodeList->length === 1);
return $nodeList->length === 1;
}
}

0 comments on commit 6ed663e

Please sign in to comment.