Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add in simplistic XML Validator
- Loading branch information
1 parent
06dd7ea
commit dc7ba14
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/** | ||
* Use it to validate xml. | ||
* <code> | ||
* | ||
* $validator = new Xerte_Validate_Xml(); | ||
* if($validator->isValid($string)) { | ||
* // $string is ok. | ||
* } | ||
* | ||
* </code> | ||
* | ||
*/ | ||
class Xerte_Validate_Xml /* implements Zend_Validate_Interface */ { // silent dependency at the moment as we don't have all of ZF1 | ||
|
||
/** | ||
* @return boolean false if it's not valid. | ||
* @param string $string - presumably some XML. | ||
*/ | ||
public static function isValid($string) { | ||
$return = false; | ||
|
||
if (exntension_loaded('libxml') && extension_loaded('simplexml')) { | ||
libxml_clear_errors(); | ||
$old_setting = libxml_use_internal_errors(true); | ||
$xml = simplexml_load_string($string); | ||
$errors = libxml_get_errors(); | ||
foreach ($errors as $error) { | ||
_debug("XML Error on {$error->line} - {$error->level} - {$error->message}"); | ||
_debug($xml); | ||
} | ||
|
||
if($xml instanceof SimpleXMLElement) { | ||
$return = true; | ||
} | ||
|
||
libxml_use_internal_errors($old_setting); | ||
libxml_clear_errors(); | ||
|
||
if (!$errors) { | ||
$return = true; | ||
} | ||
} else { | ||
_debug("Warning: simplexml extension not found"); | ||
} | ||
|
||
return $return; | ||
} | ||
|
||
} |