Your IP : 216.73.216.81


Current Path : /srv/web/sites/trentinoplant.it/httpdocs/vendor1/phpgt/dom/src/
Upload File :
Current File : /srv/web/sites/trentinoplant.it/httpdocs/vendor1/phpgt/dom/src/DOMParser.php

<?php
namespace Gt\Dom;

use Gt\Dom\Exception\MimeTypeNotSupportedException;

class DOMParser {
	public function __construct() {
	}

	public function parseFromString(
		string $content,
		string $mimeType,
	):HTMLDocument|XMLDocument {
		preg_match(
			"/\w+\/(\w+\+)?(?P<SUBTYPE>\w+)/",
			$mimeType,
			$mimeMatches
		);

		$class = match($mimeMatches["SUBTYPE"]) {
			"html" => HTMLDocument::class,
			"xml" => XMLDocument::class,
			default => null,
		};
		if(!$class) {
			throw new MimeTypeNotSupportedException($mimeType);
		}
		/** @var HTMLDocument|XMLDocument $object */
		$object = new $class($content);
		return $object;
	}
}