Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

RichText/Validator.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\RichText;
8
9 use DOMDocument;
10 use DOMElement;
11 use DOMXPath;
12 use XSLTProcessor;
13 use RuntimeException;
14
20 class Validator extends XmlBase
21 {
27 protected $schemas;
28
32 public function __construct(array $schemas)
33 {
34 $this->schemas = $schemas;
35 }
36
48 public function validate(DOMDocument $document)
49 {
50 $this->startRecordingErrors();
51 $additionalErrors = [];
52
53 foreach ($this->schemas as $schema) {
54 $errors = $this->validateBySchema($document, $schema);
55 if (!empty($errors)) {
56 $additionalErrors = array_merge($additionalErrors, $errors);
57 }
58 }
59
60 $errors = $this->collectErrors();
61 if (isset($additionalErrors)) {
62 $errors = array_merge($errors, $additionalErrors);
63 }
64
65 return $errors;
66 }
67
78 protected function validateBySchema(DOMDocument $document, $schema)
79 {
80 if (!file_exists($schema) || !is_file($schema)) {
81 throw new RuntimeException(
82 "Validation of XML document cannot be performed, file '{$schema}' does not exist."
83 );
84 }
85
86 $additionalErrors = [];
87 $pathInfo = pathinfo($schema);
88 switch ($pathInfo['extension']) {
89 case 'xsd':
90 $document->schemaValidate($schema);
91 break;
92 case 'rng':
93 $document->relaxNGValidate($schema);
94 break;
95 case 'xsl':
96 $additionalErrors = $this->schematronValidate($document, $schema);
97 break;
98 default:
99 throw new RuntimeException(
100 'Validator is capable of handling ISO Schematron (as XSLT stylesheet), ' .
101 "XSD and RELAX NG schema files, ending in .xsl, .xsd or .rng.\n" .
102 "File '{$schema}' does not seem to be either of these."
103 );
104 }
105
106 return $additionalErrors;
107 }
108
118 protected function schematronValidate(DOMDocument $document, $filename)
119 {
120 $stylesheet = $this->loadFile($filename);
121 $xsltProcessor = new XSLTProcessor();
122 $xsltProcessor->importStyleSheet($stylesheet);
123
124 $result = $xsltProcessor->transformToDoc($document);
125
126 $xpath = new DOMXPath($result);
127 $xpath->registerNamespace('svrl', 'http://purl.oclc.org/dsdl/svrl');
128 $xpathExpression = '//svrl:failed-assert';
129
130 $failures = [];
131 $failedAsserts = $xpath->query($xpathExpression);
132
133 foreach ($failedAsserts as $failedAssert) {
134 $failures[] = $this->formatSVRLFailure($failedAssert);
135 }
136
137 return $failures;
138 }
139
147 protected function formatSVRLFailure(DOMElement $failedAssert)
148 {
149 $location = $failedAssert->getAttribute('location');
150
151 return (strlen($location) ? $location . ': ' : '') . $failedAssert->textContent;
152 }
153 }
eZ\Publish\Core\FieldType\RichText\Validator
Validates XML document using ISO Schematron (as XSLT stylesheet), XSD and RELAX NG schemas.
Definition: RichText/Validator.php:21
eZ\Publish\Core\FieldType\RichText\Validator\validateBySchema
validateBySchema(DOMDocument $document, $schema)
Performs validation on given $document using given $schema file and returns validation errors.
Definition: RichText/Validator.php:78
eZ\Publish\Core\FieldType\RichText\Validator\$schemas
$schemas
Definition: RichText/Validator.php:27
eZ\Publish\Core\FieldType\RichText\Validator\validate
validate(DOMDocument $document)
Performs validation on given $document using injected schema files and returns validation errors.
Definition: RichText/Validator.php:48
eZ\Publish\Core\FieldType\RichText\Validator\schematronValidate
schematronValidate(DOMDocument $document, $filename)
Validates given $document using XSLT stylesheet converted from ISO Schematron schema and returns an a...
Definition: RichText/Validator.php:118
eZ\Publish\Core\FieldType\RichText\Validator\formatSVRLFailure
formatSVRLFailure(DOMElement $failedAssert)
Returns SVRL assertion failure as a string.
Definition: RichText/Validator.php:147
eZ\Publish\Core\FieldType\RichText\Validator\__construct
__construct(array $schemas)
Definition: RichText/Validator.php:32
eZ\Publish\Core\FieldType\RichText\XmlBase
A base class for XML document handlers.
Definition: XmlBase.php:19
eZ\Publish\Core\FieldType\RichText\XmlBase\startRecordingErrors
startRecordingErrors()
Enables user handling of libxml errors and clears error buffer.
Definition: XmlBase.php:80
eZ\Publish\Core\FieldType\RichText\XmlBase\loadFile
loadFile($path)
Returns DOMDocument object loaded from given XML file $path.
Definition: XmlBase.php:46
eZ\Publish\Core\FieldType\RichText\XmlBase\collectErrors
collectErrors()
Returns formatted errors from libxml error buffer and restores previous setting for libxml error hand...
Definition: XmlBase.php:100
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.