Loading src/Publisher.php +25 −16 Original line number Diff line number Diff line Loading @@ -161,6 +161,10 @@ class Publisher public function getSections(DOMElement $element): array { $textNs = $this->getDocument()->getNamespaceUrl('text'); Loading Loading @@ -224,15 +228,16 @@ class Publisher 'automatic-styles' )->item(0); $styleNs = $this->getDocument()->getNamespaceUrl('style'); $stylePageBreak = $this->content->createElementNS($styleNs, 'style:style'); $stylePageBreak->setAttributeNS($styleNs, 'style:name', self::PAGE_BREAK_NAME); $stylePageBreak->setAttributeNS($styleNs, 'style:family', 'paragraph'); $stylePageBreak->setAttributeNS($styleNs, 'style:parent-style-name', 'Standard'); $stylePageBreakProperties = $this->content->createElementNS($styleNs, 'style:paragraph-properties'); $stylePageBreakProperties->setAttribute('fo:break-after', 'page'); $stylePageBreak = $this->newElement('style:style', [ 'style:name' => self::PAGE_BREAK_NAME, 'style:family' => 'paragraph', 'style:parent-style-name' => 'Standard', 'style:master-page-name' => 'Standard', ]); $stylePageBreakProperties = $this->newElement('style:paragraph-properties', [ 'style:writing-mode' => 'page', 'style:page-number' => '1', ]); $stylePageBreak->appendChild($stylePageBreakProperties); $styles->appendChild($stylePageBreak); Loading Loading @@ -335,10 +340,12 @@ class Publisher $this->setVariable($vElement, $val); } } if (false !== strpos($name, '@')) { list($sName, $sType) = explode("@", $name); if ($sType == 'text:section') { $name = $sName; } } if (isset($sections[$name])) { foreach ($sections[$name] as $sElement) { if ($val === null Loading Loading @@ -420,8 +427,10 @@ class Publisher $first = true; foreach ($this->values as $values) { $bodyNode = $this->body->cloneNode(true); if (!$first && $this->isAutoBreak()) { if ($first){ $this->getDocument()->getStylist()->setVariables($values); $first = false; }elseif($this->isAutoBreak()){ $this->addPageBreak($bodyNode); } Loading @@ -448,7 +457,7 @@ class Publisher { $document = $this->getDocument(); return $document->find($document->getContent(), $name); return $document->find($this->content, $name); } Loading Loading @@ -477,7 +486,7 @@ class Publisher { $document = $this->getDocument(); return $document->newElement($document->getContent(), $name, $attrs); return $document->newElement($this->content, $name, $attrs); } Loading src/Stylist.php +81 −0 Original line number Diff line number Diff line Loading @@ -79,6 +79,13 @@ class Stylist /** * @param $text * @param array $options * * @return Stylist * @throws Exception */ public function addFiligrane($text, array $options = []): Stylist { $dw = strlen($text) * 2; Loading Loading @@ -204,6 +211,80 @@ class Stylist /** * @param DOMElement $element * * @return Publisher * @throws Exception */ public function getVariables(): array { $textNs = $this->getDocument()->getNamespaceUrl('text'); $vElements = $this->find('text:variable-set'); foreach ($vElements as $vElement) { $name = $vElement->getAttributeNs($textNs, 'name'); if (!isset($variables[$name])) $variables[$name] = []; $variables[$name][] = $vElement; } return $variables; } /** * @param DOMElement $element * @param string $value * * @return Publisher */ public function setVariable(DOMElement $element, string $value): Stylist { $textNs = $this->getDocument()->getNamespaceUrl('text'); $document = $element->ownerDocument; $value = explode("\n", $value); for ($i = 0; $i < count($value); $i++) { if ($i > 0) { $returnVNode = $document->createElementNS($textNs, 'text:line-break'); $element->parentNode->insertBefore($returnVNode, $element); } $vText = $document->createTextNode($value[$i]); $element->parentNode->insertBefore($vText, $element); } $element->parentNode->removeChild($element); $this->getDocument()->setStylesChanged(true); return $this; } /** * @param array $variables * * @return Stylist * @throws Exception */ public function setVariables(array $variables): Stylist { $vars = $this->getVariables(); foreach( $vars as $name => $elements ){ if (isset($variables[$name])){ foreach( $elements as $element ){ $this->setVariable($element, $variables[$name]); } } } return $this; } /** * @param string $name * Loading Loading
src/Publisher.php +25 −16 Original line number Diff line number Diff line Loading @@ -161,6 +161,10 @@ class Publisher public function getSections(DOMElement $element): array { $textNs = $this->getDocument()->getNamespaceUrl('text'); Loading Loading @@ -224,15 +228,16 @@ class Publisher 'automatic-styles' )->item(0); $styleNs = $this->getDocument()->getNamespaceUrl('style'); $stylePageBreak = $this->content->createElementNS($styleNs, 'style:style'); $stylePageBreak->setAttributeNS($styleNs, 'style:name', self::PAGE_BREAK_NAME); $stylePageBreak->setAttributeNS($styleNs, 'style:family', 'paragraph'); $stylePageBreak->setAttributeNS($styleNs, 'style:parent-style-name', 'Standard'); $stylePageBreakProperties = $this->content->createElementNS($styleNs, 'style:paragraph-properties'); $stylePageBreakProperties->setAttribute('fo:break-after', 'page'); $stylePageBreak = $this->newElement('style:style', [ 'style:name' => self::PAGE_BREAK_NAME, 'style:family' => 'paragraph', 'style:parent-style-name' => 'Standard', 'style:master-page-name' => 'Standard', ]); $stylePageBreakProperties = $this->newElement('style:paragraph-properties', [ 'style:writing-mode' => 'page', 'style:page-number' => '1', ]); $stylePageBreak->appendChild($stylePageBreakProperties); $styles->appendChild($stylePageBreak); Loading Loading @@ -335,10 +340,12 @@ class Publisher $this->setVariable($vElement, $val); } } if (false !== strpos($name, '@')) { list($sName, $sType) = explode("@", $name); if ($sType == 'text:section') { $name = $sName; } } if (isset($sections[$name])) { foreach ($sections[$name] as $sElement) { if ($val === null Loading Loading @@ -420,8 +427,10 @@ class Publisher $first = true; foreach ($this->values as $values) { $bodyNode = $this->body->cloneNode(true); if (!$first && $this->isAutoBreak()) { if ($first){ $this->getDocument()->getStylist()->setVariables($values); $first = false; }elseif($this->isAutoBreak()){ $this->addPageBreak($bodyNode); } Loading @@ -448,7 +457,7 @@ class Publisher { $document = $this->getDocument(); return $document->find($document->getContent(), $name); return $document->find($this->content, $name); } Loading Loading @@ -477,7 +486,7 @@ class Publisher { $document = $this->getDocument(); return $document->newElement($document->getContent(), $name, $attrs); return $document->newElement($this->content, $name, $attrs); } Loading
src/Stylist.php +81 −0 Original line number Diff line number Diff line Loading @@ -79,6 +79,13 @@ class Stylist /** * @param $text * @param array $options * * @return Stylist * @throws Exception */ public function addFiligrane($text, array $options = []): Stylist { $dw = strlen($text) * 2; Loading Loading @@ -204,6 +211,80 @@ class Stylist /** * @param DOMElement $element * * @return Publisher * @throws Exception */ public function getVariables(): array { $textNs = $this->getDocument()->getNamespaceUrl('text'); $vElements = $this->find('text:variable-set'); foreach ($vElements as $vElement) { $name = $vElement->getAttributeNs($textNs, 'name'); if (!isset($variables[$name])) $variables[$name] = []; $variables[$name][] = $vElement; } return $variables; } /** * @param DOMElement $element * @param string $value * * @return Publisher */ public function setVariable(DOMElement $element, string $value): Stylist { $textNs = $this->getDocument()->getNamespaceUrl('text'); $document = $element->ownerDocument; $value = explode("\n", $value); for ($i = 0; $i < count($value); $i++) { if ($i > 0) { $returnVNode = $document->createElementNS($textNs, 'text:line-break'); $element->parentNode->insertBefore($returnVNode, $element); } $vText = $document->createTextNode($value[$i]); $element->parentNode->insertBefore($vText, $element); } $element->parentNode->removeChild($element); $this->getDocument()->setStylesChanged(true); return $this; } /** * @param array $variables * * @return Stylist * @throws Exception */ public function setVariables(array $variables): Stylist { $vars = $this->getVariables(); foreach( $vars as $name => $elements ){ if (isset($variables[$name])){ foreach( $elements as $element ){ $this->setVariable($element, $variables[$name]); } } } return $this; } /** * @param string $name * Loading