Skip to content

Commit dfa01fe

Browse files
Merge pull request #31 from kiwilan/develop
2.0.01
2 parents b87ddaf + 409486c commit dfa01fe

File tree

6 files changed

+100
-20
lines changed

6 files changed

+100
-20
lines changed

.github/workflows/codecov.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ jobs:
1515
steps:
1616
- name: Install for Linux
1717
run: |
18+
sudo apt update
1819
sudo apt -y install p7zip-full ghostscript imagemagick
19-
sudo apt-get install -y unrar
20-
sudo apt-get install -y libunrar-dev
20+
sudo apt install -y unrar
21+
sudo apt install -y libunrar-dev
2122
sudo sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
2223
shell: bash
2324

.github/workflows/run-tests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ jobs:
1717
steps:
1818
- name: Install dependencies
1919
run: |
20+
sudo apt update
2021
sudo apt -y install p7zip-full ghostscript imagemagick
21-
sudo apt-get install -y unrar
22-
sudo apt-get install -y libunrar-dev
22+
sudo apt install -y unrar
23+
sudo apt install -y libunrar-dev
2324
sudo sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
2425
shell: bash
2526

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kiwilan/php-archive",
3-
"version": "2.0.0",
3+
"version": "2.0.01",
44
"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).",
55
"keywords": [
66
"php",
@@ -57,7 +57,7 @@
5757
"test": "vendor/bin/pest",
5858
"test-filter": "vendor/bin/pest --filter",
5959
"test-parallel": "vendor/bin/pest --parallel",
60-
"test-coverage": "vendor/bin/pest --coverage --min=90",
60+
"test-coverage": "vendor/bin/pest --coverage --min=80",
6161
"test-coverage-parallel": "vendor/bin/pest --parallel --coverage",
6262
"analyse": "vendor/bin/phpstan analyse",
6363
"format": "vendor/bin/pint"

src/Archive.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,33 @@ public static function make(string $path): ArchiveZipCreate
4949
return $archive;
5050
}
5151

52-
public function path(): string
52+
/**
53+
* Get path to the archive.
54+
*/
55+
public function getPath(): string
5356
{
5457
return $this->path;
5558
}
5659

57-
public function extension(): string
60+
/**
61+
* Get extension of the archive.
62+
*/
63+
public function getExtension(): string
5864
{
5965
return $this->extension;
6066
}
6167

62-
public function type(): ArchiveEnum
68+
/**
69+
* Get type of the archive.
70+
*/
71+
public function getType(): ArchiveEnum
6372
{
6473
return $this->type;
6574
}
6675

76+
/**
77+
* Get mime type of the archive.
78+
*/
6779
public static function getMimeType(string $path): ?string
6880
{
6981
try {

src/ArchiveZipCreate.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,40 @@
99

1010
class ArchiveZipCreate
1111
{
12-
/** @var SplFileInfo[] */
13-
protected array $files = [];
14-
15-
/** @var array<string, string> */
16-
protected array $strings = [];
17-
18-
protected int $count = 0;
19-
12+
/**
13+
* @param SplFileInfo[] $files
14+
* @param array<string, string> $strings
15+
*/
2016
protected function __construct(
2117
protected string $path,
2218
protected string $name,
19+
protected array $files = [],
20+
protected array $strings = [],
21+
protected int $count = 0,
2322
) {
2423
}
2524

26-
public static function make(string $path): self
25+
/**
26+
* Create a new instance of ArchiveZipCreate, allowing extensions are `zip`, `epub`, `cbz`.
27+
*
28+
* @param string $path Path to the archive
29+
* @param bool $skipAllowed Skip allowed extensions check
30+
*
31+
* @throws \Exception
32+
*/
33+
public static function make(string $path, bool $skipAllowed = false): self
2734
{
2835
$self = new self($path, pathinfo($path, PATHINFO_BASENAME));
2936

3037
$extension = pathinfo($path, PATHINFO_EXTENSION);
31-
if ($extension !== 'zip') {
32-
throw new \Exception("File {$path} is not a zip file, only zip files are supported.");
38+
39+
if (! $skipAllowed) {
40+
$allowedExtensions = ['zip', 'epub', 'cbz'];
41+
if (! in_array($extension, $allowedExtensions)) {
42+
$extensions = implode(', ', $allowedExtensions);
43+
throw new \Exception("File {$path} is not a zip file, only {$extensions} files are supported.");
44+
}
45+
3346
}
3447

3548
return $self;

src/Readers/BaseArchive.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ protected function __construct(
4545
) {
4646
}
4747

48+
/**
49+
* Create a new instance of Archive.
50+
*/
4851
abstract public static function read(string $path): self;
4952

5053
protected function setup(string $path): static
@@ -62,75 +65,123 @@ protected function setup(string $path): static
6265
}
6366

6467
/**
68+
* Extract all files from archive.
69+
*
6570
* @return string[]
6671
*/
6772
abstract public function extractAll(string $toPath): array;
6873

6974
/**
75+
* Extract selected files from archive.
76+
*
7077
* @param ArchiveItem[] $files
7178
* @return string[]
7279
*/
7380
abstract public function extract(string $toPath, array $files): array;
7481

82+
/**
83+
* Get path to the archive.
84+
*/
7585
public function getPath(): string
7686
{
7787
return $this->path;
7888
}
7989

90+
/**
91+
* Get extension of the archive.
92+
*/
8093
public function getExtension(): string
8194
{
8295
return $this->extension;
8396
}
8497

98+
/**
99+
* Get filename of the archive.
100+
*/
85101
public function getFilename(): string
86102
{
87103
return $this->filename;
88104
}
89105

106+
/**
107+
* Get basename of the archive.
108+
*/
90109
public function getBasename(): string
91110
{
92111
return $this->basename;
93112
}
94113

114+
/**
115+
* Get `ArchiveEnum` of the archive.
116+
*/
95117
public function getType(): ArchiveEnum
96118
{
97119
return $this->type;
98120
}
99121

122+
/**
123+
* Get first file from archive.
124+
*/
100125
public function getFirst(): ArchiveItem
101126
{
102127
return reset($this->files);
103128
}
104129

130+
/**
131+
* Get last file from archive.
132+
*/
105133
public function getLast(): ArchiveItem
106134
{
107135
return end($this->files);
108136
}
109137

138+
/**
139+
* Get files from archive.
140+
*
141+
* @return ArchiveItem[]
142+
*/
110143
public function getFiles(): array
111144
{
112145
return $this->files;
113146
}
114147

148+
/**
149+
* Get count of files.
150+
*/
115151
public function getCount(): int
116152
{
117153
return $this->count;
118154
}
119155

156+
/**
157+
* Get archive stat.
158+
*/
120159
public function getStat(): ?ArchiveStat
121160
{
122161
return $this->stat;
123162
}
124163

164+
/**
165+
* Get PDF metadata.
166+
*/
125167
public function getPdf(): ?PdfMeta
126168
{
127169
return $this->pdf;
128170
}
129171

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

177+
/**
178+
* Get text from file.
179+
*/
132180
abstract public function getText(ArchiveItem $file): ?string;
133181

182+
/**
183+
* Find file by search to get `ArchiveItem`.
184+
*/
134185
public function find(string $search, bool $skipHidden = true): ?ArchiveItem
135186
{
136187
$files = $this->findFiles($search, $skipHidden);
@@ -143,6 +194,8 @@ public function find(string $search, bool $skipHidden = true): ?ArchiveItem
143194
}
144195

145196
/**
197+
* Filter files by search to get `ArchiveItem[]`.
198+
*
146199
* @return ArchiveItem[]|null
147200
*/
148201
public function filter(string $search, bool $skipHidden = true): ?array

0 commit comments

Comments
 (0)