diff --git a/composer.json b/composer.json index 4ef4c6b..9a6af2f 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "type": "project", "require": { "php": ">=5.5.9", + "ext-zlib": "*", "greenter/core": "^1.0" }, "require-dev": { diff --git a/src/Ws/Services/BaseSunat.php b/src/Ws/Services/BaseSunat.php index ea50f16..3b89dca 100644 --- a/src/Ws/Services/BaseSunat.php +++ b/src/Ws/Services/BaseSunat.php @@ -11,7 +11,8 @@ use Greenter\Model\Response\Error; use Greenter\Ws\Reader\DomCdrReader; use Greenter\Ws\Reader\XmlErrorReader; -use Greenter\Zip\ZipFactory; +use Greenter\Zip\ZipReader; +use Greenter\Zip\ZipWriter; /** * Class BaseSunat @@ -78,7 +79,8 @@ protected function getErrorFromFault(\SoapFault $fault) */ protected function compress($filename, $xml) { - return (new ZipFactory())->compress($filename, $xml); + return (new ZipWriter()) + ->compress($filename, $xml); } /** @@ -87,11 +89,11 @@ protected function compress($filename, $xml) */ protected function extractResponse($zipContent) { - $zip = new ZipFactory(); - $xml = $zip->decompressLastFile($zipContent); - $reader = new DomCdrReader(); + $xml = (new ZipReader()) + ->decompressXmlFile($zipContent); - return $reader->getCdrResponse($xml); + return (new DomCdrReader()) + ->getCdrResponse($xml); } /** @@ -100,8 +102,8 @@ protected function extractResponse($zipContent) */ protected function getMessageError($code) { - $search = new XmlErrorReader(); - $msg = $search->getMessageByCode(intval($code)); + $msg = (new XmlErrorReader()) + ->getMessageByCode(intval($code)); return $msg; } diff --git a/src/Ws/Services/BillSender.php b/src/Ws/Services/BillSender.php index e7aca22..690b236 100644 --- a/src/Ws/Services/BillSender.php +++ b/src/Ws/Services/BillSender.php @@ -17,7 +17,6 @@ */ class BillSender extends BaseSunat implements SenderInterface { - /** * @param string $filename * @param string $content diff --git a/src/Zip/ZipFactory.php b/src/Zip/ZipFactory.php deleted file mode 100644 index 7363a79..0000000 --- a/src/Zip/ZipFactory.php +++ /dev/null @@ -1,81 +0,0 @@ -addFile($content, $filename); - - return $archive->file(); - } - - /** - * Retorna el contenido del archivo especificado dentro del zip. - * - * @param string $zipContent - * @param string $fileToExtract - * @return string - */ - public function decompress($zipContent, $fileToExtract) - { - $temp = tempnam(sys_get_temp_dir(),time() . '.zip'); - file_put_contents($temp, $zipContent); - $zip = new ZipArchive; - $output = ""; - if ($zip->open($temp) === true) { - $output = $zip->getFromName($fileToExtract); - } - $zip->close(); - unlink($temp); - - return $output; - } - - /** - * Retorna el contenido del ultimo archivo dentro del zip. - * - * @param string $zipContent - * @return string - */ - public function decompressLastFile($zipContent) - { - $temp = tempnam(sys_get_temp_dir(),time() . '.zip'); - file_put_contents($temp, $zipContent); - $zip = new ZipArchive; - $output = ""; - if (!$zip->open($temp)) { - return $output; - } - - if ($zip->numFiles > 0) { - $output = $zip->getFromIndex($zip->numFiles - 1); - } - - $zip->close(); - unlink($temp); - - return $output; - } -} \ No newline at end of file diff --git a/src/Zip/ZipReader.php b/src/Zip/ZipReader.php new file mode 100644 index 0000000..974ac10 --- /dev/null +++ b/src/Zip/ZipReader.php @@ -0,0 +1,60 @@ + 0) { + $dat = substr($zipContent, $start, 30); + if (empty($dat)) { + break; + } + + $head = unpack(self::UNZIP_FORMAT, $dat); + $filename = substr(substr($zipContent, $start),30, $head['namelen']); + if (empty($filename)) { + break; + } + $count = 30 + $head['namelen'] + $head['exlen']; + + if (strtolower($this->getFileExtension($filename)) == 'xml') { + return gzinflate(substr($zipContent, $start + $count, $head['csize'])); + } + + $start += $count + $head['csize']; + $max--; + } + + return ''; + } + + function getFileExtension($filename) + { + $lastDotPos = strrpos($filename, '.'); + if (!$lastDotPos) return ''; + + return substr($filename, $lastDotPos + 1); + } +} \ No newline at end of file diff --git a/src/Zip/ZipFile.php b/src/Zip/ZipWriter.php similarity index 93% rename from src/Zip/ZipFile.php rename to src/Zip/ZipWriter.php index 85ca3c4..c7c0afd 100644 --- a/src/Zip/ZipFile.php +++ b/src/Zip/ZipWriter.php @@ -11,7 +11,7 @@ /** * Class ZipFile. */ -class ZipFile +class ZipWriter { /** * Array to store compressed data. @@ -65,8 +65,6 @@ public function unix2DosTime($unixtime = 0) | ($timearray['seconds'] >> 1); } - // end of the 'unix2DosTime()' method - /** * Adds "file" to archive. * @@ -148,5 +146,18 @@ public function file() return $data.$header; } + /** + * Comprime el contenido del archivo con el nombre especifico y retorna el contenido del zip. + * + * @param string $filename + * @param string $content + * @return string + */ + public function compress($filename, $content) + { + $this->addFile($content, $filename); + + return $this->file(); + } // end of the 'file()' method } diff --git a/tests/Ws/Zip/ZipFactoryTest.php b/tests/Ws/Zip/ZipFactoryTest.php index 3e6d536..938a639 100644 --- a/tests/Ws/Zip/ZipFactoryTest.php +++ b/tests/Ws/Zip/ZipFactoryTest.php @@ -8,8 +8,8 @@ namespace Tests\Greenter\Zip; -use Greenter\Zip\ZipFactory; -use Greenter\Zip\ZipFile; +use Greenter\Zip\ZipReader; +use Greenter\Zip\ZipWriter; /** * Class ZipFactoryTest @@ -17,35 +17,27 @@ */ class ZipFactoryTest extends \PHPUnit_Framework_TestCase { + const DATA_XML = 'Empty'; + public function testCompressFile() { $zip = $this->createZip(); - //file_put_contents(sys_get_temp_dir() . '/myzip.zip', $zip); $this->assertNotEmpty($zip); } - public function testDecompressFile() - { - $zipContent = $this->createZip(); - $helper = new ZipFactory(); - $content = $helper->decompress($zipContent, 'myFile.txt'); - - $this->assertEquals('TEST TEXT 1', $content); - } - public function testDecompressLastFile() { $zipContent = $this->createZip(); - $helper = new ZipFactory(); - $content = $helper->decompressLastFile($zipContent); + $helper = new ZipReader(); + $content = $helper->decompressXmlFile($zipContent); - $this->assertEquals('TEST TEXT 1', $content); + $this->assertEquals(self::DATA_XML, $content); } public function testUnixTime() { - $zip = new ZipFile(); + $zip = new ZipWriter(); $result = $zip->unix2DosTime(181233012); $this->assertEquals(2162688, $result); @@ -53,17 +45,27 @@ public function testUnixTime() public function testInvalidZip() { - $zip = new ZipFactory(); - $res = $zip->decompressLastFile(''); + $zip = new ZipReader(); + $res = $zip->decompressXmlFile(''); $this->assertEmpty($res); } - private function createZip() + public function testNotXmlZip() { - $helper = new ZipFactory(); + $helper = new ZipWriter(); $zip = $helper->compress('myFile.txt', 'TEST TEXT 1'); + $res = (new ZipReader())->decompressXmlFile($zip); + + $this->assertEmpty($res); + } + + private function createZip() + { + $helper = new ZipWriter(); + $zip = $helper->compress('myFile.xml', self::DATA_XML); + return $zip; } }