<?php

namespace Plainwork\Ui\Renderer;

use Plainwork\Http\Request;
use Plainwork\Ui\Component\HtmlTemplate;
use Plainwork\Ui\Ui;

final class HtmlTemplateRenderer
{
    public $ui = Ui::class;

    public function __invoke(HtmlTemplate $el, Request $request)
    {
        $ret = $el->getHtml();

        $vars = $el->getVars();
        $replace = [];
        $i = 1;
        foreach ($vars as $v) {
            $k = '{' . $i . '}';
            $replace[$k] = $this->ui->render($v, $request);
            ++$i;
        }

        $ret = strtr($ret, $replace);

        return $ret;
    }
}
