(preview) New table/pdf generation system

This commit is contained in:
Benjamin Beganović 2020-07-09 16:05:17 +02:00
parent 6472907251
commit 94976054dd
5 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace Tests\Feature\PdfMaker;
class Business
{
public function html()
{
return file_get_contents(
base_path('tests/Feature/PdfMaker/business.html')
);
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Tests\Feature\PdfMaker;
use DOMDocument;
class PdfMaker
{
use PdfMakerUtilities;
protected $data;
public $design;
public $html;
public $document;
private $xpath;
public function __construct(array $data = [])
{
$this->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();
}
}

View File

@ -0,0 +1,106 @@
<?php
namespace Tests\Feature\PdfMaker;
use Tests\TestCase;
class PdfMakerTest extends TestCase
{
public function testDesignLoadsCorrectly()
{
$maker = new PdfMaker();
$maker->design(Business::class);
$this->assertInstanceOf(Business::class, $maker->design);
}
public function testHtmlDesignLoadsCorrectly()
{
$maker = new PdfMaker();
$maker
->design(Business::class)
->build();
$this->assertStringContainsString('<!-- Business -->', $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'));
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace Tests\Feature\PdfMaker;
use DOMDocument;
use DOMXPath;
trait PdfMakerUtilities
{
private function initializeDomDocument()
{
$document = new DOMDocument();
$document->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;
}
}

View File

@ -0,0 +1,3 @@
<!-- Business -->
<div id="header">This is $title</div>
<table id="product-table"></table>