diff --git a/.github/workflows/run-windows.yml b/.github/workflows/run-windows.yml index 8f110ed..c0a08d6 100644 --- a/.github/workflows/run-windows.yml +++ b/.github/workflows/run-windows.yml @@ -12,8 +12,9 @@ jobs: [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 iex "& {$(irm get.scoop.sh)} -RunAsAdmin" scoop update + scoop install 7zip scoop checkup - scoop install 7zip unrar + scoop install unrar [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 shell: powershell diff --git a/README.md b/README.md index 07b1e58..4a5dba1 100644 --- a/README.md +++ b/README.md @@ -53,19 +53,19 @@ If you want more information, you can read section [**About**](#about). ## Features - List files as `ArchiveItem` array - - With `files` method: list of files - - With `first` method: first file - - With `last` method: last file + - With `getFiles` method: list of files + - With `getFirst` method: first file + - With `getLast` method: last file - With `find` method: find first file that match with `path` property - With `filter` method: find all files that match with `path` property - Content of file - - With `content` method: content of file as string (useful for images) - - With `text` method: content of text file (binaries files return `null`) + - With `getContent` method: content of file as string (useful for images) + - With `getText` method: content of text file (binaries files return `null`) - Extract files - With `extract` method: extract files to directory - With `extractAll` method: extract all files to directory -- Stat of archive with `path`, `deviceNumber`, `inodeNumber`, `inodeProtectionMode`, `numberOfLinks`, `userId`, `groupId`, `deviceType`, `size`, `lastAccessAt`, `createdAt`, `modifiedAt`, `blockSize`, `numberOfBlocks`, `status`, `comment` properties -- PDF metadata: `title`, `author`, `subject`, `creator`, `creationDate`, `modDate`, `pages`, +- Stat of archive with `getPath`, `getDeviceNumber`, `getInodeNumber`, `getInodeProtectionMode`, `getNumberOfLinks`, `getUserId`, `getGroupId`, `getDeviceType`, `getSize`, `getLastAccessAt`, `getCreatedAt`, `getModifiedAt`, `getBlockSize`, `getNumberOfBlocks`, `getStatus`, `getComment` properties +- PDF metadata: `getTitle`, `getAuthor`, `getSubject`, `getCreator`, `getCreationDate`, `getModDate`, `getPages`, - Count files - Create or edit archives, only with `.zip` format - With `make` method: create or edit archive @@ -91,12 +91,12 @@ With archive file (`.zip`, `.rar`, `.tar`, `.7z`, `epub`, `cbz`, `cbr`, `cb7`, ` ```php $archive = Archive::read('path/to/archive.zip'); -$files = $archive->files(); // ArchiveItem[] -$count = $archive->count(); // int of files count +$files = $archive->getFiles(); // ArchiveItem[] +$count = $archive->getCount(); // int of files count $images = $archive->filter('jpeg'); // ArchiveItem[] with `jpeg` in their path $metadataXml = $archive->find('metadata.xml'); // First ArchiveItem with `metadata.xml` in their path -$content = $archive->content($metadataXml); // `metadata.xml` file content +$content = $archive->getContent($metadataXml); // `metadata.xml` file content $paths = $archive->extract('/path/to/directory', [$metadataXml]); // string[] of extracted files paths $paths = $archive->extractAll('/path/to/directory'); // string[] of extracted files paths @@ -107,10 +107,10 @@ PDF files works with same API than archives but with some differences. ```php $archive = Archive::read('path/to/file.pdf'); -$pdf = $archive->pdf(); // Metadata of PDF +$pdf = $archive->getPdf(); // Metadata of PDF -$content = $archive->content($archive->first()); // PDF page as image -$text = $archive->text($archive->first()); // PDF page as text +$content = $archive->getContent($archive->getFirst()); // PDF page as image +$text = $archive->getText($archive->getFirst()); // PDF page as text ``` ### Stat @@ -123,21 +123,21 @@ From `stat` PHP function: $archive = Archive::read('path/to/file.zip'); $stat = $archive->stat(); -$stat->path(); // Path of file -$stat->deviceNumber(); // Device number -$stat->inodeNumber(); // Inode number -$stat->inodeProtectionMode(); // Inode protection mode -$stat->numberOfLinks(); // Number of links -$stat->userId(); // User ID -$stat->groupId(); // Group ID -$stat->deviceType(); // Device type -$stat->size(); // Size of file -$stat->lastAccessAt(); // Last access time -$stat->createdAt(); // Creation time -$stat->modifiedAt(); // Last modification time -$stat->blockSize(); // Block size -$stat->numberOfBlocks(); // Number of blocks -$stat->status(); // Status +$stat->getPath(); // Path of file +$stat->getDeviceNumber(); // Device number +$stat->getInodeNumber(); // Inode number +$stat->getInodeProtectionMode(); // Inode protection mode +$stat->getNumberOfLinks(); // Number of links +$stat->getUserId(); // User ID +$stat->getGroupId(); // Group ID +$stat->getDeviceType(); // Device type +$stat->getSize(); // Size of file +$stat->getLastAccessAt(); // Last access time +$stat->getCreatedAt(); // Creation time +$stat->getModifiedAt(); // Last modification time +$stat->getBlockSize(); // Block size +$stat->getNumberOfBlocks(); // Number of blocks +$stat->getStatus(); // Status ``` ### Create diff --git a/composer.json b/composer.json index 9e46048..724b1db 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "kiwilan/php-archive", - "version": "1.5.12", + "version": "2.0.0", "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", diff --git a/src/ArchiveTemporaryDirectory.php b/src/ArchiveTemporaryDirectory.php index 663d8d9..f74c751 100644 --- a/src/ArchiveTemporaryDirectory.php +++ b/src/ArchiveTemporaryDirectory.php @@ -13,7 +13,7 @@ protected function __construct( ) { } - public static function make(?string $filename = null): self + public static function make(string $filename = null): self { return new self(uniqid(), $filename); } diff --git a/src/ArchiveZipCreate.php b/src/ArchiveZipCreate.php index bdfe4e2..01c1ef7 100755 --- a/src/ArchiveZipCreate.php +++ b/src/ArchiveZipCreate.php @@ -35,17 +35,17 @@ public static function make(string $path): self return $self; } - public function path(): string + public function getPath(): string { return $this->path; } - public function name(): string + public function getName(): string { return $this->name; } - public function count(): int + public function getCount(): int { return $this->count; } @@ -53,7 +53,7 @@ public function count(): int /** * @return SplFileInfo[] */ - public function files(): array + public function getFiles(): array { return $this->files; } @@ -61,7 +61,7 @@ public function files(): array /** * @return array */ - public function strings(): array + public function getStrings(): array { return $this->strings; } diff --git a/src/Enums/ArchiveEnum.php b/src/Enums/ArchiveEnum.php index 15108ee..5aab6c0 100644 --- a/src/Enums/ArchiveEnum.php +++ b/src/Enums/ArchiveEnum.php @@ -10,7 +10,7 @@ enum ArchiveEnum: string case rar = 'rar'; case pdf = 'pdf'; - public static function fromExtension(string $extension, ?string $mimeType = null): self + public static function fromExtension(string $extension, string $mimeType = null): self { $extension = strtolower($extension); if (str_contains($extension, '.')) { diff --git a/src/Models/ArchiveItem.php b/src/Models/ArchiveItem.php index b976e4b..33cdcc2 100644 --- a/src/Models/ArchiveItem.php +++ b/src/Models/ArchiveItem.php @@ -34,7 +34,7 @@ public function __construct( ) { } - public static function fromP7zip(array $data, ?string $archivePath = null): self + public static function fromP7zip(array $data, string $archivePath = null): self { if (empty($data)) { throw new \Exception('No data provided.'); @@ -97,47 +97,47 @@ public static function fromP7zip(array $data, ?string $archivePath = null): self /** * Path encoded in base64. */ - public function id(): ?string + public function getId(): ?string { return $this->id; } - public function archivePath(): ?string + public function getArchivePath(): ?string { return $this->archivePath; } - public function filename(): ?string + public function getFilename(): ?string { return $this->filename; } - public function extension(): ?string + public function getExtension(): ?string { return $this->extension; } - public function path(): ?string + public function getPath(): ?string { return $this->path; } - public function rootPath(): ?string + public function getRootPath(): ?string { return $this->rootPath; } - public function sizeHuman(): ?string + public function getSizeHuman(): ?string { return $this->sizeHuman; } - public function size(): ?int + public function getSize(): ?int { return $this->size; } - public function packedSize(): ?int + public function getPackedSize(): ?int { return $this->packedSize; } @@ -157,27 +157,27 @@ public function isHidden(): bool return $this->isHidden; } - public function modified(): ?DateTime + public function getModified(): ?DateTime { return $this->modified; } - public function created(): ?DateTime + public function getCreated(): ?DateTime { return $this->created; } - public function accessed(): ?DateTime + public function getAccessed(): ?DateTime { return $this->accessed; } - public function extraInfos(): array + public function getExtraInfos(): array { return $this->extraInfos; } - public function hostOS(): ?string + public function getHostOS(): ?string { return $this->hostOS; } @@ -185,32 +185,32 @@ public function hostOS(): ?string public function toArray(): array { return [ - 'id' => $this->id(), - 'archivePath' => $this->archivePath(), + 'id' => $this->getId(), + 'archivePath' => $this->getArchivePath(), - 'filename' => $this->filename(), - 'extension' => $this->extension(), - 'path' => $this->path(), - 'rootPath' => $this->rootPath(), + 'filename' => $this->getFilename(), + 'extension' => $this->getExtension(), + 'path' => $this->getPath(), + 'rootPath' => $this->getRootPath(), - 'sizeHuman' => $this->sizeHuman(), - 'size' => $this->size(), - 'packedSize' => $this->packedSize(), + 'sizeHuman' => $this->getSizeHuman(), + 'size' => $this->getSize(), + 'packedSize' => $this->getPackedSize(), 'isDirectory' => $this->isDirectory(), 'isImage' => $this->isImage(), 'isHidden' => $this->isHidden(), - 'modified' => $this->modified(), - 'created' => $this->created(), - 'accessed' => $this->accessed(), + 'modified' => $this->getModified(), + 'created' => $this->getCreated(), + 'accessed' => $this->getAccessed(), - 'hostOS' => $this->hostOS(), + 'hostOS' => $this->getHostOS(), ]; } public function __toString(): string { - return $this->path(); + return $this->getPath(); } } diff --git a/src/Models/ArchiveStat.php b/src/Models/ArchiveStat.php index 63ce521..1b1acdb 100644 --- a/src/Models/ArchiveStat.php +++ b/src/Models/ArchiveStat.php @@ -48,82 +48,82 @@ public static function make(string $path): self return $self; } - public function path(): string + public function getPath(): string { return $this->path; } - public function deviceNumber(): ?int + public function getDeviceNumber(): ?int { return $this->deviceNumber; } - public function inodeNumber(): ?int + public function getInodeNumber(): ?int { return $this->inodeNumber; } - public function inodeProtectionMode(): ?int + public function getInodeProtectionMode(): ?int { return $this->inodeProtectionMode; } - public function numberOfLinks(): ?int + public function getNumberOfLinks(): ?int { return $this->numberOfLinks; } - public function userId(): ?int + public function getUserId(): ?int { return $this->userId; } - public function groupId(): ?int + public function getGroupId(): ?int { return $this->groupId; } - public function deviceType(): ?int + public function getDeviceType(): ?int { return $this->deviceType; } - public function size(): ?int + public function getSize(): ?int { return $this->size; } - public function lastAccessAt(): ?DateTime + public function getLastAccessAt(): ?DateTime { return $this->lastAccessAt; } - public function createdAt(): ?DateTime + public function getCreatedAt(): ?DateTime { return $this->createdAt; } - public function modifiedAt(): ?DateTime + public function getModifiedAt(): ?DateTime { return $this->modifiedAt; } - public function blockSize(): ?int + public function getBlockSize(): ?int { return $this->blockSize; } - public function numberOfBlocks(): ?int + public function getNumberOfBlocks(): ?int { return $this->numberOfBlocks; } - public function status(): ?string + public function getStatus(): ?string { return $this->status; } - public function comment(): ?string + public function getComment(): ?string { return $this->comment; } diff --git a/src/Models/PdfMeta.php b/src/Models/PdfMeta.php index 77a7f91..59d23e6 100644 --- a/src/Models/PdfMeta.php +++ b/src/Models/PdfMeta.php @@ -61,17 +61,17 @@ public static function make(array $details): self return $self; } - public function title(): ?string + public function getTitle(): ?string { return $this->title; } - public function author(): ?string + public function getAuthor(): ?string { return $this->author; } - public function subject(): ?string + public function getSubject(): ?string { return $this->subject; } @@ -79,27 +79,27 @@ public function subject(): ?string /** * @return array */ - public function keywords(): array + public function getKeywords(): array { return $this->keywords; } - public function creator(): ?string + public function getCreator(): ?string { return $this->creator; } - public function creationDate(): ?DateTime + public function getCreationDate(): ?DateTime { return $this->creationDate; } - public function modDate(): ?DateTime + public function getModDate(): ?DateTime { return $this->modDate; } - public function pages(): ?int + public function getPages(): ?int { return $this->pages; } diff --git a/src/Processes/SevenZipProcess.php b/src/Processes/SevenZipProcess.php index 9058510..d480edf 100644 --- a/src/Processes/SevenZipProcess.php +++ b/src/Processes/SevenZipProcess.php @@ -115,18 +115,18 @@ public function list(): array /** * @param ArchiveItem[] $files */ - public function extract(string $toPath, ?array $files = null): bool + public function extract(string $toPath, array $files = null): bool { if ($this->isRar && $this->isDarwin) { if ($files) { - $list = array_map(fn (ArchiveItem $item) => $item->path(), $files); + $list = array_map(fn (ArchiveItem $item) => $item->getPath(), $files); $this->execute('rar', ['x', '-y', $this->path, ...$list, "{$toPath}/"]); } else { $this->execute('rar', ['x', '-y', $this->path, "{$toPath}"]); } } else { if ($files) { - $list = array_map(fn (ArchiveItem $item) => $item->path(), $files); + $list = array_map(fn (ArchiveItem $item) => $item->getPath(), $files); $this->execute('7z', ['x', '-y', $this->path, "-o{$toPath}", ...$list, '-r']); } else { $this->execute('7z', ['x', '-y', $this->path, "-o{$toPath}", '-r']); @@ -140,7 +140,7 @@ public function content(ArchiveItem $file) { $archive = pathinfo($this->path, PATHINFO_BASENAME); $output = "{$this->outputDir}/{$archive}"; - $filePath = "{$output}/{$file->rootPath()}"; + $filePath = "{$output}/{$file->getRootPath()}"; $filePath = BaseArchive::pathToOsPath($filePath); if (! file_exists($filePath)) { @@ -148,9 +148,9 @@ public function content(ArchiveItem $file) } if ($this->isRar && $this->isDarwin) { - $this->execute('rar', ['x', '-y', $this->path, $file->path(), "{$output}/"]); + $this->execute('rar', ['x', '-y', $this->path, $file->getPath(), "{$output}/"]); } else { - $this->execute('7z', ['x', '-y', $this->path, "-o{$output}", $file->path(), '-r']); + $this->execute('7z', ['x', '-y', $this->path, "-o{$output}", $file->getPath(), '-r']); } $content = file_get_contents($filePath); BaseArchive::recurseRmdir($this->outputDir); diff --git a/src/Readers/ArchivePdf.php b/src/Readers/ArchivePdf.php index 176ce67..538b5ee 100755 --- a/src/Readers/ArchivePdf.php +++ b/src/Readers/ArchivePdf.php @@ -32,8 +32,8 @@ public function extract(string $toPath, array $files): array $paths = []; foreach ($this->files as $file) { if (in_array($file, $files)) { - $content = $this->content($file); - $toPathFile = "{$toPath}{$file->path()}.{$this->pdfExt}"; + $content = $this->getContent($file); + $toPathFile = "{$toPath}{$file->getPath()}.{$this->pdfExt}"; if (! is_dir(dirname($toPathFile))) { mkdir(dirname($toPathFile), 0755, true); @@ -47,7 +47,7 @@ public function extract(string $toPath, array $files): array return $paths; } - public function content(?ArchiveItem $file, bool $toBase64 = false): ?string + public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string { if (! $file) { return null; @@ -55,7 +55,7 @@ public function content(?ArchiveItem $file, bool $toBase64 = false): ?string $this->extensionImagickTest(); - $index = (int) $file->path(); + $index = (int) $file->getPath(); $format = $this->pdfExt; $format = 'jpg'; @@ -73,8 +73,8 @@ public function content(?ArchiveItem $file, bool $toBase64 = false): ?string $imagick->clear(); $imagick->destroy(); } catch (\Throwable $th) { - // throw new \Exception("Error, {$file->filename()} is not an image"); - error_log("Error, {$file->filename()} is not an image"); + // throw new \Exception("Error, {$file->getFilename()} is not an image"); + error_log("Error, {$file->getFilename()} is not an image"); } if (! $content) { @@ -86,16 +86,16 @@ public function content(?ArchiveItem $file, bool $toBase64 = false): ?string : $content; } - public function text(ArchiveItem $file): ?string + public function getText(ArchiveItem $file): ?string { if ($file->isImage()) { - throw new \Exception("Error, {$file->filename()} is an image"); + throw new \Exception("Error, {$file->getFilename()} is an image"); } - $index = (int) $file->path(); + $index = (int) $file->getPath(); $parser = new Parser(); - $document = $parser->parseFile($this->path()); + $document = $parser->parseFile($this->getPath()); $pages = $document->getPages(); @@ -112,7 +112,7 @@ public function text(ArchiveItem $file): ?string private function parse(): static { $parser = new Parser(); - $document = $parser->parseFile($this->path()); + $document = $parser->parseFile($this->getPath()); $this->stat = ArchiveStat::make($this->path); $this->pdf = PdfMeta::make($document->getDetails()); diff --git a/src/Readers/ArchivePhar.php b/src/Readers/ArchivePhar.php index c31dbf4..3e3d549 100755 --- a/src/Readers/ArchivePhar.php +++ b/src/Readers/ArchivePhar.php @@ -18,33 +18,33 @@ public static function read(string $path): self return $self; } - public function content(?ArchiveItem $file, bool $toBase64 = false): ?string + public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string { if (! $file) { return null; } - $content = file_get_contents($file->path()); + $content = file_get_contents($file->getPath()); return $toBase64 ? base64_encode($content) : $content; } - public function text(ArchiveItem $file): ?string + public function getText(ArchiveItem $file): ?string { if ($file->isImage()) { - throw new \Exception("Error, {$file->filename()} is an image"); + throw new \Exception("Error, {$file->getFilename()} is an image"); } - return $this->content($file); + return $this->getContent($file); } public function extract(string $toPath, array $files): array { $paths = []; foreach ($files as $file) { - $content = $this->content($file); + $content = $this->getContent($file); - $toPathFile = "{$toPath}{$file->rootPath()}"; + $toPathFile = "{$toPath}{$file->getRootPath()}"; if (! is_dir(dirname($toPathFile))) { mkdir(dirname($toPathFile), 0755, true); @@ -61,7 +61,7 @@ public function extractAll(string $toPath): array { $phar = new PharData($this->path); $phar->extractTo($toPath, null, true); - $files = $this->getFiles($toPath); + $files = $this->getAllFiles($toPath); return $files; } @@ -72,7 +72,7 @@ private function parse(): static $phar->extractTo($this->outputDirectory, null, true); $this->stat = ArchiveStat::make($this->path); - $files = $this->getFiles($this->outputDirectory); + $files = $this->getAllFiles($this->outputDirectory); foreach ($files as $item) { $file = new SplFileInfo($item); diff --git a/src/Readers/ArchiveRar.php b/src/Readers/ArchiveRar.php index 62fc8ac..39c4662 100755 --- a/src/Readers/ArchiveRar.php +++ b/src/Readers/ArchiveRar.php @@ -39,7 +39,7 @@ public function extract(string $toPath, array $files): array $paths = []; $this->parser(function (ArchiveItem $file, $stream) use ($files, $toPath, &$paths) { if (in_array($file, $files)) { - $toPathFile = "{$toPath}{$file->rootPath()}"; + $toPathFile = "{$toPath}{$file->getRootPath()}"; if (! is_dir(dirname($toPathFile))) { mkdir(dirname($toPathFile), 0755, true); @@ -53,14 +53,14 @@ public function extract(string $toPath, array $files): array return $paths; } - public function content(?ArchiveItem $item, bool $toBase64 = false): ?string + public function getContent(?ArchiveItem $item, bool $toBase64 = false): ?string { if (! $item) { return null; } $content = $this->parser(function (ArchiveItem $file, $stream) use ($item) { - if ($file->rootPath() === $item->rootPath()) { + if ($file->getRootPath() === $item->getRootPath()) { return $this->convertStream($stream); } }); @@ -70,13 +70,13 @@ public function content(?ArchiveItem $item, bool $toBase64 = false): ?string : $content; } - public function text(ArchiveItem $file): ?string + public function getText(ArchiveItem $file): ?string { if ($file->isImage()) { - throw new \Exception("Error, {$file->filename()} is an image"); + throw new \Exception("Error, {$file->getFilename()} is an image"); } - return $this->content($file); + return $this->getContent($file); } private function parse(): static @@ -99,14 +99,14 @@ private function parse(): static } /** - * @param Closure(ArchiveItem $file, resource $stream): mixed $closure + * @param Closure(ArchiveItem $file, resource $stream): mixed $closure */ private function parser(Closure $closure): mixed { - $archive = RarArchive::open($this->path()); + $archive = RarArchive::open($this->getPath()); if ($archive->isBroken()) { - throw new \Exception("Archive is broken {$this->path()}"); + throw new \Exception("Archive is broken {$this->getPath()}"); } foreach ($archive->getEntries() as $key => $entry) { diff --git a/src/Readers/ArchiveSevenZip.php b/src/Readers/ArchiveSevenZip.php index 3264237..719f18b 100755 --- a/src/Readers/ArchiveSevenZip.php +++ b/src/Readers/ArchiveSevenZip.php @@ -17,7 +17,7 @@ public static function read(string $path): self return $self; } - public function content(?ArchiveItem $file, bool $toBase64 = false): ?string + public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string { if (! $file) { return null; @@ -31,13 +31,13 @@ public function content(?ArchiveItem $file, bool $toBase64 = false): ?string : $content; } - public function text(ArchiveItem $file): ?string + public function getText(ArchiveItem $file): ?string { if ($file->isImage()) { - throw new \Exception("Error, {$file->filename()} is an image"); + throw new \Exception("Error, {$file->getFilename()} is an image"); } - return $this->content($file); + return $this->getContent($file); } public function extract(string $toPath, array $files): array diff --git a/src/Readers/ArchiveZip.php b/src/Readers/ArchiveZip.php index 42418af..fc28c84 100755 --- a/src/Readers/ArchiveZip.php +++ b/src/Readers/ArchiveZip.php @@ -23,8 +23,8 @@ public function extract(string $toPath, array $files): array $paths = []; $this->parser(function (ArchiveItem $item, ZipArchive $archive, int $i) use (&$files, $toPath, &$paths) { if (in_array($item, $files)) { - $content = $this->content($item); - $toPathFile = "{$toPath}{$item->rootPath()}"; + $content = $this->getContent($item); + $toPathFile = "{$toPath}{$item->getRootPath()}"; if (! is_dir(dirname($toPathFile))) { mkdir(dirname($toPathFile), 0755, true); @@ -44,21 +44,21 @@ public function extractAll(string $toPath): array $archive->open($this->path); $archive->extractTo($toPath); - $files = $this->getFiles($toPath); + $files = $this->getAllFiles($toPath); $archive->close(); return $files; } - public function content(?ArchiveItem $file, bool $toBase64 = false): ?string + public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string { if (! $file) { return null; } $content = $this->parser(function (ArchiveItem $item, ZipArchive $archive, int $i) use ($file) { - if ($item->filename() === $file->filename()) { + if ($item->getFilename() === $file->getFilename()) { return $archive->getFromIndex($i); } @@ -68,13 +68,13 @@ public function content(?ArchiveItem $file, bool $toBase64 = false): ?string return $toBase64 ? base64_encode($content) : $content; } - public function text(ArchiveItem $file): ?string + public function getText(ArchiveItem $file): ?string { if ($file->isImage()) { - throw new \Exception("Error, {$file->filename()} is an image"); + throw new \Exception("Error, {$file->getFilename()} is an image"); } - return $this->content($file); + return $this->getContent($file); } private function parse(): static @@ -101,7 +101,7 @@ private function parse(): static } /** - * @param Closure(ArchiveItem $file, ZipArchive $archive, int $i): mixed $closure + * @param Closure(ArchiveItem $file, ZipArchive $archive, int $i): mixed $closure */ private function parser(Closure $closure): mixed { diff --git a/src/Readers/BaseArchive.php b/src/Readers/BaseArchive.php index 58f7a45..730e8a8 100755 --- a/src/Readers/BaseArchive.php +++ b/src/Readers/BaseArchive.php @@ -72,64 +72,64 @@ abstract public function extractAll(string $toPath): array; */ abstract public function extract(string $toPath, array $files): array; - public function path(): string + public function getPath(): string { return $this->path; } - public function extension(): string + public function getExtension(): string { return $this->extension; } - public function filename(): string + public function getFilename(): string { return $this->filename; } - public function basename(): string + public function getBasename(): string { return $this->basename; } - public function type(): ArchiveEnum + public function getType(): ArchiveEnum { return $this->type; } - public function first(): ArchiveItem + public function getFirst(): ArchiveItem { return reset($this->files); } - public function last(): ArchiveItem + public function getLast(): ArchiveItem { return end($this->files); } - public function files(): array + public function getFiles(): array { return $this->files; } - public function count(): int + public function getCount(): int { return $this->count; } - public function stat(): ?ArchiveStat + public function getStat(): ?ArchiveStat { return $this->stat; } - public function pdf(): ?PdfMeta + public function getPdf(): ?PdfMeta { return $this->pdf; } - abstract public function content(?ArchiveItem $file, bool $toBase64 = false): ?string; + abstract public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string; - abstract public function text(ArchiveItem $file): ?string; + abstract public function getText(ArchiveItem $file): ?string; public function find(string $search, bool $skipHidden = true): ?ArchiveItem { @@ -181,7 +181,7 @@ protected function convertStream(mixed $stream): string */ protected function findFiles(string $search, bool $skipHidden): array { - $files = $this->files(); + $files = $this->getFiles(); $filtered = array_filter($files, function (ArchiveItem $file) use ($search, $skipHidden) { $isExtension = ! str_contains($search, '.'); @@ -191,13 +191,13 @@ protected function findFiles(string $search, bool $skipHidden): array } if ($isExtension) { - return $file->extension() === $search; + return $file->getExtension() === $search; } else { - return str_contains($file->path(), $search); + return str_contains($file->getPath(), $search); } }); - $property = 'rootPath'; + $property = 'getRootPath'; $sort = fn ($a, $b) => strnatcmp($a->{$property}(), $b->{$property}()); usort($filtered, $sort); @@ -206,7 +206,7 @@ protected function findFiles(string $search, bool $skipHidden): array protected function sortFiles() { - usort($this->files, fn (ArchiveItem $a, ArchiveItem $b) => strcmp($a->path(), $b->path())); + usort($this->files, fn (ArchiveItem $a, ArchiveItem $b) => strcmp($a->getPath(), $b->getPath())); $this->files = array_values($this->files); } @@ -260,7 +260,7 @@ public static function binaryP7zipTest(bool $exception = true): bool return true; } - protected function getFiles(string $path): array + protected function getAllFiles(string $path): array { $files = array_diff(scandir($path), ['.', '..']); @@ -270,7 +270,7 @@ protected function getFiles(string $path): array $fullPath = $path.DIRECTORY_SEPARATOR.$file; if (is_dir($fullPath)) { - $items = array_merge($items, $this->getFiles($fullPath)); + $items = array_merge($items, $this->getAllFiles($fullPath)); } else { $items[] = $fullPath; } diff --git a/tests/ArchiveCreateTest.php b/tests/ArchiveCreateTest.php index b682cae..11b0a84 100644 --- a/tests/ArchiveCreateTest.php +++ b/tests/ArchiveCreateTest.php @@ -19,11 +19,11 @@ $archive->addFiles($medias); $archive->save(); - expect($archive->path())->toBe($path); - expect($archive->name())->toBe('test.zip'); - expect($archive->path())->toBeReadableFile($path); - expect($archive->count())->toBe(5); - expect($archive->files())->toBeArray() + expect($archive->getPath())->toBe($path); + expect($archive->getName())->toBe('test.zip'); + expect($archive->getPath())->toBeReadableFile($path); + expect($archive->getCount())->toBe(5); + expect($archive->getFiles())->toBeArray() ->each(fn ($file) => expect($file->value)->toBeInstanceOf(SplFileInfo::class)); }); @@ -41,8 +41,8 @@ $archive->addFromString('test.txt', 'Hello World!'); $archive->save(); - expect($archive->path())->toBeReadableFile($path); - expect($archive->count())->toBe(6); + expect($archive->getPath())->toBeReadableFile($path); + expect($archive->getCount())->toBe(6); }); it('can create with strings', function () { @@ -53,8 +53,8 @@ $archive->addFromString('test-2.txt', 'Hello World!'); $archive->save(); - expect($archive->path())->toBeReadableFile($path); - expect($archive->count())->toBe(2); + expect($archive->getPath())->toBeReadableFile($path); + expect($archive->getCount())->toBe(2); }); it('can create with directory', function () { @@ -64,8 +64,8 @@ $archive->addDirectory(mediaPath('archive')); $archive->save(); - expect($archive->path())->toBeReadableFile($path); - expect($archive->count())->toBe(5); + expect($archive->getPath())->toBeReadableFile($path); + expect($archive->getCount())->toBe(5); }); it('can edit', function () { @@ -79,6 +79,6 @@ $archive->addFromString('test.txt', 'Hello World!'); $archive->save(); - expect($archive->path())->toBeReadableFile($path); - expect($archive->count())->toBe(6); + expect($archive->getPath())->toBeReadableFile($path); + expect($archive->getCount())->toBe(6); }); diff --git a/tests/ArchiveItemTest.php b/tests/ArchiveItemTest.php index aea4885..7d8f40e 100644 --- a/tests/ArchiveItemTest.php +++ b/tests/ArchiveItemTest.php @@ -87,26 +87,26 @@ $modifiedFormat = $modified->format('Y-m-d H:i:s'); $accessedFormat = $accessed->format('Y-m-d H:i:s'); - expect($item->id())->toBe(base64_encode('archive/metadata.xml')); - expect($item->filename())->toBe('metadata.xml'); - expect($item->extension())->toBe('xml'); - expect($item->path())->toBe('archive/metadata.xml'); + expect($item->getId())->toBe(base64_encode('archive/metadata.xml')); + expect($item->getFilename())->toBe('metadata.xml'); + expect($item->getExtension())->toBe('xml'); + expect($item->getPath())->toBe('archive/metadata.xml'); expect($item->isDirectory())->toBe(false); expect($item->isHidden())->toBe(false); - expect($item->sizeHuman())->toBe(BaseArchive::bytesToHuman(313)); + expect($item->getSizeHuman())->toBe(BaseArchive::bytesToHuman(313)); // expect($item->folder())->toBe('-'); - expect($item->size())->toBe(313); - expect($item->packedSize())->toBe(199); - expect($item->modified()->format('Y-m-d H:i:s'))->toBe($modifiedFormat); - expect($item->created())->toBeNull(); - expect($item->accessed()->format('Y-m-d H:i:s'))->toBe($accessedFormat); + expect($item->getSize())->toBe(313); + expect($item->getPackedSize())->toBe(199); + expect($item->getModified()->format('Y-m-d H:i:s'))->toBe($modifiedFormat); + expect($item->getCreated())->toBeNull(); + expect($item->getAccessed()->format('Y-m-d H:i:s'))->toBe($accessedFormat); // expect($item->attributes())->toBe('_ -rw-r--r--'); // expect($item->encrypted())->toBe('-'); // expect($item->comment())->toBe(''); // expect($item->crc())->toBe('89F4DD2B'); // expect($item->method())->toBe('Deflate'); // expect($item->characteristics())->toBe('UT 0x7875'); - expect($item->hostOS())->toBe('Unix'); + expect($item->getHostOS())->toBe('Unix'); // expect($item->versionIndex())->toBe(20); // expect($item->volume())->toBe(0); // expect($item->offset())->toBe(116929); diff --git a/tests/ArchiveStatTest.php b/tests/ArchiveStatTest.php index 08a0c91..3f03a88 100644 --- a/tests/ArchiveStatTest.php +++ b/tests/ArchiveStatTest.php @@ -8,24 +8,24 @@ it('can read', function (string $path) { $archive = Archive::read($path); - $stat = $archive->stat(); + $stat = $archive->getStat(); - expect($stat->path())->toBeString(); - expect($stat->deviceNumber())->toBeInt(); - expect($stat->inodeNumber())->toBeInt(); - expect($stat->inodeProtectionMode())->toBeInt(); - expect($stat->numberOfLinks())->toBeInt(); - expect($stat->userId())->toBeInt(); - expect($stat->groupId())->toBeInt(); - expect($stat->deviceType())->toBeInt(); - expect($stat->size())->toBeInt(); - expect($stat->lastAccessAt())->toBeInstanceOf(DateTime::class); - expect($stat->createdAt())->toBeInstanceOf(DateTime::class); - expect($stat->modifiedAt())->toBeInstanceOf(DateTime::class); - expect($stat->blockSize())->toBeInt(); - expect($stat->numberOfBlocks())->toBeInt(); - if ($stat->status()) { - expect($stat->status())->toBeString(); + expect($stat->getPath())->toBeString(); + expect($stat->getDeviceNumber())->toBeInt(); + expect($stat->getInodeNumber())->toBeInt(); + expect($stat->getInodeProtectionMode())->toBeInt(); + expect($stat->getNumberOfLinks())->toBeInt(); + expect($stat->getUserId())->toBeInt(); + expect($stat->getGroupId())->toBeInt(); + expect($stat->getDeviceType())->toBeInt(); + expect($stat->getSize())->toBeInt(); + expect($stat->getLastAccessAt())->toBeInstanceOf(DateTime::class); + expect($stat->getCreatedAt())->toBeInstanceOf(DateTime::class); + expect($stat->getModifiedAt())->toBeInstanceOf(DateTime::class); + expect($stat->getBlockSize())->toBeInt(); + expect($stat->getNumberOfBlocks())->toBeInt(); + if ($stat->getStatus()) { + expect($stat->getStatus())->toBeString(); } expect($stat->toArray())->toBeArray(); diff --git a/tests/ArchiveTest.php b/tests/ArchiveTest.php index 9b2d22a..ee3b95f 100644 --- a/tests/ArchiveTest.php +++ b/tests/ArchiveTest.php @@ -15,17 +15,17 @@ $extension = pathinfo($path, PATHINFO_EXTENSION); $type = ArchiveEnum::fromExtension($extension, Archive::getMimeType($path)); - expect($archive->extension())->toBe($extension); - expect($archive->path())->toBe($path); - expect($archive->type())->toBe($type); + expect($archive->getExtension())->toBe($extension); + expect($archive->getPath())->toBe($path); + expect($archive->getType())->toBe($type); })->with([...ARCHIVES_ZIP, ...ARCHIVES_TAR, ...ARCHIVES_PDF, ...ARCHIVES_RAR, SEVENZIP]); it('can get text', function (string $path) { $archive = Archive::read($path); - $files = $archive->files(); + $files = $archive->getFiles(); $first = array_filter($files, fn (ArchiveItem $item) => ! $item->isImage()); $first = array_shift($first); - $text = $archive->text($first); + $text = $archive->getText($first); expect($text)->toBeString(); })->with([...ARCHIVES_ZIP, ...ARCHIVES_TAR, ...ARCHIVES_PDF, ...ARCHIVES_RAR, SEVENZIP]); @@ -36,17 +36,17 @@ it('can get metadata', function (string $path) { $archive = Archive::read($path); - $stat = $archive->stat(); + $stat = $archive->getStat(); expect($stat)->toBeInstanceOf(ArchiveStat::class); })->with([...ARCHIVES_ZIP, ...ARCHIVES_TAR, ...ARCHIVES_PDF, ...ARCHIVES_RAR, SEVENZIP]); it('can get files', function (string $path) { $archive = Archive::read($path); - $files = $archive->files(); + $files = $archive->getFiles(); expect($files)->toBeArray(); - expect($files)->toHaveCount($archive->count()); + expect($files)->toHaveCount($archive->getCount()); })->with([...ARCHIVES_ZIP, ...ARCHIVES_TAR, ...ARCHIVES_PDF, ...ARCHIVES_RAR, SEVENZIP]); it('can find all images', function (string $path) { @@ -63,14 +63,14 @@ function (Pest\Expectation $item) use ($ext) { $file = $item->value; expect($file)->toBeInstanceOf(ArchiveItem::class); - expect($file->extension())->toBe($ext); + expect($file->getExtension())->toBe($ext); } ); })->with([...ARCHIVES_ZIP, ...ARCHIVES_TAR, ...ARCHIVES_RAR, SEVENZIP]); it('can get content first file', function (string $path) { $archive = Archive::read($path); - $content = $archive->content($archive->first()); + $content = $archive->getContent($archive->getFirst()); $output = outputPath(); $file = BaseArchive::pathToOsPath("{$output}first.jpg"); @@ -83,7 +83,7 @@ function (Pest\Expectation $item) use ($ext) { it('can get cover', function (string $path) { $archive = Archive::read($path); $cover = $archive->find('cover.jpeg'); - $content = $archive->content($cover); + $content = $archive->getContent($cover); $output = outputPath(); $coverPath = "{$output}cover.jpeg"; @@ -97,7 +97,7 @@ function (Pest\Expectation $item) use ($ext) { it('can cover with base64', function (string $path) { $archive = Archive::read($path); $cover = $archive->find('cover.jpeg'); - $content = $archive->content($cover, true); + $content = $archive->getContent($cover, true); $isBase64 = isBase64($content); expect($isBase64)->toBeTrue(); @@ -105,8 +105,8 @@ function (Pest\Expectation $item) use ($ext) { it('can extract some files', function (string $path) { $archive = Archive::read($path); - $files = $archive->files(); - $output = outputPath($archive->basename()); + $files = $archive->getFiles(); + $output = outputPath($archive->getBasename()); $select = [$files[0], $files[1]]; $paths = $archive->extract($output, $select); @@ -124,3 +124,7 @@ function (Pest\Expectation $item) use ($ext) { expect($paths)->toBeArray(); expect($paths)->toBeGreaterThanOrEqual(5); })->with([...ARCHIVES_ZIP, ...ARCHIVES_TAR, ...ARCHIVES_RAR, SEVENZIP]); + +it('can handle bad archive', function (string $path) { + expect(fn () => Archive::read($path))->toThrow(ValueError::class); +})->with([EPUB_BAD_FILE]); diff --git a/tests/EbookTest.php b/tests/EbookTest.php index 997ca9c..f0a2cef 100644 --- a/tests/EbookTest.php +++ b/tests/EbookTest.php @@ -5,26 +5,26 @@ it('can read epub', function () { $archive = Archive::read(EPUB); - $files = $archive->files(); + $files = $archive->getFiles(); $extension = pathinfo(EPUB, PATHINFO_EXTENSION); - expect($archive->extension())->toBe($extension); - expect($archive->path())->toBe(EPUB); - expect($archive->type())->toBeInstanceOf(ArchiveEnum::class); + expect($archive->getExtension())->toBe($extension); + expect($archive->getPath())->toBe(EPUB); + expect($archive->getType())->toBeInstanceOf(ArchiveEnum::class); expect($files)->toBeIterable(); - expect($archive->count())->toBeGreaterThan(0); + expect($archive->getCount())->toBeGreaterThan(0); }); it('can read cba', function (string $path) { $archive = Archive::read($path); - $files = $archive->files(); + $files = $archive->getFiles(); $extension = pathinfo($path, PATHINFO_EXTENSION); - expect($archive->extension())->toBe($extension); - expect($archive->path())->toBe($path); - expect($archive->type())->toBeInstanceOf(ArchiveEnum::class); + expect($archive->getExtension())->toBe($extension); + expect($archive->getPath())->toBe($path); + expect($archive->getType())->toBeInstanceOf(ArchiveEnum::class); expect($files)->toBeIterable(); - expect($archive->count())->toBeGreaterThan(0); + expect($archive->getCount())->toBeGreaterThan(0); })->with(CBA_ITEMS); it('can get cover with cba', function (string $path) { diff --git a/tests/PdfTest.php b/tests/PdfTest.php index 8db6ead..c2fcac5 100644 --- a/tests/PdfTest.php +++ b/tests/PdfTest.php @@ -8,15 +8,15 @@ it('can get files', function () { $archive = Archive::read(PDF); - $files = $archive->files(); + $files = $archive->getFiles(); expect($files)->toBeArray(); - expect($files)->toHaveCount($archive->count()); + expect($files)->toHaveCount($archive->getCount()); }); it('can get content first file', function () { $archive = Archive::read(PDF); - $content = $archive->content($archive->first()); + $content = $archive->getContent($archive->getFirst()); $output = outputPath(); $file = "{$output}first.jpg"; @@ -28,8 +28,8 @@ it('can extract some files', function () { $archive = Archive::read(PDF); - $files = $archive->files(); - $output = outputPath($archive->basename()); + $files = $archive->getFiles(); + $output = outputPath($archive->getBasename()); $select = [$files[0], $files[1]]; $paths = $archive->extract($output, $select); @@ -50,17 +50,17 @@ it('can read metadata', function () { $archive = Archive::read(PDF); - $pdf = $archive->pdf(); + $pdf = $archive->getPdf(); - expect($pdf->title())->toBe('Example PDF'); - expect($pdf->author())->toBe('Ewilan Rivière'); - expect($pdf->subject())->toBe('This is an example PDF for php-archive package tests.'); - expect($pdf->keywords())->toBe(['test', 'pdf', 'example']); - expect($pdf->creator())->toBe('Kiwilan'); - expect($pdf->creationDate())->toBeInstanceOf(\DateTime::class); - expect($pdf->modDate())->toBeInstanceOf(\DateTime::class); - expect($pdf->pages())->toBe(4); - expect($pdf->keywords())->toBeArray(); + expect($pdf->getTitle())->toBe('Example PDF'); + expect($pdf->getAuthor())->toBe('Ewilan Rivière'); + expect($pdf->getSubject())->toBe('This is an example PDF for php-archive package tests.'); + expect($pdf->getKeywords())->toBe(['test', 'pdf', 'example']); + expect($pdf->getCreator())->toBe('Kiwilan'); + expect($pdf->getCreationDate())->toBeInstanceOf(\DateTime::class); + expect($pdf->getModDate())->toBeInstanceOf(\DateTime::class); + expect($pdf->getPages())->toBe(4); + expect($pdf->getKeywords())->toBeArray(); expect($pdf->toArray())->toBeArray(); expect($pdf->toJson())->toBeString(); }); diff --git a/tests/Pest.php b/tests/Pest.php index a0e435d..b6d2910 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -21,6 +21,7 @@ 'ZIP' => ZIP, ]); define('EPUB', __DIR__.'/media/epub.epub'); +define('EPUB_BAD_FILE', __DIR__.'/media/epub-bad-file.epub'); define('CBZ', __DIR__.'/media/cba.cbz'); define('CBR', __DIR__.'/media/cba.cbr'); define('CBT', __DIR__.'/media/cba.cbt'); @@ -61,7 +62,7 @@ function mediaPath(string $filename): string return $pathBase.$filename; } -function outputPath(?string $path = null, ?string $filename = null): string +function outputPath(string $path = null, string $filename = null): string { $pathBase = __DIR__.'/output/'; diff --git a/tests/media/epub-bad-file.epub b/tests/media/epub-bad-file.epub new file mode 100644 index 0000000..955947f --- /dev/null +++ b/tests/media/epub-bad-file.epub @@ -0,0 +1,2 @@ +clsTbsZip ERROR with the zip archive: The End of Central Directory Record is not found.
+Exception : Failed to read epub file \ No newline at end of file