Skip to content

Commit

Permalink
Simplify codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
sroehrl committed Aug 15, 2022
1 parent a1ff2b2 commit 69390bd
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions Interpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,22 @@ function stepThrough(DOMNodeList $nodes): void
$this->handleAttributes($child);
}
// IS delimiter?
if(Constants::delimiterIsTag() && $child->tagName === substr(Constants::getDelimiter()[0],1,-1) && isset($this->flatData[trim($child->textContent)])){
$child->nodeValue = $this->flatData[trim($child->textContent)];
}
$this->handleDelimiterIsTag($child);

}
if($child instanceof DOMText && trim($child->nodeValue) !== ''){
$this->handleTextNode($child);
}
$this->stepThrough($child->childNodes);
if($child->hasChildNodes()){
$this->stepThrough($child->childNodes);
}
}
}

function handleDelimiterIsTag(DOMElement $node): void
{
if(Constants::delimiterIsTag() && $node->tagName === substr(Constants::getDelimiter()[0],1,-1) && isset($this->flatData[trim($node->textContent)])){
$node->nodeValue = $this->flatData[trim($node->textContent)];
}
}

Expand All @@ -137,11 +145,7 @@ function handleTextNode(DOMText $node): void
// readDelimiter
$givenValue = $this->readDelimiter($node->nodeValue);
if($givenValue !== strip_tags($givenValue)){
$subDoc = new Interpreter($givenValue, $this->contextData);
$fragment = $this->doc->createDocumentFragment();
$fragment->appendXML($subDoc->asHtml());
$node->nodeValue = '';
$node->parentNode->appendChild($fragment);
$this->appendAsFragment($node, $givenValue);
} else {
$node->nodeValue = $this->readDelimiter($node->nodeValue);
// handle functions
Expand All @@ -150,6 +154,15 @@ function handleTextNode(DOMText $node): void

}

private function appendAsFragment(DOMText $parentNode, string $htmlPartial): void
{
$subDoc = new Interpreter($htmlPartial, $this->contextData);
$fragment = $this->doc->createDocumentFragment();
$fragment->appendXML($subDoc->asHtml());
$parentNode->nodeValue = '';
$parentNode->parentNode->appendChild($fragment);
}

/**
* @param DOMText $element
* @return void
Expand All @@ -161,14 +174,18 @@ function handleFunctions(DOMText $element): void
$pattern = "/({$delimiter[0]}.*)*$function\(([^)]*)\)(.*{$delimiter[1]})*/";
$hit = preg_match_all($pattern, $element->nodeValue, $matches, PREG_SET_ORDER);
if($hit){
foreach($matches as $match){
if(!empty($match[2]) && array_key_exists($match[2],$this->flatData)){
$element->nodeValue = str_replace($match[0], $closure($this->flatData[$match[2]]), $element->nodeValue);
} elseif (empty($match[2])){
$element->nodeValue = str_replace($match[0], $closure(), $element->nodeValue);
}
}
$this->executeFunction($closure, $matches, $element);
}
}
}

private function executeFunction(callable $callable, $matches, $element): void
{
foreach($matches as $match){
if(!empty($match[2]) && array_key_exists($match[2],$this->flatData)){
$element->nodeValue = str_replace($match[0], $callable($this->flatData[$match[2]]), $element->nodeValue);
} elseif (empty($match[2])){
$element->nodeValue = str_replace($match[0], $callable(), $element->nodeValue);
}
}
}
Expand Down

0 comments on commit 69390bd

Please sign in to comment.