mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 02:04:33 -04:00
Refactor logic for replacing variables
This commit is contained in:
parent
92b2295db1
commit
66d23cd816
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
namespace Tests\Feature\PdfMaker;
|
namespace Tests\Feature\PdfMaker;
|
||||||
|
|
||||||
use DOMDocument;
|
|
||||||
|
|
||||||
class PdfMaker
|
class PdfMaker
|
||||||
{
|
{
|
||||||
use PdfMakerUtilities;
|
use PdfMakerUtilities;
|
||||||
@ -42,9 +40,7 @@ class PdfMaker
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->data['variables'] as $entry) {
|
$this->updateVariables($this->data['variables']);
|
||||||
$this->updateVariable($entry['id'], $entry['variable'], $entry['value']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,7 @@ class PdfMakerTest extends TestCase
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
'variables' => [
|
'variables' => [
|
||||||
['id' => 'header', 'variable' => '$title', 'value' => 'Invoice Ninja'],
|
'$title' => 'Invoice Ninja',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ trait PdfMakerUtilities
|
|||||||
|
|
||||||
$document->validateOnParse = true;
|
$document->validateOnParse = true;
|
||||||
@$document->loadHTML($this->design->html());
|
@$document->loadHTML($this->design->html());
|
||||||
|
|
||||||
$this->document = $document;
|
$this->document = $document;
|
||||||
$this->xpath = new DOMXPath($document);
|
$this->xpath = new DOMXPath($document);
|
||||||
}
|
}
|
||||||
@ -49,6 +49,15 @@ trait PdfMakerUtilities
|
|||||||
return $element;
|
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)
|
public function updateVariable(string $element, string $variable, string $value)
|
||||||
{
|
{
|
||||||
$element = $this->document->getElementById($element);
|
$element = $this->document->getElementById($element);
|
||||||
@ -56,7 +65,7 @@ trait PdfMakerUtilities
|
|||||||
$original = $element->nodeValue;
|
$original = $element->nodeValue;
|
||||||
|
|
||||||
info([$variable => $value]);
|
info([$variable => $value]);
|
||||||
|
|
||||||
$element->nodeValue = '';
|
$element->nodeValue = '';
|
||||||
|
|
||||||
$replaced = strtr($original, [$variable => $value]);
|
$replaced = strtr($original, [$variable => $value]);
|
||||||
@ -67,4 +76,4 @@ trait PdfMakerUtilities
|
|||||||
|
|
||||||
return $element;
|
return $element;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user