Example #1
0
 /**
  * Validate dom document
  *
  * @param \DOMDocument $dom
  * @param string $schema Absolute schema file path or URN
  * @param string $errorFormat
  * @return array of errors
  * @throws \Exception
  */
 public static function validateDomDocument(
     \DOMDocument $dom,
     $schema,
     $errorFormat = self::ERROR_FORMAT_DEFAULT
 ) {
     if (!self::$urnResolver) {
         self::$urnResolver = new UrnResolver();
     }
     $schema = self::$urnResolver->getRealPath($schema);
     libxml_use_internal_errors(true);
     libxml_set_external_entity_loader([self::$urnResolver, 'registerEntityLoader']);
     try {
         $result = $dom->schemaValidate($schema);
         $errors = [];
         if (!$result) {
             $validationErrors = libxml_get_errors();
             if (count($validationErrors)) {
                 foreach ($validationErrors as $error) {
                     $errors[] = self::_renderErrorMessage($error, $errorFormat);
                 }
             } else {
                 $errors[] = 'Unknown validation error';
             }
         }
     } catch (\Exception $exception) {
         libxml_use_internal_errors(false);
         throw $exception;
     }
     libxml_set_external_entity_loader(null);
     libxml_use_internal_errors(false);
     return $errors;
 }
Example #2
0
 /**
  * Validate dom document
  *
  * @param \DOMDocument $dom
  * @param string $schema Absolute schema file path or URN
  * @param string $errorFormat
  * @return array of errors
  * @throws \Exception
  */
 public static function validateDomDocument(\DOMDocument $dom, $schema, $errorFormat = self::ERROR_FORMAT_DEFAULT)
 {
     if (!function_exists('libxml_set_external_entity_loader')) {
         return [];
     }
     if (!self::$urnResolver) {
         self::$urnResolver = new UrnResolver();
     }
     $schema = self::$urnResolver->getRealPath($schema);
     libxml_use_internal_errors(true);
     libxml_set_external_entity_loader([self::$urnResolver, 'registerEntityLoader']);
     $errors = [];
     try {
         $result = $dom->schemaValidate($schema);
         if (!$result) {
             $errors = self::getXmlErrors($errorFormat);
         }
     } catch (\Exception $exception) {
         libxml_use_internal_errors(false);
         throw $exception;
     }
     libxml_set_external_entity_loader(null);
     libxml_use_internal_errors(false);
     return $errors;
 }