Skip to content

Commit

Permalink
Merge pull request #31 from kiwilan/develop
Browse files Browse the repository at this point in the history
2.0.01
  • Loading branch information
ewilan-riviere committed Aug 28, 2023
2 parents b87ddaf + 409486c commit dfa01fe
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 20 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ jobs:
steps:
- name: Install for Linux
run: |
sudo apt update
sudo apt -y install p7zip-full ghostscript imagemagick
sudo apt-get install -y unrar
sudo apt-get install -y libunrar-dev
sudo apt install -y unrar
sudo apt install -y libunrar-dev
sudo sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
shell: bash

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ jobs:
steps:
- name: Install dependencies
run: |
sudo apt update
sudo apt -y install p7zip-full ghostscript imagemagick
sudo apt-get install -y unrar
sudo apt-get install -y libunrar-dev
sudo apt install -y unrar
sudo apt install -y libunrar-dev
sudo sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
shell: bash

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kiwilan/php-archive",
"version": "2.0.0",
"version": "2.0.01",
"description": "PHP package to handle archives (.zip, .rar, .tar, .7z) or .pdf with hybrid solution (native/p7zip), designed to works with eBooks (.epub, .cbz, .cbr, .cb7, .cbt).",
"keywords": [
"php",
Expand Down Expand Up @@ -57,7 +57,7 @@
"test": "vendor/bin/pest",
"test-filter": "vendor/bin/pest --filter",
"test-parallel": "vendor/bin/pest --parallel",
"test-coverage": "vendor/bin/pest --coverage --min=90",
"test-coverage": "vendor/bin/pest --coverage --min=80",
"test-coverage-parallel": "vendor/bin/pest --parallel --coverage",
"analyse": "vendor/bin/phpstan analyse",
"format": "vendor/bin/pint"
Expand Down
18 changes: 15 additions & 3 deletions src/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,33 @@ public static function make(string $path): ArchiveZipCreate
return $archive;
}

public function path(): string
/**
* Get path to the archive.
*/
public function getPath(): string
{
return $this->path;
}

public function extension(): string
/**
* Get extension of the archive.
*/
public function getExtension(): string
{
return $this->extension;
}

public function type(): ArchiveEnum
/**
* Get type of the archive.
*/
public function getType(): ArchiveEnum
{
return $this->type;
}

/**
* Get mime type of the archive.
*/
public static function getMimeType(string $path): ?string
{
try {
Expand Down
35 changes: 24 additions & 11 deletions src/ArchiveZipCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,40 @@

class ArchiveZipCreate
{
/** @var SplFileInfo[] */
protected array $files = [];

/** @var array<string, string> */
protected array $strings = [];

protected int $count = 0;

/**
* @param SplFileInfo[] $files
* @param array<string, string> $strings
*/
protected function __construct(
protected string $path,
protected string $name,
protected array $files = [],
protected array $strings = [],
protected int $count = 0,
) {
}

public static function make(string $path): self
/**
* Create a new instance of ArchiveZipCreate, allowing extensions are `zip`, `epub`, `cbz`.
*
* @param string $path Path to the archive
* @param bool $skipAllowed Skip allowed extensions check
*
* @throws \Exception
*/
public static function make(string $path, bool $skipAllowed = false): self
{
$self = new self($path, pathinfo($path, PATHINFO_BASENAME));

$extension = pathinfo($path, PATHINFO_EXTENSION);
if ($extension !== 'zip') {
throw new \Exception("File {$path} is not a zip file, only zip files are supported.");

if (! $skipAllowed) {
$allowedExtensions = ['zip', 'epub', 'cbz'];
if (! in_array($extension, $allowedExtensions)) {
$extensions = implode(', ', $allowedExtensions);
throw new \Exception("File {$path} is not a zip file, only {$extensions} files are supported.");
}

}

return $self;
Expand Down
53 changes: 53 additions & 0 deletions src/Readers/BaseArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ protected function __construct(
) {
}

/**
* Create a new instance of Archive.
*/
abstract public static function read(string $path): self;

protected function setup(string $path): static
Expand All @@ -62,75 +65,123 @@ protected function setup(string $path): static
}

/**
* Extract all files from archive.
*
* @return string[]
*/
abstract public function extractAll(string $toPath): array;

/**
* Extract selected files from archive.
*
* @param ArchiveItem[] $files
* @return string[]
*/
abstract public function extract(string $toPath, array $files): array;

/**
* Get path to the archive.
*/
public function getPath(): string
{
return $this->path;
}

/**
* Get extension of the archive.
*/
public function getExtension(): string
{
return $this->extension;
}

/**
* Get filename of the archive.
*/
public function getFilename(): string
{
return $this->filename;
}

/**
* Get basename of the archive.
*/
public function getBasename(): string
{
return $this->basename;
}

/**
* Get `ArchiveEnum` of the archive.
*/
public function getType(): ArchiveEnum
{
return $this->type;
}

/**
* Get first file from archive.
*/
public function getFirst(): ArchiveItem
{
return reset($this->files);
}

/**
* Get last file from archive.
*/
public function getLast(): ArchiveItem
{
return end($this->files);
}

/**
* Get files from archive.
*
* @return ArchiveItem[]
*/
public function getFiles(): array
{
return $this->files;
}

/**
* Get count of files.
*/
public function getCount(): int
{
return $this->count;
}

/**
* Get archive stat.
*/
public function getStat(): ?ArchiveStat
{
return $this->stat;
}

/**
* Get PDF metadata.
*/
public function getPdf(): ?PdfMeta
{
return $this->pdf;
}

/**
* Get content from file.
*/
abstract public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string;

/**
* Get text from file.
*/
abstract public function getText(ArchiveItem $file): ?string;

/**
* Find file by search to get `ArchiveItem`.
*/
public function find(string $search, bool $skipHidden = true): ?ArchiveItem
{
$files = $this->findFiles($search, $skipHidden);
Expand All @@ -143,6 +194,8 @@ public function find(string $search, bool $skipHidden = true): ?ArchiveItem
}

/**
* Filter files by search to get `ArchiveItem[]`.
*
* @return ArchiveItem[]|null
*/
public function filter(string $search, bool $skipHidden = true): ?array
Expand Down

0 comments on commit dfa01fe

Please sign in to comment.