Refactor logic for replacing variables

This commit is contained in:
Benjamin Beganović 2020-07-13 14:16:18 +02:00
parent 92b2295db1
commit 66d23cd816
3 changed files with 14 additions and 9 deletions

View File

@ -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;
}

View File

@ -95,7 +95,7 @@ class PdfMakerTest extends TestCase
],
],
'variables' => [
['id' => 'header', 'variable' => '$title', 'value' => 'Invoice Ninja'],
'$title' => 'Invoice Ninja',
],
];

View File

@ -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);