1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

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

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