<?php

namespace App\Framework\View;

use Dom\HTMLDocument;
use DOMDocument;

final class DomTemplateParser
{
    public function parse(string $html): HTMLDocument
    {
        //$html = preg_replace('#<(img|br|hr|input|meta|link|area|base|col|embed|source|track|wbr)([^>]*)>#i', '<$1$2 />', $html);


        libxml_use_internal_errors(true);

        $dom = HTMLDocument::createFromString($html);

        libxml_clear_errors();

        return $dom;
    }

    /**
     * Erstellt einen DomWrapper aus HTML-String
     */
    public function parseToWrapper(string $html): DomWrapper
    {
        return DomWrapper::fromString($html);
    }

    /**
     * Erstellt einen DomWrapper aus Datei
     */
    public function parseFileToWrapper(string $path): DomWrapper
    {
        return DomWrapper::fromFile($path);
    }

    public function toHtml(HTMLDocument $dom): string
    {
        return $dom->saveHTML();
    }

    public function parseFile(string $path): HTMLDocument
    {
        return HTMLDocument::createFromFile($path);
    }
}
