Advertisement
Guest User

XmlXsdValidator

a guest
Oct 1st, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Helpers;
  4.  
  5. use DOMDocument;
  6. use InvalidArgumentException;
  7. use UnexpectedValueException;
  8.  
  9. class XmlXsdValidator
  10. {
  11.     /**
  12.      * XmlXsdValidator constructor.
  13.      */
  14.     public function __construct()
  15.     {
  16.         // Enable user error handling
  17.         libxml_use_internal_errors(true);
  18.     }
  19.  
  20.     /**
  21.      * Method to validate raw xml data against specified xsd schema.
  22.      *
  23.      * @param $xmlString
  24.      * @param $xsdSchemaPath
  25.      * @return bool
  26.      * @throws Exception
  27.      */
  28.     public function validate($xmlString, $xsdSchemaPath)
  29.     {
  30.         // Validate xml string
  31.         if (empty($xmlString)) {
  32.             throw new InvalidArgumentException('Empty xml string.');
  33.         }
  34.  
  35.         // Validate xsd schema path
  36.         if (!file_exists($xsdSchemaPath)) {
  37.             throw new InvalidArgumentException('XSD schema file '. (!empty($xsdSchemaPath) ? $xsdSchemaPath : 'N/A') .' does not exist.');
  38.         }
  39.  
  40.         // Load xml string
  41.         $xml = new DOMDocument();
  42.         $xml->loadXML($xmlString);
  43.  
  44.         // Validate xml string against xsd schema
  45.         if (!$xml->schemaValidate($xsdSchemaPath))
  46.         {
  47.             // Init validation errors
  48.             $validation_errors = [];
  49.  
  50.             // Load validation errors
  51.             $errors = libxml_get_errors();
  52.  
  53.             // Parse validation error messages
  54.             foreach ($errors as $error)
  55.             {
  56.                 // Parse error type
  57.                 $error_type = '';
  58.                 switch ($error->level) {
  59.                     case LIBXML_ERR_WARNING:
  60.                         $error_type = "Warning $error->code: ";
  61.                         break;
  62.                     case LIBXML_ERR_ERROR:
  63.                         $error_type = "Error $error->code: ";
  64.                         break;
  65.                     case LIBXML_ERR_FATAL:
  66.                         $error_type = "Fatal Error $error->code: ";
  67.                         break;
  68.                 }
  69.  
  70.                 // Build validation error
  71.                 $validation_error = $error_type . $error->message;
  72.                 if ($error->file) {
  73.                     $validation_error .= ' on file ' . basename($error->file);
  74.                 }
  75.                 $validation_error .= ' on line #' . $error->line;
  76.  
  77.                 // Append validation error
  78.                 $validation_errors[] = $validation_error;
  79.             }
  80.  
  81.             // Clean-up
  82.             libxml_clear_errors();
  83.  
  84.             // Error
  85.             throw new UnexpectedValueException('XML validation failed: ' . implode(' | ', $validation_errors));
  86.         }
  87.  
  88.         // Clean-up
  89.         $xml = null;
  90.  
  91.         // Success
  92.         return true;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement