Skip to content

Commit

Permalink
Type Zip
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinArtus committed Apr 24, 2023
1 parent b05376b commit 20b4484
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions src/Gaufrette/Adapter/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,21 @@
*/
class Zip implements Adapter
{
/**
* @var string The zip archive full path
*/
protected $zipFile;
protected ZipArchive $zipArchive;

/**
* @var ZipArchive
*/
protected $zipArchive;

public function __construct($zipFile)
public function __construct(private readonly string $zipFile)
{
if (!extension_loaded('zip')) {
throw new \RuntimeException(sprintf('Unable to use %s as the ZIP extension is not available.', __CLASS__));
}

$this->zipFile = $zipFile;
$this->reinitZipArchive();
}

/**
* {@inheritdoc}
*/
public function read($key)
public function read(string $key): string|bool
{
if (false === ($content = $this->zipArchive->getFromName($key, 0))) {
return false;
Expand All @@ -49,7 +40,7 @@ public function read($key)
/**
* {@inheritdoc}
*/
public function write($key, $content)
public function write(string $key, mixed $content): int|bool
{
if (!$this->zipArchive->addFromString($key, $content)) {
return false;
Expand All @@ -65,15 +56,15 @@ public function write($key, $content)
/**
* {@inheritdoc}
*/
public function exists($key)
public function exists(string $key): bool
{
return (boolean) $this->getStat($key);
}

/**
* {@inheritdoc}
*/
public function keys()
public function keys(): array
{
$keys = [];

Expand All @@ -89,15 +80,15 @@ public function keys()
*
* {@inheritdoc}
*/
public function isDirectory($key)
public function isDirectory(string $key): bool
{
return false;
}

/**
* {@inheritdoc}
*/
public function mtime($key)
public function mtime(string $key): int|bool
{
$stat = $this->getStat($key);

Expand All @@ -107,7 +98,7 @@ public function mtime($key)
/**
* {@inheritdoc}
*/
public function delete($key)
public function delete(string $key): bool
{
if (!$this->zipArchive->deleteName($key)) {
return false;
Expand All @@ -119,7 +110,7 @@ public function delete($key)
/**
* {@inheritdoc}
*/
public function rename($sourceKey, $targetKey)
public function rename(string $sourceKey, string $targetKey): bool
{
if (!$this->zipArchive->renameName($sourceKey, $targetKey)) {
return false;
Expand All @@ -131,12 +122,8 @@ public function rename($sourceKey, $targetKey)
/**
* Returns the stat of a file in the zip archive
* (name, index, crc, mtime, compression size, compression method, filesize).
*
* @param $key
*
* @return array|bool
*/
public function getStat($key)
public function getStat(string $key): array
{
$stat = $this->zipArchive->statName($key);
if (false === $stat) {
Expand All @@ -157,7 +144,7 @@ public function __destruct()
}
}

protected function reinitZipArchive()
protected function reinitZipArchive(): self
{
$this->zipArchive = new ZipArchive();

Expand Down Expand Up @@ -216,7 +203,7 @@ protected function reinitZipArchive()
*
* @throws \RuntimeException If file could not be saved
*/
protected function save()
protected function save(): bool
{
// Close to save modification
if (!$this->zipArchive->close()) {
Expand Down

0 comments on commit 20b4484

Please sign in to comment.