Example #1
0
 /**
  * Parses XML as a string and convert the defined custom tags into HTML.
  *
  * @param string $xml
  * The XML string to be parsed. It represents a full document.
  *
  * @param string|null $emptyNamespaceSource
  * Where do we look for custom tag definition files.
  *
  * @return string
  * A string. It's supposed to be HTML.
  *
  * @throws \Error
  * If XML is not well formed.
  *
  * @since 2.0.0 Second parameter becomes optional.
  * @since 0.0.0
  *
  * @see https://github.com/Odepax/pasap/wiki/Namespaces#the-empty-namespace
  * @see http://www.w3schools.com/xml/xml_validator.asp
  */
 public static function parse(string $xml, $emptyNamespaceSource = null) : string
 {
     if ($emptyNamespaceSource !== null) {
         Configure::namespaceSource('', $emptyNamespaceSource);
     }
     $document = new \DOMDocument("1.0", "UTF-8");
     // Get rid of errors about HTML5.
     libxml_use_internal_errors(true);
     if (!$document->loadXML($xml)) {
         throw new \Error("Why you no check your XML before parsing !?");
     }
     libxml_use_internal_errors(false);
     if (Configure::get('doctype') === Configure::ALWAYS_HTML5) {
         $output = '<!DOCTYPE html>';
     } else {
         if (!is_null($document->doctype)) {
             if ($document->doctype->publicId === '' || $document->doctype->systemId === '') {
                 $output = "<!DOCTYPE {$document->doctype->name}>";
             } else {
                 $output = "<!DOCTYPE {$document->doctype->name} PUBLIC \"{$document->doctype->publicId}\" \"{$document->doctype->systemId}\">";
             }
         } else {
             $output = '';
         }
     }
     $output .= new Element($document->documentElement);
     switch (Configure::get('output')) {
         case Configure::MINIFY:
             return static::minify($output);
         case Configure::PRETTIFY:
             return static::prettify($output);
         default:
             return $output;
     }
 }