Skip to content

Commit

Permalink
use xml reader in dom cdr
Browse files Browse the repository at this point in the history
  • Loading branch information
giansalex committed Jan 30, 2019
1 parent 880d6b5 commit 3b1bcdc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 68 deletions.
83 changes: 27 additions & 56 deletions src/Ws/Reader/DomCdrReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,90 +16,61 @@
class DomCdrReader implements CdrReaderInterface
{
/**
* Get Cdr using DomDocument.
*
* @param string $xml
*
* @return CdrResponse
*
* @throws \Exception
* @var XmlReader
*/
public function getCdrResponse($xml)
{
$xpt = $this->getXpath($xml);

$cdr = $this->getResponseByXpath($xpt);
if (!$cdr) {
throw new \Exception('Not found cdr response in xml');
}
$cdr->setNotes($this->getNotes($xpt));

return $cdr;
}
private $reader;

/**
* Get Xpath from xml content.
*
* @param string $xmlContent
*
* @return \DOMXPath
* DomCdrReader constructor.
* @param XmlReader $reader
*/
private function getXpath($xmlContent)
public function __construct(XmlReader $reader)
{
$doc = new \DOMDocument();
$doc->loadXML($xmlContent);
$xpt = new \DOMXPath($doc);
$xpt->registerNamespace('x', $doc->documentElement->namespaceURI);

return $xpt;
$this->reader = $reader;
}

/**
* @param \DOMXPath $xpath
* Get Cdr using DomDocument.
*
* @param string $xml
*
* @return CdrResponse
*/
private function getResponseByXpath(\DOMXPath $xpath)
public function getCdrResponse($xml)
{
$resp = $xpath->query('/x:ApplicationResponse/cac:DocumentResponse/cac:Response');

if ($resp->length !== 1) {
return null;
}
$obj = $resp[0];
$this->reader->loadXpath($xml);

$cdr = new CdrResponse();
$cdr->setId($this->getValueByName($obj, 'ReferenceID'))
->setCode($this->getValueByName($obj, 'ResponseCode'))
->setDescription($this->getValueByName($obj, 'Description'));
$cdr = $this->createCdr();

return $cdr;
}

/**
* @param \DOMElement $node
* @param string $name
*
* @return string
* @return CdrResponse
*/
private function getValueByName(\DOMElement $node, $name)
private function createCdr()
{
$values = $node->getElementsByTagName($name);
if ($values->length !== 1) {
return '';
}
$nodePrefix = 'cac:DocumentResponse/cac:Response/';

return $values[0]->nodeValue;
$cdr = new CdrResponse();
$cdr->setId($this->reader->getValue($nodePrefix.'cbc:ReferenceID'))
->setCode($this->reader->getValue($nodePrefix.'cbc:ResponseCode'))
->setDescription($this->reader->getValue($nodePrefix.'cbc:Description'))
->setNotes($this->getNotes());

return $cdr;
}

/**
* @param \DOMXPath $xpath
* Get Notes if exist.
*
* @return string[]
*/
private function getNotes(\DOMXPath $xpath)
private function getNotes()
{
$nodes = $xpath->query('/x:ApplicationResponse/cbc:Note');
$xpath = $this->reader->getXpath();

$nodes = $xpath->query($this->reader->getRoot().'/cbc:Note');
$notes = [];
if ($nodes->length === 0) {
return $notes;
Expand Down
3 changes: 2 additions & 1 deletion src/Ws/Services/BaseSunat.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Greenter\Validator\ErrorCodeProviderInterface;
use Greenter\Ws\Reader\CdrReaderInterface;
use Greenter\Ws\Reader\DomCdrReader;
use Greenter\Ws\Reader\XmlReader;
use Greenter\Zip\CompressInterface;
use Greenter\Zip\DecompressInterface;
use Greenter\Zip\ZipFileDecompress;
Expand Down Expand Up @@ -143,7 +144,7 @@ protected function compress($filename, $xml)
protected function extractResponse($zipContent)
{
if (!$this->cdrReader) {
$this->cdrReader = new DomCdrReader();
$this->cdrReader = new DomCdrReader(new XmlReader());
}

$xml = $this->getXmlResponse($zipContent);
Expand Down
32 changes: 21 additions & 11 deletions tests/Ws/Reader/DomCdrReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Tests\Greenter\Ws\Reader;

use Greenter\Ws\Reader\DomCdrReader;
use Greenter\Ws\Reader\XmlReader;

/**
* Class DomCdrReaderTest
Expand All @@ -23,7 +24,7 @@ public function testGetResponse()
{
$path = __DIR__ . '/../../Resources/R-20600995805-01-F001-1.xml';
$xml = file_get_contents($path);
$reader = new DomCdrReader();
$reader = $this->getCdrReader();
$cdr = $reader->getCdrResponse($xml);

$this->assertNotEmpty($cdr);
Expand All @@ -40,26 +41,30 @@ public function testGetResponseWithNotes()
{
$path = __DIR__ . '/../../Resources/R-20600995805-01-F001-3.xml';
$xml = file_get_contents($path);
$reader = new DomCdrReader();
$reader = $this->getCdrReader();
$cdr = $reader->getCdrResponse($xml);

$this->assertNotEmpty($cdr);
$this->assertEquals(2, count($cdr->getNotes()));
}

/**
* @expectedException \Exception
*/
public function testNotFoundResponse()
{
$xml = <<<XML
<AppRespnse xmlns="urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
<ar:AppRespnse xmlns:ar="urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
<cac:item>Empty</cac:item>
</AppRespnse>
<cbc:value>1</cbc:value>
</ar:AppRespnse>
XML;
$reader = new DomCdrReader();
$reader->getCdrResponse($xml);
$reader = $this->getCdrReader();
$cdr = $reader->getCdrResponse($xml);

$this->assertEmpty($cdr->getId());
$this->assertEmpty($cdr->getCode());
$this->assertEmpty($cdr->getDescription());
$this->assertEmpty($cdr->getNotes());
}

/**
Expand All @@ -76,9 +81,14 @@ public function testEmptyNodes()

$referenceId->parentNode->removeChild($referenceId);
$xml = $doc->saveXML();
$reader = new DomCdrReader();
$reader = $this->getCdrReader();
$cdr = $reader->getCdrResponse($xml);

$this->assertEmpty($cdr->getId());
}

private function getCdrReader()
{
return new DomCdrReader(new XmlReader());
}
}

0 comments on commit 3b1bcdc

Please sign in to comment.