wrap up engine for setting properties on elements

This commit is contained in:
Benjamin Beganović 2020-07-14 13:50:00 +02:00
parent 2be39a4756
commit 5307569bba
4 changed files with 27 additions and 13 deletions

View File

@ -32,10 +32,13 @@ class PdfMaker
public function build() public function build()
{ {
// $raw = $this->design->html(); if (isset($this->data['template'])) {
$this->updateElementProperties($this->data['template']); $this->updateElementProperties($this->data['template']);
}
if (isset($this->data['variables'])) {
$this->updateVariables($this->data['variables']); $this->updateVariables($this->data['variables']);
}
return $this; return $this;
} }

View File

@ -120,12 +120,18 @@ class PdfMakerTest extends TestCase
['element' => 'thead', 'content' => '', 'elements' => [ ['element' => 'thead', 'content' => '', 'elements' => [
['element' => 'th', 'content' => 'Company',], ['element' => 'th', 'content' => 'Company',],
['element' => 'th', 'content' => 'Contact'], ['element' => 'th', 'content' => 'Contact'],
['element' => 'th', 'content' => 'Country'], ['element' => 'th', 'content' => 'Country', 'properties' => [
'colspan' => 3,
]],
]], ]],
['element' => 'tr', 'content' => '', 'elements' => [ ['element' => 'tr', 'content' => '', 'elements' => [
['element' => 'td', 'content' => '$company'], ['element' => 'td', 'content' => '$company'],
['element' => 'td', 'content' => '$email'], ['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',
]],
]],
]], ]],
], ],
], ],
@ -143,8 +149,8 @@ class PdfMakerTest extends TestCase
->design(Business::class) ->design(Business::class)
->build(); ->build();
info($maker->getCompiledHTML()); $compiled = 'contact@invoiceninja.com';
$this->assertTrue(true); $this->assertStringContainsString($compiled, $maker->getCompiledHTML());
} }
} }

View File

@ -41,7 +41,7 @@ trait PdfMakerUtilities
if (isset($element['properties'])) { if (isset($element['properties'])) {
foreach ($element['properties'] as $property => $value) { 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); $element->setAttribute($attribute, $value);
if ($element->getAttribute($attribute) === $value) { if ($element->getAttribute($attribute) === $value) {
@ -70,6 +68,12 @@ trait PdfMakerUtilities
$_child = $this->document->createElement($child['element'], $child['content']); $_child = $this->document->createElement($child['element'], $child['content']);
$element->appendChild($_child); $element->appendChild($_child);
if (isset($child['properties'])) {
foreach ($child['properties'] as $property => $value) {
$this->updateElementProperty($_child, $property, $value);
}
}
if (isset($child['elements'])) { if (isset($child['elements'])) {
$this->createElementContent($_child, $child['elements']); $this->createElementContent($_child, $child['elements']);
} }

View File

@ -1,3 +1,4 @@
<!-- Business --> <!-- Business -->
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<div id="header">This is $title</div> <div id="header">This is $title</div>
<table id="product-table"></table> <table id="product-table"></table>