-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the DigiDoc package. | ||
* | ||
* (c) Kristen Gilden <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KG\DigiDoc\Native; | ||
|
||
use KG\DigiDoc\EnvelopeInterface; | ||
|
||
class Envelope implements EnvelopeInterface | ||
{ | ||
/** | ||
* @var \ZipArchive | ||
*/ | ||
private $archive; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $path; | ||
|
||
/** | ||
* @param string $path | ||
*/ | ||
public function __construct($path) | ||
{ | ||
$this->path = $path; | ||
|
||
$this->archive = new \ZipArchive(); | ||
if (true !== ($error = $this->archive->open($this->path))) { | ||
// @todo better exception? | ||
throw new \RuntimeException(sprintf('Failed to open archive "%s", ZipArchive code %d', $this->path, $error)); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getFiles() | ||
{ | ||
$nonMetaNames = array(); | ||
|
||
foreach ($this->getAllFileNames() as $fileName) { | ||
// Skip metadata files. | ||
if (0 === strpos($fileName, 'META-INF/') || 'mimetype' === $fileName) { | ||
continue; | ||
} | ||
|
||
$nonMetaNames[] = $fileName; | ||
} | ||
|
||
return new \ArrayIterator($this->convertNamesToFullPaths($nonMetaNames)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getSignatures() | ||
{ | ||
$signatureNames = array(); | ||
|
||
foreach ($this->getAllFileNames() as $fileName) { | ||
if (preg_match('/^META-INF\/signatures\d+\.xml$/', $fileName)) { | ||
$signatureNames[] = $fileName; | ||
} | ||
} | ||
|
||
return new \ArrayIterator($this->convertNamesToFullPaths($signatureNames)); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
$this->archive->close(); | ||
} | ||
|
||
/** | ||
* @param array $names | ||
* | ||
* @return array | ||
*/ | ||
private function convertNamesToFullPaths($names) | ||
{ | ||
$paths = array(); | ||
|
||
foreach ($names as $name) { | ||
$paths[] = sprintf('zip://%s#%s', $this->path, $name); | ||
} | ||
|
||
return $paths; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private function getAllFileNames() | ||
{ | ||
$fileNames = array(); | ||
|
||
for ($i = 0; $i < $this->archive->numFiles; $i++) { | ||
$fileNames[] = $this->archive->getNameIndex($i); | ||
} | ||
|
||
return $fileNames; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the DigiDoc package. | ||
* | ||
* (c) Kristen Gilden <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KG\DigiDoc\Tests\Envelope; | ||
|
||
use KG\DigiDoc\Native\Envelope; | ||
|
||
class EnvelopeTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @expectedException \RuntimeException | ||
*/ | ||
public function testGetFilesFailsIfArchiveFailsToOpen() | ||
{ | ||
$envelope = new Envelope('not-exists.bdoc'); | ||
} | ||
|
||
public function testGetFilesReturnsIterator() | ||
{ | ||
$envelope = new Envelope(__DIR__ . '/../fixtures/envelope.bdoc'); | ||
$this->assertInstanceOf('\Iterator', $envelope->getFiles()); | ||
} | ||
|
||
public function testGetFilesNotContainsMetaFiles() | ||
{ | ||
$path = __DIR__ . '/../fixtures/envelope.bdoc'; | ||
|
||
$envelope = new Envelope($path); | ||
$expected = array( | ||
'zip://' . $path . '#' . 'hello.txt', | ||
'zip://' . $path . '#' . 'kitten.jpg', | ||
); | ||
|
||
$this->assertEquals($expected, iterator_to_array($envelope->getFiles())); | ||
} | ||
|
||
public function testGetSignaturesReturnsIterator() | ||
{ | ||
$envelope = new Envelope(__DIR__ . '/../fixtures/envelope.bdoc'); | ||
$this->assertInstanceOf('\Iterator', $envelope->getSignatures()); | ||
} | ||
|
||
public function testGetSignaturesContainsOnlySignatureFiles() | ||
{ | ||
$path = __DIR__ . '/../fixtures/envelope.bdoc'; | ||
|
||
$envelope = new Envelope($path); | ||
$expected = array( | ||
'zip://' . $path . '#' . 'META-INF/signatures0.xml', | ||
'zip://' . $path . '#' . 'META-INF/signatures1.xml', | ||
); | ||
|
||
$this->assertEquals($expected, iterator_to_array($envelope->getSignatures())); | ||
} | ||
} |
Binary file not shown.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.