chore: lots of changes
This commit is contained in:
42
src/Framework/View/Processors/PlaceholderReplacer.php
Normal file
42
src/Framework/View/Processors/PlaceholderReplacer.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Framework\View\Processors;
|
||||
|
||||
use App\Framework\View\DomProcessor;
|
||||
use App\Framework\View\RenderContext;
|
||||
|
||||
final readonly class PlaceholderReplacer implements DomProcessor
|
||||
{
|
||||
|
||||
public function process(\DOMDocument $dom, RenderContext $context): void
|
||||
{
|
||||
$xpath = new \DOMXPath($dom);
|
||||
foreach ($xpath->query('//text()') as $textNode) {
|
||||
$textNode->nodeValue = preg_replace_callback(
|
||||
'/{{\s*([\w.]+)\s*}}/',
|
||||
fn($m) => $this->resolveValue($context->data, $m[1]),
|
||||
$textNode->nodeValue
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function resolveValue(array $data, string $expr): string
|
||||
{
|
||||
$keys = explode('.', $expr);
|
||||
$value = $data;
|
||||
foreach ($keys as $key) {
|
||||
if (!is_array($value) || !array_key_exists($key, $value)) {
|
||||
return "{{ $expr }}"; // Platzhalter bleibt erhalten
|
||||
}
|
||||
$value = $value[$key];
|
||||
}
|
||||
return is_scalar($value) ? (string)$value : '';
|
||||
}
|
||||
|
||||
public function supports(\DOMElement $element): bool
|
||||
{
|
||||
return $element->tagName === 'text';
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user