for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Eduardokum\CorreiosPhp;
class Validator
{
/**
* @param $xml
* @param $xsd
*
* @return bool
* @throws \Exception
*/
public static function isValid($xml, $xsd)
if (!self::isXML($xml)) {
throw new \Exception('XML invalid');
}
libxml_use_internal_errors(true);
libxml_clear_errors();
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = false;
$dom->loadXML($xml, LIBXML_NOBLANKS | LIBXML_NOEMPTYTAG);
if (! $dom->schemaValidate($xsd)) {
$errors = [];
foreach (libxml_get_errors() as $error) {
$errors[] = $error->message;
throw new \Exception('Errors found: ' . implode(', ', $errors));
return true;
* @param $content
public static function isXML($content)
$content = trim($content);
if (empty($content)) {
return false;
if (stripos($content, '<!DOCTYPE html>') !== false
|| stripos($content, '</html>') !== false
) {
simplexml_load_string($content, \SimpleXMLElement::class, LIBXML_NOCDATA);
$errors = libxml_get_errors();
return empty($errors);