Skip to content

Commit

Permalink
Merge pull request #3 from giansalex/feature/path-resolver
Browse files Browse the repository at this point in the history
XSD Resolver
  • Loading branch information
giansalex committed Oct 25, 2018
2 parents f0f898a + 0cbfd99 commit ba74d9b
Show file tree
Hide file tree
Showing 27 changed files with 549 additions and 237 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ language: php

php:
- 5.6
- 7.0
- 7.1

before_script:
- wget -O phpunit https://phar.phpunit.de/phpunit-5.phar
- chmod +x phpunit
- ./phpunit --version
- composer self-update
- composer require php-coveralls/php-coveralls
- composer install --prefer-source --no-interaction

Expand All @@ -17,5 +16,8 @@ script:
- ./phpunit --configuration phpunit.xml --coverage-clover build/logs/clover.xml

after_success:
- travis_retry php vendor/bin/php-coveralls -v
- |
if [ $(phpenv version-name) = "7.1" ]; then
travis_retry php vendor/bin/php-coveralls -v;
fi
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ composer require greenter/ubl-validator

## Example
```php
use Greenter\Ubl\Resolver\UblVersionResolver;
use Greenter\Ubl\SchemaValidator;
use Greenter\Ubl\UblValidator;

$xmlPath = '20000000001-01-F001-1.xml';
$xml = file_get_contents($xmlPath);

$validator = new SchemaValidator();
$resolver = new UblVersionResolver()
$validator->setVersion($resolver->getVersion($xml));
$validator = new UblValidator();

$result = $validator->validate($xml);
$result = $validator->isValid($xml);

if ($result) {
echo 'Validación exitosa';
echo 'Success!!!';
} else {
echo $validator->getMessage();
echo $validator->getError();
}


Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"homepage": "https://github.com/giansalex/ubl-validator",
"type": "library",
"require": {
"php": ">=5.5.9"
"php": ">=5.5.9",
"ext-dom": "*",
"ext-libxml": "*"
},
"autoload": {
"psr-4": {
Expand Down
23 changes: 23 additions & 0 deletions src/Ubl/Resolver/PathResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Created by PhpStorm.
* User: Soporte
* Date: 25/10/2018
* Time: 10:39
*/

namespace Greenter\Ubl\Resolver;

/**
* Interface PathResolverInterface
*/
interface PathResolverInterface
{
/**
* Get Path XSD.
*
* @param \DOMDocument $document
* @return string|null
*/
function getPath(\DOMDocument $document);
}
71 changes: 71 additions & 0 deletions src/Ubl/Resolver/UblPathResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Created by PhpStorm.
* User: Soporte
* Date: 25/10/2018
* Time: 12:13
*/

namespace Greenter\Ubl\Resolver;

/**
* Class UblPathResolver
* @package Greenter\Ubl\Resolver
*/
class UblPathResolver implements PathResolverInterface
{
const XSD_EXTENSION = '.xsd';

/**
* UBL Version.
*
* @var string
*/
public $version;

/**
* @var VersionResolverInterface
*/
public $versionResolver;

/**
* Base XSD Directory
*
* @var string
*/
public $baseDirectory;

/**
* Get Path XSD.
*
* @param \DOMDocument $document
* @return string|null
*/
function getPath(\DOMDocument $document)
{
$name = $document->documentElement->localName;
if (empty($this->version)) {
$this->loadVersion($document);
}
$path = $this->getFullPath($name);

return $path;
}

private function getFullPath($name)
{
$filename = 'UBL-'.$name.'-'.$this->version.self::XSD_EXTENSION;
$path = join(DIRECTORY_SEPARATOR, [$this->baseDirectory, $this->version, 'maindoc', $filename]);

return $path;
}

private function loadVersion(\DOMDocument $document)
{
if (!$this->versionResolver) {
$this->versionResolver = new UblVersionResolver();
}

$this->version = $this->versionResolver->getVersion($document);
}
}
29 changes: 7 additions & 22 deletions src/Ubl/Resolver/UblVersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,24 @@ class UblVersionResolver implements VersionResolverInterface
private $rootNs;

/**
* @param string|\DOMDocument $value
* UBL Version resolver.
*
* @param \DOMDocument $document
* @return string
*/
public function getVersion($value)
public function getVersion(\DOMDocument $document)
{
$doc = $this->getDocument($value);
if (!isset($doc)) {
if (empty($document->documentElement)) {
return '';
}

$xpath = $this->getXpath($doc);
$this->setNs($doc);
$xpath = $this->getXpath($document);
$this->setNs($document);
$xpath->registerNamespace('cbc', self::CBC_NS);

return $this->getSingleValue($xpath, 'cbc:UBLVersionID');
}

private function getDocument($value)
{
if ($value instanceof \DOMDocument) {
$doc = $value;
} else {
$doc = new \DOMDocument();
@$doc->loadXML($value);
}

if (empty($doc->documentElement)) {
return null;
}

return $doc;
}

private function setNs(\DOMDocument $doc)
{
$docName = $doc->documentElement->nodeName;
Expand Down
6 changes: 4 additions & 2 deletions src/Ubl/Resolver/VersionResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
interface VersionResolverInterface
{
/**
* @param string|\DOMDocument $value
* Resolver version from document.
*
* @param \DOMDocument $document
* @return string
*/
public function getVersion($value);
public function getVersion(\DOMDocument $document);
}
96 changes: 26 additions & 70 deletions src/Ubl/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,101 +14,57 @@
class SchemaValidator implements SchemaValidatorInterface
{
/**
* @var string
* @var XmlError[]
*/
private $error;
private $errors;

/**
* @var string
*/
private $version = '2.0';

/**
* @param string $version UBL Version '2.0' or '2.1'
*/
public function setVersion($version)
{
$this->version = $version;
}

/**
* Get last message error or warning.
* Get errors list.
*
* @return string
* @return XmlError[]
*/
public function getMessage()
public function getErrors()
{
return $this->error;
return $this->errors;
}

/**
* @param \DOMDocument|string $value Xml content or DomDocument
* @param \DOMDocument $document
* @param string $xsdPath XSD full path
*
* @return bool
*/
public function validate($value)
public function validate(\DOMDocument $document, $xsdPath)
{
if ($value instanceof \DOMDocument) {
$doc = $value;
} else {
$doc = new \DOMDocument();
@$doc->loadXML($value);
}

$filename = $this->getFilename($doc->documentElement->nodeName);
if (!file_exists($filename)) {
$this->error = 'Schema file not found';

return false;
}

$state = libxml_use_internal_errors(true);
$result = $doc->schemaValidate($filename);
$this->error = $this->getErrors();
$result = $document->schemaValidate($xsdPath);
$this->errors = $this->extractErrors();
libxml_use_internal_errors($state);

return $result;
}

private function getErrors()
/**
* Get errors list.
*
* @return XmlError[]
*/
public function extractErrors()
{
$message = '';
$errors = libxml_get_errors();
$list = [];
foreach ($errors as $error) {
$message .= $this->getError($error).PHP_EOL;
$item = new XmlError();
$item->level = $error->level;
$item->code = $error->code;
$item->column = $error->column;
$item->message = $error->message;
$item->line = $error->line;
$list[] = $item;
}

libxml_clear_errors();

return $message;
}

public function getError($error)
{
$msg = $error->code.': '.trim($error->message).' en la linea '.$error->line;

return $msg;
}

private function getFilename($rootName)
{
$name = $this->getName($rootName);

$path = __DIR__.'/../xsd/'.$this->version.'/maindoc/'.$name.'.xsd';

return $path;
}

/**
* @param $rootName
* @return string
*/
private function getName($rootName)
{
if ($this->version == '2.0') {
return $rootName == 'DespatchAdvice' ? 'UBL-DespatchAdvice-2.0' : 'UBLPE-' . $rootName . '-1.0';
}

return 'UBL-' . $rootName . '-2.1';
return $list;
}
}
11 changes: 6 additions & 5 deletions src/Ubl/SchemaValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
interface SchemaValidatorInterface
{
/**
* Get last message error or warning.
* Get errors list.
*
* @return string
* @return XmlError[]
*/
public function getMessage();
public function getErrors();

/**
* @param \DOMDocument|string $value Xml content or DomDocument
* @param \DOMDocument $document
* @param string $xsdPath XSD full path
*
* @return bool
*/
public function validate($value);
public function validate(\DOMDocument $document, $xsdPath);
}
Loading

0 comments on commit ba74d9b

Please sign in to comment.