2020-07-09 16:05:17 +02:00
|
|
|
<?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());
|
2020-07-13 14:16:18 +02:00
|
|
|
|
2020-07-09 16:05:17 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:16:18 +02:00
|
|
|
public function updateVariables(array $variables)
|
|
|
|
{
|
|
|
|
$html = strtr($this->getCompiledHTML(), $variables);
|
|
|
|
|
|
|
|
$this->document->loadHTML($html);
|
|
|
|
|
|
|
|
$this->document->saveHTML();
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:05:17 +02:00
|
|
|
public function updateVariable(string $element, string $variable, string $value)
|
|
|
|
{
|
|
|
|
$element = $this->document->getElementById($element);
|
|
|
|
|
|
|
|
$original = $element->nodeValue;
|
|
|
|
|
|
|
|
info([$variable => $value]);
|
2020-07-13 14:16:18 +02:00
|
|
|
|
2020-07-09 16:05:17 +02:00
|
|
|
$element->nodeValue = '';
|
|
|
|
|
|
|
|
$replaced = strtr($original, [$variable => $value]);
|
|
|
|
|
|
|
|
$element->appendChild(
|
|
|
|
$this->document->createTextNode($replaced)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
2020-07-13 14:16:18 +02:00
|
|
|
}
|