<?php  namespace HtmlValidator;  class NodeWrapper {  public function wrap($parser, $nodes, $charset = null) { switch ($parser) { case Validator::PARSER_XML: case Validator::PARSER_XMLDTD: return $this->wrapInXmlDocument($nodes, $charset); case Validator::PARSER_HTML: case Validator::PARSER_HTML5: return $this->wrapInHtml5Document($nodes, $charset); case Validator::PARSER_HTML4: case Validator::PARSER_HTML4TR: return $this->wrapInHtml4Document($nodes, $charset, $parser); default: throw new Exception\UnknownParserException('Unknown parser "' . $parser . '"'); } }  protected function wrapInXmlDocument($nodes, $charset = null) { $charset = strtoupper($charset ?: Validator::CHARSET_UTF_8); $document = '<?xml version="1.0" encoding="' . $charset . '"?>' . PHP_EOL; $document .= '<root>' . $nodes . '</root>'; return $document; }  protected function wrapInHtml5Document($nodes, $charset = null) { $charset = strtolower($charset ?: Validator::CHARSET_UTF_8); $document = '<!DOCTYPE html>' . PHP_EOL; $document .= '<html><head>' . PHP_EOL; $document .= '<meta charset="' . $charset . '">' . PHP_EOL; $document .= '<title>Validation document</title>' . PHP_EOL; $document .= '</head><body>' . $nodes . '</body></html>'; return $document; }  protected function wrapInHtml4Document($nodes, $charset = null, $parser = null) { if ($parser === Validator::PARSER_HTML4TR) { $doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'; } else { $doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'; } $charset = strtolower($charset ?: Validator::CHARSET_UTF_8); $document = $doctype . PHP_EOL; $document .= '<html><head>' . PHP_EOL; $document .= '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '">' . PHP_EOL; $document .= '<title>Validation document</title>' . PHP_EOL; $document .= '</head><body>' . $nodes . '</body></html>'; return $document; } }