Skip to content

Commit

Permalink
Merge pull request #3 from giansalex/f002
Browse files Browse the repository at this point in the history
New Zip routines
  • Loading branch information
giansalex authored Nov 14, 2017
2 parents f028c4c + 5415191 commit d2882ad
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 113 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"type": "project",
"require": {
"php": ">=5.5.9",
"ext-zlib": "*",
"greenter/core": "^1.0"
},
"require-dev": {
Expand Down
18 changes: 10 additions & 8 deletions src/Ws/Services/BaseSunat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion src/Ws/Services/BillSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
class BillSender extends BaseSunat implements SenderInterface
{

/**
* @param string $filename
* @param string $content
Expand Down
81 changes: 0 additions & 81 deletions src/Zip/ZipFactory.php

This file was deleted.

60 changes: 60 additions & 0 deletions src/Zip/ZipReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Created by PhpStorm.
* User: Giansalex
* Date: 16/07/2017
* Time: 12:23
*/

namespace Greenter\Zip;

/**
* Class ZipFactory
* @package Greenter\Zip
*/
final class ZipReader
{
const UNZIP_FORMAT = 'Vsig/vver/vflag/vmeth/vmodt/vmodd/Vcrc/Vcsize/Vsize/vnamelen/vexlen';

/**
* Retorna el contenido del primer xml dentro del zip.
*
* @param string $zipContent
* @return string
*/
public function decompressXmlFile($zipContent)
{
$start = 0;
$max = 10;
while ($max > 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);
}
}
17 changes: 14 additions & 3 deletions src/Zip/ZipFile.php → src/Zip/ZipWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Class ZipFile.
*/
class ZipFile
class ZipWriter
{
/**
* Array to store compressed data.
Expand Down Expand Up @@ -65,8 +65,6 @@ public function unix2DosTime($unixtime = 0)
| ($timearray['seconds'] >> 1);
}

// end of the 'unix2DosTime()' method

/**
* Adds "file" to archive.
*
Expand Down Expand Up @@ -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
}
42 changes: 22 additions & 20 deletions tests/Ws/Zip/ZipFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,64 @@

namespace Tests\Greenter\Zip;

use Greenter\Zip\ZipFactory;
use Greenter\Zip\ZipFile;
use Greenter\Zip\ZipReader;
use Greenter\Zip\ZipWriter;

/**
* Class ZipFactoryTest
* @package Tests\Greenter\Zip\ZipFactory
*/
class ZipFactoryTest extends \PHPUnit_Framework_TestCase
{
const DATA_XML = '<root>Empty</root>';

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);
}

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;
}
}

0 comments on commit d2882ad

Please sign in to comment.