diff --git a/.phpunit.result.cache b/.phpunit.result.cache
index 70149ca..366e0d6 100644
--- a/.phpunit.result.cache
+++ b/.phpunit.result.cache
@@ -1 +1 @@
-{"version":1,"defects":{"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTable":3,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testImages":3},"times":{"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTable":0.0070000000000000001,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testXmlToString":0.0089999999999999993,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testGetDocx":0.002,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testGetXml":0.0089999999999999993,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testPatchXml":0.0030000000000000001,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testRenderXml":0.031,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testRender":0.048000000000000001,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testLineBreak":0.0030000000000000001,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testCyrillic":0.0080000000000000002,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testForLoop":0.0060000000000000001,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testImages":2.1389999999999998}}
\ No newline at end of file
+{"version":1,"defects":{"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTable":3,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testImages":4,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testSections":3},"times":{"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTable":0.008,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testXmlToString":0.005,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testGetDocx":0.002,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testGetXml":0.002,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testPatchXml":0.002,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testRenderXml":0.014,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testRender":0.007,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testLineBreak":0.003,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testCyrillic":0.002,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testForLoop":0.006,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testImages":0.788,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testSections":0.009}}
\ No newline at end of file
diff --git a/composer.json b/composer.json
index 9806557..ea0d928 100644
--- a/composer.json
+++ b/composer.json
@@ -9,7 +9,10 @@
"email": "bingosoft@bk.ru"
}],
"config": {
- "vendor-dir": "./vendor"
+ "vendor-dir": "./vendor",
+ "allow-plugins": {
+ "dealerdirect/phpcodesniffer-composer-installer": true
+ }
},
"require": {
"ext-zip": "*",
diff --git a/src/DocxDocument.php b/src/DocxDocument.php
index dc9111f..7b4106f 100644
--- a/src/DocxDocument.php
+++ b/src/DocxDocument.php
@@ -24,6 +24,8 @@ class DocxDocument
private $tempDocumentRelations = [];
private $tempDocumentContentTypes = '';
private $tempDocumentNewImages = [];
+ private $tempDocumentHeaders = [];
+ private $tempDocumentFooters = [];
/**
* Construct an instance of Document
@@ -58,6 +60,17 @@ private function extract(): void
$this->zipClass->open($this->path);
$this->zipClass->extractTo($this->tmpDir);
+ $index = 1;
+ while (false !== $this->zipClass->locateName($this->getHeaderName($index))) {
+ $this->tempDocumentHeaders[$index] = $this->readPartWithRels($this->getHeaderName($index));
+ $index++;
+ }
+ $index = 1;
+ while (false !== $this->zipClass->locateName($this->getFooterName($index))) {
+ $this->tempDocumentFooters[$index] = $this->readPartWithRels($this->getFooterName($index));
+ $index++;
+ }
+
$this->tempDocumentMainPart = $this->readPartWithRels($this->getMainPartName());
$this->tempDocumentContentTypes = $this->zipClass->getFromName($this->getDocumentContentTypesName());
@@ -75,6 +88,32 @@ public function getDocumentMainPart(): string
return $this->tempDocumentMainPart;
}
+ /**
+ * @return array
+ */
+ public function getHeaders(): array
+ {
+ return $this->tempDocumentHeaders;
+ }
+
+ /**
+ * @return array
+ */
+ public function getFooters(): array
+ {
+ return $this->tempDocumentFooters;
+ }
+
+ public function setHeaders(array $headers): void
+ {
+ $this->tempDocumentHeaders = $headers;
+ }
+
+ public function setFooters(array $footers): void
+ {
+ $this->tempDocumentFooters = $footers;
+ }
+
/**
* Get the name of main part document (method from PhpOffice\PhpWord)
*
@@ -636,6 +675,13 @@ public function save(string $path): void
$this->savePartWithRels($this->getMainPartName());
file_put_contents($this->tmpDir . DIRECTORY_SEPARATOR . $this->getDocumentContentTypesName(), $this->tempDocumentContentTypes);
+ foreach ($this->tempDocumentHeaders as $index => $xml) {
+ file_put_contents($this->tmpDir . DIRECTORY_SEPARATOR . $this->getHeaderName($index), $xml);
+ }
+ foreach ($this->tempDocumentFooters as $index => $xml) {
+ file_put_contents($this->tmpDir . DIRECTORY_SEPARATOR . $this->getFooterName($index), $xml);
+ }
+
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
diff --git a/src/PhpDocxTemplate.php b/src/PhpDocxTemplate.php
index 222f687..6d72a38 100644
--- a/src/PhpDocxTemplate.php
+++ b/src/PhpDocxTemplate.php
@@ -429,7 +429,6 @@ private function renderXml(string $srcXml, array $context): string
);
$template->addExtension($ext);
-
$dstXml = $template->render('index', $context);
$dstXml = str_replace(
@@ -476,6 +475,9 @@ public function render(array $context): void
$xmlSrc = $this->buildXml($context);
$newXml = $this->docx->fixTables($xmlSrc);
$this->updateXml($newXml);
+
+ $this->renderHeaders($context);
+ $this->renderFooters($context);
}
/**
@@ -490,6 +492,20 @@ public function save(string $path): void
//$this->postProcessing($path);
}
+ public function renderHeaders(array $context): void
+ {
+ $this->docx->setHeaders(array_map(function ($header) use ($context) {
+ return $this->renderXml($header, $context);
+ }, $this->docx->getHeaders()));
+ }
+
+ public function renderFooters(array $context): void
+ {
+ $this->docx->setFooters(array_map(function ($footer) use ($context) {
+ return $this->renderXml($footer, $context);
+ }, $this->docx->getFooters()));
+ }
+
/**
* Clean everything after rendering
*/
diff --git a/tests/PhpDocxTemplateTest.php b/tests/PhpDocxTemplateTest.php
index 0ff08e4..066de7e 100644
--- a/tests/PhpDocxTemplateTest.php
+++ b/tests/PhpDocxTemplateTest.php
@@ -18,6 +18,7 @@ class PhpDocxTemplateTest extends TestCase
private const TEMPLATE4 = __DIR__ . "/templates/template4.docx";
private const TEMPLATE5 = __DIR__ . "/templates/template5.docx";
private const TEMPLATE8 = __DIR__ . "/templates/template8.docx";
+ private const TEMPLATE9 = __DIR__ . "/templates/template9.docx";
public function testXmlToString(): void
{
@@ -749,4 +750,91 @@ public function testImages(): void
$this->assertNotEmpty($expectedImage, 'Embed image doesn\'t found.');
}
+
+ public function testSections(): void
+ {
+ $reporter = new PhpDocxTemplate(self::TEMPLATE9);
+ /* $this->assertEquals(
+ $reporter->buildXml(["object" => "world"]),
+ "\n" .
+ "" .
+ "Hello world!" .
+ "" .
+ "" .
+ "" .
+ "\n"
+ );*/
+ $docName = './doc9.docx';
+ $reporter->render(["object" => "test", "section" => [["id" => "test section"]]]);
+ $reporter->save($docName);
+ // $reporter->close();
+
+ $expectedDocumentZip = new ZipArchive();
+ $expectedDocumentZip->open($docName);
+ $header = $expectedDocumentZip->getFromName('word/header2.xml');
+ $this->assertEquals('' . PHP_EOL .
+'Opa test section', $header);
+ }
}
diff --git a/tests/templates/image.docx b/tests/templates/image.docx
index 5954650..16baf07 100644
Binary files a/tests/templates/image.docx and b/tests/templates/image.docx differ
diff --git a/tests/templates/template9.docx b/tests/templates/template9.docx
new file mode 100644
index 0000000..4080425
Binary files /dev/null and b/tests/templates/template9.docx differ