diff --git a/src/SchemaValidatableElementInterface.php b/src/SchemaValidatableElementInterface.php new file mode 100644 index 00000000..82a3e317 --- /dev/null +++ b/src/SchemaValidatableElementInterface.php @@ -0,0 +1,23 @@ +schemaValidate($schemaFile); + + if ($result === false) { + $msgs = []; + foreach (libxml_get_errors() as $err) { + $msgs[] = trim($err->message) . ' on line ' . $err->line; + } + + throw new SchemaViolationException(sprintf( + "XML schema validation errors:\n - %s", + implode("\n - ", array_unique($msgs)), + )); + } + + return $document; + } + + + /** + * Get the schema file that can validate this element. + * + * @return string + */ + public static function getSchemaFile(): string + { + if (defined('static::SCHEMA')) { + $schemaFile = static::SCHEMA; + } + + Assert::true(file_exists($schemaFile), IOException::class); + return $schemaFile; + } +}