PHP 8.5.0 Alpha 4 available for testing

Voting

: min(zero, four)?
(Example: nine)

The Note You're Voting On

a dot schaffhirt at sedna-soft dot de
16 years ago
When validating documents with this method there are two issues I don't like about it. First, it creates a bunch of warnings, which one would not expect, as the plot of calling this method is preventing any warnings that could occur when erroneously relying on the document's validity. Second, it only returns a boolean with no chance of getting additional details about the reasons for rendering invalid.

That's the reason for me to use a little wrapper, which I post here in case anyone finds it useful.

Please note that it only works with PHP5 or later.

<?php
class MyDOMDocument {
private
$_delegate;
private
$_validationErrors;

public function
__construct (DOMDocument $pDocument) {
$this->_delegate = $pDocument;
$this->_validationErrors = array();
}

public function
__call ($pMethodName, $pArgs) {
if (
$pMethodName == "validate") {
$eh = set_error_handler(array($this, "onValidateError"));
$rv = $this->_delegate->validate();
if (
$eh) {
set_error_handler($eh);
}
return
$rv;
}
else {
return
call_user_func_array(array($this->_delegate, $pMethodName), $pArgs);
}
}
public function
__get ($pMemberName) {
if (
$pMemberName == "errors") {
return
$this->_validationErrors;
}
else {
return
$this->_delegate->$pMemberName;
}
}
public function
__set ($pMemberName, $pValue) {
$this->_delegate->$pMemberName = $pValue;
}
public function
onValidateError ($pNo, $pString, $pFile = null, $pLine = null, $pContext = null) {
$this->_validationErrors[] = preg_replace("/^.+: */", "", $pString);
}
}
?>

<?php
// $doc is a DOMDocument object
$myDoc = new MyDOMDocument($doc); // copy constructor

// do anything with $myDoc that you would with $doc

$isValid = $myDoc->validate(); // won't create warnings
if (!$isValid) {
print_r($myDoc->errors); // the array all warnings are collected in
}
?>

Maybe you need to change the the part
preg_replace("/^.+: */", "", $pString)
to something different depending on your system's error reporting settings (HTML or plain text), whatsoever

Best Regards,

Anja

<< Back to user notes page

To Top