note 85792 added to domdocument.validate
| From: | a dot schaffhirt at sedna-soft dot de | Date: | Wed, 17 Sep 2008 21:22:01 +0000 |
| Subject: | note 85792 added to domdocument.validate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144498@lists.php.net to get a copy of this message | ||
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
----
Server IP: 212.124.37.9
Probable Submitter: 77.179.254.71
----
Manual Page -- http://www.php.net/manual/en/domdocument.validate.php
Edit -- https://master.php.net/note/edit/85792
Del: integrated -- https://master.php.net/note/delete/85792/integrated
Del: useless -- https://master.php.net/note/delete/85792/useless
Del: bad code -- https://master.php.net/note/delete/85792/bad+code
Del: spam -- https://master.php.net/note/delete/85792/spam
Del: non-english -- https://master.php.net/note/delete/85792/non-english
Del: in docs -- https://master.php.net/note/delete/85792/in+docs
Del: other reasons-- https://master.php.net/note/delete/85792
Reject -- https://master.php.net/note/reject/85792
Search -- https://master.php.net/manage/user-notes.php