Skip to content

Commit

Permalink
add types
Browse files Browse the repository at this point in the history
  • Loading branch information
repl6669 committed Apr 5, 2024
1 parent 0d0fd5b commit 2ee5251
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 76 deletions.
38 changes: 12 additions & 26 deletions src/Madnest/Madzipper/Repositories/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface RepositoryInterface
*
* @param bool $new
*/
public function __construct($filePath, $new = false, $archiveImplementation = null);
public function __construct(string $filePath, bool $create = false, mixed $archive = null);

/**
* Check if the archive is open.
Expand All @@ -29,70 +29,56 @@ public function isClosed(): bool;
/**
* Add a file to the opened Archive.
*/
public function addFile($pathToFile, $pathInArchive);
public function addFile(string $pathToFile, string $pathInArchive): void;

/**
* Add a file to the opened Archive using its contents.
*/
public function addFromString($name, $content);
public function addFromString(string $name, string $content): void;

/**
* Add an empty directory.
*/
public function addEmptyDir($dirName);
public function addEmptyDir(string $dirName): void;

/**
* Remove a file permanently from the Archive.
*/
public function removeFile($pathInArchive);
public function removeFile(string $pathInArchive): void;

/**
* Get the content of a file.
*
*
* @return string
*/
public function getFileContent($pathInArchive);
public function getFileContent(string $pathInArchive): string|false;

/**
* Get the stream of a file.
*
*
* @return mixed
*/
public function getFileStream($pathInArchive);
public function getFileStream(string $pathInArchive): mixed;

/**
* Will loop over every item in the archive and will execute the callback on them
* Will provide the filename for every item.
*/
public function each($callback);
public function each(callable $callback): void;

/**
* Checks whether the file is in the archive.
*
*
* @return bool
*/
public function fileExists($fileInArchive);
public function fileExists(string $fileInArchive): bool;

/**
* Sets the password to be used for decompressing.
*
*
* @return bool
*/
public function usePassword($password);
public function usePassword(string $password): bool;

/**
* Returns the status of the archive as a string.
*
* @return string
*/
public function getStatus();
public function getStatus(): string|false;

/**
* Closes the archive and saves it.
*/
public function close();
public function close(): bool;
}
53 changes: 17 additions & 36 deletions src/Madnest/Madzipper/Repositories/ZipRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@

class ZipRepository implements RepositoryInterface
{
private $archive;
private ?ZipArchive $archive = null;

public bool $open = false;

/**
* Construct with a given path.
*
* @param bool $create
* @param ZipArchive $archive
* @return ZipRepository
*
* @throws \Exception
*/
public function __construct($filePath, $create = false, $archive = null)
public function __construct(string $filePath, bool $create = false, mixed $archive = null)
{
// Check if ZipArchive is available
if (! class_exists('ZipArchive')) {
Expand All @@ -35,17 +31,14 @@ public function __construct($filePath, $create = false, $archive = null)
/**
* Open the archive.
*
* @param mixed $filePath
* @param bool $create
*
* @throws Exception
*/
protected function open($filePath, $create = false): void
protected function open(string $filePath, bool $create = false): void
{
$res = $this->archive->open($filePath, ($create ? ZipArchive::CREATE : null));
$res = $this->archive->open($filePath, ($create ? ZipArchive::CREATE : 0));

if ($res !== true) {
throw new Exception("Error: Failed to open $filePath! Error: ".$this->getErrorMessage($res));
throw new Exception("Error: Failed to open {$filePath}! Error: ".$this->getErrorMessage($res));
}

$this->open = true;
Expand All @@ -70,53 +63,49 @@ public function isClosed(): bool
/**
* Add a file to the opened Archive.
*/
public function addFile($pathToFile, $pathInArchive)
public function addFile(string $pathToFile, string $pathInArchive): void
{
$this->archive->addFile($pathToFile, $pathInArchive);
}

/**
* Add an empty directory.
*/
public function addEmptyDir($dirName)
public function addEmptyDir(string $dirName): void
{
$this->archive->addEmptyDir($dirName);
}

/**
* Add a file to the opened Archive using its contents.
*/
public function addFromString($name, $content)
public function addFromString(string $name, string $content): void
{
$this->archive->addFromString($name, $content);
}

/**
* Remove a file permanently from the Archive.
*/
public function removeFile($pathInArchive)
public function removeFile(string $pathInArchive): void
{
$this->archive->deleteName($pathInArchive);
}

/**
* Get the content of a file.
*
*
* @return string
*/
public function getFileContent($pathInArchive)
public function getFileContent(string $pathInArchive): string|false
{
return $this->archive->getFromName($pathInArchive);
}

/**
* Get the stream of a file.
*
*
* @return mixed
*/
public function getFileStream($pathInArchive)
public function getFileStream(string $pathInArchive): mixed
{
return $this->archive->getStream($pathInArchive);
}
Expand All @@ -125,7 +114,7 @@ public function getFileStream($pathInArchive)
* Will loop over every item in the archive and will execute the callback on them
* Will provide the filename for every item.
*/
public function each($callback)
public function each(callable $callback): void
{
for ($i = 0; $i < $this->archive->numFiles; $i++) {
//skip if folder
Expand All @@ -139,33 +128,25 @@ public function each($callback)

/**
* Checks whether the file is in the archive.
*
*
* @return bool
*/
public function fileExists($fileInArchive)
public function fileExists(string $fileInArchive): bool
{
return $this->archive->locateName($fileInArchive) !== false;
}

/**
* Sets the password to be used for decompressing
* function named usePassword for clarity.
*
*
* @return bool
*/
public function usePassword($password)
public function usePassword(string $password): bool
{
return $this->archive->setPassword($password);
}

/**
* Returns the status of the archive as a string.
*
* @return string
*/
public function getStatus()
public function getStatus(): string|false
{
return $this->archive->getStatusString();
}
Expand All @@ -185,7 +166,7 @@ public function close(): bool
*
* @param mixed $resultCode
*/
private function getErrorMessage($resultCode): string
private function getErrorMessage(int $resultCode): string
{
switch ($resultCode) {
case ZipArchive::ER_EXISTS:
Expand All @@ -205,7 +186,7 @@ private function getErrorMessage($resultCode): string
case ZipArchive::ER_SEEK:
return 'ZipArchive::ER_SEEK - Seek error.';
default:
return "An unknown error [$resultCode] has occurred.";
return "An unknown error [{$resultCode}] has occurred.";
}
}
}
26 changes: 12 additions & 14 deletions tests/ArrayArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ArrayArchive implements RepositoryInterface
*
* @param bool $new
*/
public function __construct($filePath, $new = false, $archiveImplementation = null)
public function __construct(string $filePath, bool $create = false, mixed $archive = null)
{
}

Expand All @@ -36,42 +36,39 @@ public function isClosed(): bool
/**
* Add a file to the opened Archive.
*/
public function addFile($pathToFile, $pathInArchive): void
public function addFile(string $pathToFile, string $pathInArchive): void
{
$this->entries[$pathInArchive] = $pathInArchive;
}

/**
* Add a file to the opened Archive using its contents.
*/
public function addFromString($name, $content): void
public function addFromString(string $name, string $content): void
{
$this->entries[$name] = $name;
}

/**
* Remove a file permanently from the Archive.
*/
public function removeFile($pathInArchive): void
public function removeFile(string $pathInArchive): void
{
unset($this->entries[$pathInArchive]);
}

/**
* Get the content of a file.
*/
public function getFileContent($pathInArchive): string
public function getFileContent(string $pathInArchive): string|false
{
return $this->entries[$pathInArchive];
}

/**
* Get the stream of a file.
*
*
* @return mixed
*/
public function getFileStream($pathInArchive)
public function getFileStream(string $pathInArchive): mixed
{
return $this->entries[$pathInArchive];
}
Expand All @@ -80,7 +77,7 @@ public function getFileStream($pathInArchive)
* Will loop over every item in the archive and will execute the callback on them
* Will provide the filename for every item.
*/
public function each($callback): void
public function each(callable $callback): void
{
foreach ($this->entries as $entry) {
call_user_func_array($callback, [
Expand All @@ -92,7 +89,7 @@ public function each($callback): void
/**
* Checks whether the file is in the archive.
*/
public function fileExists($fileInArchive): bool
public function fileExists(string $fileInArchive): bool
{
return array_key_exists($fileInArchive, $this->entries);
}
Expand All @@ -108,22 +105,23 @@ public function getStatus(): string
/**
* Closes the archive and saves it.
*/
public function close(): void
public function close(): bool
{
return true;
}

/**
* Add an empty directory.
*/
public function addEmptyDir($dirName): void
public function addEmptyDir(string $dirName): void
{
// CODE...
}

/**
* Sets the password to be used for decompressing.
*/
public function usePassword($password): void
public function usePassword(string $password): bool
{
// CODE...
}
Expand Down

0 comments on commit 2ee5251

Please sign in to comment.