<?php

namespace PWH_DCFH\App\Controllers;

use DOMDocument;

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_DOM')) {
    class PWH_DCFH_DOM
    {
        private static $_instance;

        /**
         * Returns the singleton instance of the class.
         * @return self
         */
        public static function instance()
        {
            if (self::$_instance === null) {
                self::$_instance = new self();
            }

            return self::$_instance;
        }

        /**
         * Load DOMDocument
         *
         * @param $html
         *
         * @return DOMDocument
         */
        public static function dom($html)
        {
            // https://stackoverflow.com/questions/4879946/how-to-savehtml-of-domdocument-without-html-wrapper
            $charset = 'utf-8';
            $dom = new DOMDocument('1.0', $charset);
            $dom->loadHTML('<?xml version="1.0" encoding="'.$charset.'"?>'."\n".$html, LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

            return $dom;
        }
    }
}
