From 94976054ddda443ca349d67d9f040b88dcc9476a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 9 Jul 2020 16:05:17 +0200 Subject: [PATCH 1/9] (preview) New table/pdf generation system --- tests/Feature/PdfMaker/Business.php | 13 +++ tests/Feature/PdfMaker/PdfMaker.php | 56 ++++++++++ tests/Feature/PdfMaker/PdfMakerTest.php | 106 +++++++++++++++++++ tests/Feature/PdfMaker/PdfMakerUtilities.php | 70 ++++++++++++ tests/Feature/PdfMaker/business.html | 3 + 5 files changed, 248 insertions(+) create mode 100644 tests/Feature/PdfMaker/Business.php create mode 100644 tests/Feature/PdfMaker/PdfMaker.php create mode 100644 tests/Feature/PdfMaker/PdfMakerTest.php create mode 100644 tests/Feature/PdfMaker/PdfMakerUtilities.php create mode 100644 tests/Feature/PdfMaker/business.html diff --git a/tests/Feature/PdfMaker/Business.php b/tests/Feature/PdfMaker/Business.php new file mode 100644 index 000000000000..6429bb9468f1 --- /dev/null +++ b/tests/Feature/PdfMaker/Business.php @@ -0,0 +1,13 @@ +data = $data; + } + + public function design(string $design) + { + $this->design = new $design(); + + $this->initializeDomDocument(); + + return $this; + } + + public function build() + { + $raw = $this->design->html(); + + foreach ($this->data['template'] as $element) { + foreach ($element['properties'] as $property => $value) { + $this->updateElementProperty($element['id'], $property, $value); + } + } + + foreach ($this->data['variables'] as $entry) { + $this->updateVariable($entry['id'], $entry['variable'], $entry['value']); + } + + return $this; + } + + public function getCompiledHTML() + { + return $this->document->saveHTML(); + } +} diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php new file mode 100644 index 000000000000..2ebb6f640d89 --- /dev/null +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -0,0 +1,106 @@ +design(Business::class); + + $this->assertInstanceOf(Business::class, $maker->design); + } + + public function testHtmlDesignLoadsCorrectly() + { + $maker = new PdfMaker(); + + $maker + ->design(Business::class) + ->build(); + + $this->assertStringContainsString('', $maker->html); + } + + public function testGetSectionUtility() + { + $maker = new PdfMaker(); + + $maker + ->design(Business::class) + ->build(); + + $this->assertEquals('table', $maker->getSectionNode('product-table')->nodeName); + } + + public function testTableAttributesAreInjected() + { + $state = [ + 'template' => [ + 'product-table' => [ + 'id' => 'product-table', + 'properties' => [ + 'class' => 'my-awesome-class', + 'style' => 'margin-top: 10px;', + 'script' => 'console.log(1)', + ], + ], + 'header' => [ + 'id' => 'header', + 'properties' => [ + 'class' => 'header-class', + ], + ], + ], + 'variables' => [], + ]; + + $maker = new PdfMaker($state); + + $maker + ->design(Business::class) + ->build(); + + $this->assertStringContainsString('my-awesome-class', $maker->getSection('product-table', 'class')); + $this->assertStringContainsString('margin-top: 10px;', $maker->getSection('product-table', 'style')); + $this->assertStringContainsString('console.log(1)', $maker->getSection('product-table', 'script')); + } + + public function testVariablesAreReplaced() + { + $state = [ + 'template' => [ + 'product-table' => [ + 'id' => 'product-table', + 'properties' => [ + 'class' => 'my-awesome-class', + 'style' => 'margin-top: 10px;', + 'script' => 'console.log(1)', + ], + ], + 'header' => [ + 'id' => 'header', + 'properties' => [ + 'class' => 'header-class', + ], + ], + ], + 'variables' => [ + ['id' => 'header', 'variable' => '$title', 'value' => 'Invoice Ninja'], + ], + ]; + + $maker = new PdfMaker($state); + + $maker + ->design(Business::class) + ->build(); + + $this->assertStringContainsString('Invoice Ninja', $maker->getCompiledHTML()); + $this->assertStringContainsString('Invoice Ninja', $maker->getSection('header')); + } +} diff --git a/tests/Feature/PdfMaker/PdfMakerUtilities.php b/tests/Feature/PdfMaker/PdfMakerUtilities.php new file mode 100644 index 000000000000..4931072f5178 --- /dev/null +++ b/tests/Feature/PdfMaker/PdfMakerUtilities.php @@ -0,0 +1,70 @@ +validateOnParse = true; + @$document->loadHTML($this->design->html()); + + $this->document = $document; + $this->xpath = new DOMXPath($document); + } + + public function getSection(string $selector, string $section = null) + { + $element = $this->document->getElementById($selector); + + if ($section) { + return $element->getAttribute($section); + } + + return $element->nodeValue; + } + + public function getSectionNode(string $selector) + { + return $this->document->getElementById($selector); + } + + public function updateElementProperty(string $element, string $attribute, string $value) + { + $element = $this->document->getElementById($element); + + $element->setAttribute($attribute, $value); + + if ($element->getAttribute($attribute) === $value) { + return $element; + } + + info('Setting element property failed.'); + + return $element; + } + + public function updateVariable(string $element, string $variable, string $value) + { + $element = $this->document->getElementById($element); + + $original = $element->nodeValue; + + info([$variable => $value]); + + $element->nodeValue = ''; + + $replaced = strtr($original, [$variable => $value]); + + $element->appendChild( + $this->document->createTextNode($replaced) + ); + + return $element; + } +} \ No newline at end of file diff --git a/tests/Feature/PdfMaker/business.html b/tests/Feature/PdfMaker/business.html new file mode 100644 index 000000000000..f7dd62b71001 --- /dev/null +++ b/tests/Feature/PdfMaker/business.html @@ -0,0 +1,3 @@ + + +
\ No newline at end of file From 92b2295db14d91cb389164336f893798d46f54be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 13 Jul 2020 13:51:54 +0200 Subject: [PATCH 2/9] Adjust state & make tests green --- tests/Feature/PdfMaker/PdfMaker.php | 2 +- tests/Feature/PdfMaker/PdfMakerTest.php | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/Feature/PdfMaker/PdfMaker.php b/tests/Feature/PdfMaker/PdfMaker.php index 40a2f8806ca0..84ec1c92a47a 100644 --- a/tests/Feature/PdfMaker/PdfMaker.php +++ b/tests/Feature/PdfMaker/PdfMaker.php @@ -18,7 +18,7 @@ class PdfMaker private $xpath; - public function __construct(array $data = []) + public function __construct(array $data) { $this->data = $data; } diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index 2ebb6f640d89..b220cce7a88f 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -6,9 +6,14 @@ use Tests\TestCase; class PdfMakerTest extends TestCase { + public $state = [ + 'template' => [], + 'variables' => [], + ]; + public function testDesignLoadsCorrectly() { - $maker = new PdfMaker(); + $maker = new PdfMaker($this->state); $maker->design(Business::class); @@ -17,18 +22,18 @@ class PdfMakerTest extends TestCase public function testHtmlDesignLoadsCorrectly() { - $maker = new PdfMaker(); + $maker = new PdfMaker($this->state); $maker ->design(Business::class) ->build(); - $this->assertStringContainsString('', $maker->html); + $this->assertStringContainsString('', $maker->getCompiledHTML()); } public function testGetSectionUtility() { - $maker = new PdfMaker(); + $maker = new PdfMaker($this->state); $maker ->design(Business::class) From 66d23cd816111cfe12dced988f9ca71d23707076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 13 Jul 2020 14:16:18 +0200 Subject: [PATCH 3/9] Refactor logic for replacing variables --- tests/Feature/PdfMaker/PdfMaker.php | 6 +----- tests/Feature/PdfMaker/PdfMakerTest.php | 2 +- tests/Feature/PdfMaker/PdfMakerUtilities.php | 15 ++++++++++++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/Feature/PdfMaker/PdfMaker.php b/tests/Feature/PdfMaker/PdfMaker.php index 84ec1c92a47a..fd13b78013dc 100644 --- a/tests/Feature/PdfMaker/PdfMaker.php +++ b/tests/Feature/PdfMaker/PdfMaker.php @@ -2,8 +2,6 @@ namespace Tests\Feature\PdfMaker; -use DOMDocument; - class PdfMaker { use PdfMakerUtilities; @@ -42,9 +40,7 @@ class PdfMaker } } - foreach ($this->data['variables'] as $entry) { - $this->updateVariable($entry['id'], $entry['variable'], $entry['value']); - } + $this->updateVariables($this->data['variables']); return $this; } diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index b220cce7a88f..5602fa8c0272 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -95,7 +95,7 @@ class PdfMakerTest extends TestCase ], ], 'variables' => [ - ['id' => 'header', 'variable' => '$title', 'value' => 'Invoice Ninja'], + '$title' => 'Invoice Ninja', ], ]; diff --git a/tests/Feature/PdfMaker/PdfMakerUtilities.php b/tests/Feature/PdfMaker/PdfMakerUtilities.php index 4931072f5178..5274be7e619c 100644 --- a/tests/Feature/PdfMaker/PdfMakerUtilities.php +++ b/tests/Feature/PdfMaker/PdfMakerUtilities.php @@ -13,7 +13,7 @@ trait PdfMakerUtilities $document->validateOnParse = true; @$document->loadHTML($this->design->html()); - + $this->document = $document; $this->xpath = new DOMXPath($document); } @@ -49,6 +49,15 @@ trait PdfMakerUtilities return $element; } + public function updateVariables(array $variables) + { + $html = strtr($this->getCompiledHTML(), $variables); + + $this->document->loadHTML($html); + + $this->document->saveHTML(); + } + public function updateVariable(string $element, string $variable, string $value) { $element = $this->document->getElementById($element); @@ -56,7 +65,7 @@ trait PdfMakerUtilities $original = $element->nodeValue; info([$variable => $value]); - + $element->nodeValue = ''; $replaced = strtr($original, [$variable => $value]); @@ -67,4 +76,4 @@ trait PdfMakerUtilities return $element; } -} \ No newline at end of file +} From 2be39a4756b0cf739798830297ccdf255cdf5753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 13 Jul 2020 17:49:28 +0200 Subject: [PATCH 4/9] Generating template elements recursively --- tests/Feature/PdfMaker/PdfMaker.php | 9 +---- tests/Feature/PdfMaker/PdfMakerTest.php | 39 ++++++++++++++++++++ tests/Feature/PdfMaker/PdfMakerUtilities.php | 33 +++++++++++++++-- 3 files changed, 70 insertions(+), 11 deletions(-) diff --git a/tests/Feature/PdfMaker/PdfMaker.php b/tests/Feature/PdfMaker/PdfMaker.php index fd13b78013dc..487be1ba1da4 100644 --- a/tests/Feature/PdfMaker/PdfMaker.php +++ b/tests/Feature/PdfMaker/PdfMaker.php @@ -32,14 +32,9 @@ class PdfMaker public function build() { - $raw = $this->design->html(); - - foreach ($this->data['template'] as $element) { - foreach ($element['properties'] as $property => $value) { - $this->updateElementProperty($element['id'], $property, $value); - } - } + // $raw = $this->design->html(); + $this->updateElementProperties($this->data['template']); $this->updateVariables($this->data['variables']); return $this; diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index 5602fa8c0272..97cf08795edf 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -108,4 +108,43 @@ class PdfMakerTest extends TestCase $this->assertStringContainsString('Invoice Ninja', $maker->getCompiledHTML()); $this->assertStringContainsString('Invoice Ninja', $maker->getSection('header')); } + + public function testElementContentIsGenerated() + { + $state = [ + 'template' => [ + 'product-table' => [ + 'id' => 'product-table', + 'properties' => [], + 'elements' => [ + ['element' => 'thead', 'content' => '', 'elements' => [ + ['element' => 'th', 'content' => 'Company',], + ['element' => 'th', 'content' => 'Contact'], + ['element' => 'th', 'content' => 'Country'], + ]], + ['element' => 'tr', 'content' => '', 'elements' => [ + ['element' => 'td', 'content' => '$company'], + ['element' => 'td', 'content' => '$email'], + ['element' => 'td', 'content' => '$country'], + ]], + ], + ], + ], + 'variables' => [ + '$company' => 'Invoice Ninja', + '$email' => 'contact@invoiceninja.com', + '$country' => 'UK', + ], + ]; + + $maker = new PdfMaker($state); + + $maker + ->design(Business::class) + ->build(); + + info($maker->getCompiledHTML()); + + $this->assertTrue(true); + } } diff --git a/tests/Feature/PdfMaker/PdfMakerUtilities.php b/tests/Feature/PdfMaker/PdfMakerUtilities.php index 5274be7e619c..d810185bf0b0 100644 --- a/tests/Feature/PdfMaker/PdfMakerUtilities.php +++ b/tests/Feature/PdfMaker/PdfMakerUtilities.php @@ -34,6 +34,23 @@ trait PdfMakerUtilities return $this->document->getElementById($selector); } + public function updateElementProperties(array $elements) + { + foreach ($elements as $element) { + $node = $this->document->getElementById($element['id']); + + if (isset($element['properties'])) { + foreach ($element['properties'] as $property => $value) { + $this->updateElementProperty($element['id'], $property, $value); + } + } + + if (isset($element['elements'])) { + $this->createElementContent($node, $element['elements']); + } + } + } + public function updateElementProperty(string $element, string $attribute, string $value) { $element = $this->document->getElementById($element); @@ -44,11 +61,21 @@ trait PdfMakerUtilities return $element; } - info('Setting element property failed.'); - return $element; } + public function createElementContent($element, $children) + { + foreach ($children as $child) { + $_child = $this->document->createElement($child['element'], $child['content']); + $element->appendChild($_child); + + if (isset($child['elements'])) { + $this->createElementContent($_child, $child['elements']); + } + } + } + public function updateVariables(array $variables) { $html = strtr($this->getCompiledHTML(), $variables); @@ -64,8 +91,6 @@ trait PdfMakerUtilities $original = $element->nodeValue; - info([$variable => $value]); - $element->nodeValue = ''; $replaced = strtr($original, [$variable => $value]); From 5307569bba25e1abb428c529c2e188a4f1344672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 14 Jul 2020 13:50:00 +0200 Subject: [PATCH 5/9] wrap up engine for setting properties on elements --- tests/Feature/PdfMaker/PdfMaker.php | 11 +++++++---- tests/Feature/PdfMaker/PdfMakerTest.php | 16 +++++++++++----- tests/Feature/PdfMaker/PdfMakerUtilities.php | 12 ++++++++---- tests/Feature/PdfMaker/business.html | 1 + 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/tests/Feature/PdfMaker/PdfMaker.php b/tests/Feature/PdfMaker/PdfMaker.php index 487be1ba1da4..cba480b4f179 100644 --- a/tests/Feature/PdfMaker/PdfMaker.php +++ b/tests/Feature/PdfMaker/PdfMaker.php @@ -32,11 +32,14 @@ class PdfMaker public function build() { - // $raw = $this->design->html(); - - $this->updateElementProperties($this->data['template']); - $this->updateVariables($this->data['variables']); + if (isset($this->data['template'])) { + $this->updateElementProperties($this->data['template']); + } + if (isset($this->data['variables'])) { + $this->updateVariables($this->data['variables']); + } + return $this; } diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index 97cf08795edf..6d4d53b92af6 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -120,12 +120,18 @@ class PdfMakerTest extends TestCase ['element' => 'thead', 'content' => '', 'elements' => [ ['element' => 'th', 'content' => 'Company',], ['element' => 'th', 'content' => 'Contact'], - ['element' => 'th', 'content' => 'Country'], + ['element' => 'th', 'content' => 'Country', 'properties' => [ + 'colspan' => 3, + ]], ]], ['element' => 'tr', 'content' => '', 'elements' => [ ['element' => 'td', 'content' => '$company'], ['element' => 'td', 'content' => '$email'], - ['element' => 'td', 'content' => '$country'], + ['element' => 'td', 'content' => '$country', 'elements' => [ + ['element' => 'a', 'content' => 'Click here for a link', 'properties' => [ + 'href' => 'https://github.com/invoiceninja/invoiceninja', + ]], + ]], ]], ], ], @@ -133,7 +139,7 @@ class PdfMakerTest extends TestCase 'variables' => [ '$company' => 'Invoice Ninja', '$email' => 'contact@invoiceninja.com', - '$country' => 'UK', + '$country' => 'UK', ], ]; @@ -143,8 +149,8 @@ class PdfMakerTest extends TestCase ->design(Business::class) ->build(); - info($maker->getCompiledHTML()); + $compiled = 'contact@invoiceninja.com'; - $this->assertTrue(true); + $this->assertStringContainsString($compiled, $maker->getCompiledHTML()); } } diff --git a/tests/Feature/PdfMaker/PdfMakerUtilities.php b/tests/Feature/PdfMaker/PdfMakerUtilities.php index d810185bf0b0..22ce8760ae5a 100644 --- a/tests/Feature/PdfMaker/PdfMakerUtilities.php +++ b/tests/Feature/PdfMaker/PdfMakerUtilities.php @@ -41,7 +41,7 @@ trait PdfMakerUtilities if (isset($element['properties'])) { foreach ($element['properties'] as $property => $value) { - $this->updateElementProperty($element['id'], $property, $value); + $this->updateElementProperty($node, $property, $value); } } @@ -51,10 +51,8 @@ trait PdfMakerUtilities } } - public function updateElementProperty(string $element, string $attribute, string $value) + public function updateElementProperty($element, string $attribute, string $value) { - $element = $this->document->getElementById($element); - $element->setAttribute($attribute, $value); if ($element->getAttribute($attribute) === $value) { @@ -69,6 +67,12 @@ trait PdfMakerUtilities foreach ($children as $child) { $_child = $this->document->createElement($child['element'], $child['content']); $element->appendChild($_child); + + if (isset($child['properties'])) { + foreach ($child['properties'] as $property => $value) { + $this->updateElementProperty($_child, $property, $value); + } + } if (isset($child['elements'])) { $this->createElementContent($_child, $child['elements']); diff --git a/tests/Feature/PdfMaker/business.html b/tests/Feature/PdfMaker/business.html index f7dd62b71001..0ecd32a30766 100644 --- a/tests/Feature/PdfMaker/business.html +++ b/tests/Feature/PdfMaker/business.html @@ -1,3 +1,4 @@ +
\ No newline at end of file From ceecddb8a2e0939dc51da659c0a097767279a981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 14 Jul 2020 14:02:02 +0200 Subject: [PATCH 6/9] Tests for hidding elements --- tests/Feature/PdfMaker/PdfMakerTest.php | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index 6d4d53b92af6..04bcff181349 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -153,4 +153,43 @@ class PdfMakerTest extends TestCase $this->assertStringContainsString($compiled, $maker->getCompiledHTML()); } + + public function testConditionalRenderingOfElements() + { + $maker1 = new PdfMaker([ + 'template' => [ + 'header' => [ + 'id' => 'header', + 'properties' => [], + ], + ], + ]); + + $maker1 + ->design(Business::class) + ->build(); + + $output1 = $maker1->getCompiledHTML(); + + $this->assertStringContainsString('', $output1); + + $maker2 = new PdfMaker([ + 'template' => [ + 'header' => [ + 'id' => 'header', + 'properties' => ['hidden' => "true"], + ], + ], + ]); + + $maker2 + ->design(Business::class) + ->build(); + + $output2 = $maker2->getCompiledHTML(); + + $this->assertStringContainsString('', $output2); + + $this->assertNotSame($output1, $output2); + } } From bfbe31b185b58ddc73476935d0d7f536c6db13c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 14 Jul 2020 14:35:27 +0200 Subject: [PATCH 7/9] enable sorting between children elements --- tests/Feature/PdfMaker/PdfMakerTest.php | 57 ++++++++++++++++++++ tests/Feature/PdfMaker/PdfMakerUtilities.php | 27 +++++++++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index 04bcff181349..ec1418e7ebf2 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -192,4 +192,61 @@ class PdfMakerTest extends TestCase $this->assertNotSame($output1, $output2); } + + public function testOrderingElements() + { + $maker = new PdfMaker([ + 'template' => [ + 'header' => [ + 'id' => 'header', + 'properties' => [], + 'elements' => [ + ['element' => 'h1', 'content' => 'h1-element'], + ['element' => 'span', 'content' => 'span-element'], + ] + ], + ], + ]); + + $maker + ->design(Business::class) + ->build(); + + $node = $maker->getSectionNode('header'); + + $before = []; + + foreach ($node->childNodes as $child) { + $before[] = $child->nodeName; + } + + $this->assertEquals('h1', $before[1]); + + $maker = new PdfMaker([ + 'template' => [ + 'header' => [ + 'id' => 'header', + 'properties' => [], + 'elements' => [ + ['element' => 'h1', 'content' => 'h1-element', 'order' => 1], + ['element' => 'span', 'content' => 'span-element', 'order' => 0], + ] + ], + ], + ]); + + $maker + ->design(Business::class) + ->build(); + + $node = $maker->getSectionNode('header'); + + $after = []; + + foreach ($node->childNodes as $child) { + $after[] = $child->nodeName; + } + + $this->assertEquals('span', $after[1]); + } } diff --git a/tests/Feature/PdfMaker/PdfMakerUtilities.php b/tests/Feature/PdfMaker/PdfMakerUtilities.php index 22ce8760ae5a..bc1a03cb7d65 100644 --- a/tests/Feature/PdfMaker/PdfMakerUtilities.php +++ b/tests/Feature/PdfMaker/PdfMakerUtilities.php @@ -46,11 +46,32 @@ trait PdfMakerUtilities } if (isset($element['elements'])) { - $this->createElementContent($node, $element['elements']); + $sorted = $this->processChildrenOrder($element['elements']); + + $this->createElementContent($node, $sorted); } } } + public function processChildrenOrder(array $children) + { + $processed = []; + + foreach($children as $child) { + if (!isset($child['order'])) { + $child['order'] = 0; + } + + $processed[] = $child; + } + + usort($processed, function ($a, $b) { + return $a['order'] <=> $b['order']; + }); + + return $processed; + } + public function updateElementProperty($element, string $attribute, string $value) { $element->setAttribute($attribute, $value); @@ -65,6 +86,8 @@ trait PdfMakerUtilities public function createElementContent($element, $children) { foreach ($children as $child) { + // info($child); + $_child = $this->document->createElement($child['element'], $child['content']); $element->appendChild($_child); @@ -73,7 +96,7 @@ trait PdfMakerUtilities $this->updateElementProperty($_child, $property, $value); } } - + if (isset($child['elements'])) { $this->createElementContent($_child, $child['elements']); } From b774a07e3039b5c1d4fe65baf5df4514af1d79c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 14 Jul 2020 15:05:29 +0200 Subject: [PATCH 8/9] Fix sorting children --- tests/Feature/PdfMaker/PdfMakerUtilities.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Feature/PdfMaker/PdfMakerUtilities.php b/tests/Feature/PdfMaker/PdfMakerUtilities.php index bc1a03cb7d65..6672ffa7b584 100644 --- a/tests/Feature/PdfMaker/PdfMakerUtilities.php +++ b/tests/Feature/PdfMaker/PdfMakerUtilities.php @@ -64,11 +64,11 @@ trait PdfMakerUtilities $processed[] = $child; } - + usort($processed, function ($a, $b) { return $a['order'] <=> $b['order']; }); - + return $processed; } @@ -86,7 +86,6 @@ trait PdfMakerUtilities public function createElementContent($element, $children) { foreach ($children as $child) { - // info($child); $_child = $this->document->createElement($child['element'], $child['content']); $element->appendChild($_child); @@ -98,7 +97,9 @@ trait PdfMakerUtilities } if (isset($child['elements'])) { - $this->createElementContent($_child, $child['elements']); + $sorted = $this->processChildrenOrder($child['elements']); + + $this->createElementContent($_child, $sorted); } } } From 141d82b925c0bcfc40638c527417af6ce8c2b74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 14 Jul 2020 15:13:46 +0200 Subject: [PATCH 9/9] Include example table html --- .gitignore | 2 +- tests/Feature/PdfMaker/PdfMakerTest.php | 63 ++++++++++++++++++++++++- tests/Feature/PdfMaker/business.html | 7 ++- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index c6c7e22a3f38..2a4490336337 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,4 @@ local_version.txt storage/migrations nbproject -.php_cs.cache \ No newline at end of file +.php_cs.cache diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index ec1418e7ebf2..006a37f45ccb 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\PdfMaker; +use Spatie\Browsershot\Browsershot; use Tests\TestCase; class PdfMakerTest extends TestCase @@ -171,7 +172,7 @@ class PdfMakerTest extends TestCase $output1 = $maker1->getCompiledHTML(); - $this->assertStringContainsString('', $output1); + $this->assertStringContainsString('', $output1); $maker2 = new PdfMaker([ 'template' => [ @@ -188,7 +189,7 @@ class PdfMakerTest extends TestCase $output2 = $maker2->getCompiledHTML(); - $this->assertStringContainsString('', $output2); + $this->assertStringContainsString('', $output2); $this->assertNotSame($output1, $output2); } @@ -249,4 +250,62 @@ class PdfMakerTest extends TestCase $this->assertEquals('span', $after[1]); } + + public function testGeneratingPdf() + { + $state = [ + 'template' => [ + 'header' => [ + 'id' => 'header', + 'properties' => ['class' => 'text-white bg-blue-600 p-2'], + ], + 'product-table' => [ + 'id' => 'product-table', + 'properties' => ['class' => 'table-auto'], + 'elements' => [ + ['element' => 'thead', 'content' => '', 'elements' => [ + ['element' => 'tr', 'content' => '', 'elements' => [ + ['element' => 'th', 'content' => 'Title', 'properties' => ['class' => 'px-4 py-2']], + ['element' => 'th', 'content' => 'Author', 'properties' => ['class' => 'px-4 py-2']], + ['element' => 'th', 'content' => 'Views', 'properties' => ['class' => 'px-4 py-2']], + ]] + ]], + ['element' => 'tbody', 'content' => '', 'elements' => [ + ['element' => 'tr', 'content' => '', 'elements' => [ + ['element' => 'td', 'content' => 'An amazing guy', 'properties' => ['class' => 'border px-4 py-2']], + ['element' => 'td', 'content' => 'David Bomba', 'properties' => ['class' => 'border px-4 py-2']], + ['element' => 'td', 'content' => '1M', 'properties' => ['class' => 'border px-4 py-2']], + ]], + ['element' => 'tr', 'content' => '', 'elements' => [ + ['element' => 'td', 'content' => 'Flutter master', 'properties' => ['class' => 'border px-4 py-2']], + ['element' => 'td', 'content' => 'Hillel Coren', 'properties' => ['class' => 'border px-4 py-2']], + ['element' => 'td', 'content' => '1M', 'properties' => ['class' => 'border px-4 py-2']], + ]], + ['element' => 'tr', 'content' => '', 'elements' => [ + ['element' => 'td', 'content' => 'Bosssssssss', 'properties' => ['class' => 'border px-4 py-2']], + ['element' => 'td', 'content' => 'Shalom Stark', 'properties' => ['class' => 'border px-4 py-2']], + ['element' => 'td', 'content' => '1M', 'properties' => ['class' => 'border px-4 py-2']], + ]], + ['element' => 'tr', 'content' => '', 'order' => 4, 'elements' => [ + ['element' => 'td', 'content' => 'Three amazing guys', 'properties' => ['class' => 'border px-4 py-2', 'colspan' => '100%']], + ]], + ]], + ], + ] + ], + 'variables' =>[ + '$title' => 'Invoice Ninja', + ] + ]; + + $maker = new PdfMaker($state); + + $maker + ->design(Business::class) + ->build(); + + info($maker->getCompiledHTML()); + + $this->assertTrue(true); + } } diff --git a/tests/Feature/PdfMaker/business.html b/tests/Feature/PdfMaker/business.html index 0ecd32a30766..b2c0c81a2b06 100644 --- a/tests/Feature/PdfMaker/business.html +++ b/tests/Feature/PdfMaker/business.html @@ -1,4 +1,7 @@ - -
\ No newline at end of file + + + +
+ \ No newline at end of file