mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-11 05:32:39 +01:00
105 lines
2.7 KiB
PHP
105 lines
2.7 KiB
PHP
<?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 updateElementProperties(array $elements)
|
|
{
|
|
foreach ($elements as $element) {
|
|
$node = $this->document->getElementById($element['id']);
|
|
|
|
if (isset($element['properties'])) {
|
|
foreach ($element['properties'] as $property => $value) {
|
|
$this->updateElementProperty($element['id'], $property, $value);
|
|
}
|
|
}
|
|
|
|
if (isset($element['elements'])) {
|
|
$this->createElementContent($node, $element['elements']);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
return $element;
|
|
}
|
|
|
|
public function createElementContent($element, $children)
|
|
{
|
|
foreach ($children as $child) {
|
|
$_child = $this->document->createElement($child['element'], $child['content']);
|
|
$element->appendChild($_child);
|
|
|
|
if (isset($child['elements'])) {
|
|
$this->createElementContent($_child, $child['elements']);
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
$original = $element->nodeValue;
|
|
|
|
$element->nodeValue = '';
|
|
|
|
$replaced = strtr($original, [$variable => $value]);
|
|
|
|
$element->appendChild(
|
|
$this->document->createTextNode($replaced)
|
|
);
|
|
|
|
return $element;
|
|
}
|
|
}
|