<?php
/**
 * Jingga
 *
 * PHP Version 8.2
 *
 * @package   phpOMS\Utils\Formatter
 * @copyright Dennis Eichhorn
 * @license   OMS License 2.2
 * @version   1.0.0
 * @link      https://jingga.app
 */
declare(strict_types=1);

namespace phpOMS\Utils\Formatter;

/**
 * Html code formatter
 *
 * @package phpOMS\Utils\Formatter
 * @license OMS License 2.2
 * @link    https://jingga.app
 * @since   1.0.0
 */
final class HtmlFormatter
{
    /**
     * Constructor.
     *
     * @since 1.0.0
     * @codeCoverageIgnore
     */
    private function __construct()
    {
    }

    /**
     * Format html code
     *
     * @param string $text Html code
     *
     * @return string
     *
     * @since 1.0.0
     */
    public static function format(string $text) : string
    {
        $dom = new \DOMDocument();

        $dom->loadHTML($text);

        $dom->preserveWhiteSpace = false;
        $dom->formatOutput       = true;

        $formatted = $dom->saveXML($dom->documentElement);

        return $formatted === false ? '' : $formatted;
    }
}
