render(
* componentClass: Button::class,
* content: 'Click Me',
* attributes: ['variant' => 'primary']
* );
* // Returns:
* ```
*/
final readonly class StaticComponentRenderer
{
public function __construct(
private HtmlRenderer $htmlRenderer = new HtmlRenderer()
) {
}
/**
* Render a StaticComponent to HTML
*
* @param class-string $componentClass Fully qualified component class name
* @param string $content Inner HTML content from template
* @param array $attributes Key-value attributes from template
* @return string Rendered HTML
* @throws \RuntimeException If class does not implement StaticComponent
*/
public function render(string $componentClass, string $content, array $attributes = []): string
{
// Verify it's a StaticComponent
if (! is_subclass_of($componentClass, StaticComponent::class)) {
throw new \RuntimeException(
"Class {$componentClass} does not implement StaticComponent interface"
);
}
// Instantiate StaticComponent with content + attributes
$component = new $componentClass($content, $attributes);
// Get root node tree from component
$rootNode = $component->getRootNode();
// Render tree to HTML string
return $this->htmlRenderer->render($rootNode);
}
}