Skip to content

Commit

Permalink
Merge pull request #55 from kiwilan/develop
Browse files Browse the repository at this point in the history
v2.2.01
  • Loading branch information
ewilan-riviere authored Dec 6, 2023
2 parents 44646e4 + 58e3207 commit 4e8d0e4
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.1, 8.2]
php: [8.1, 8.2, 8.3]
stability: [prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,15 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio

[<img src="https://user-images.githubusercontent.com/48261459/201463225-0a5a084e-df15-4b11-b1d2-40fafd3555cf.svg" height="120rem" width="100%" />](https://github.com/kiwilan)

[version-src]: https://img.shields.io/packagist/v/kiwilan/php-ebook.svg?style=flat-square&colorA=18181B&colorB=777BB4
[version-src]: https://img.shields.io/packagist/v/kiwilan/php-ebook.svg?style=flat&colorA=18181B&colorB=777BB4
[version-href]: https://packagist.org/packages/kiwilan/php-ebook
[php-version-src]: https://img.shields.io/static/v1?style=flat-square&label=PHP&message=v8.1&color=777BB4&logo=php&logoColor=ffffff&labelColor=18181b
[php-version-src]: https://img.shields.io/static/v1?style=flat&label=PHP&message=v8.1&color=777BB4&logo=php&logoColor=ffffff&labelColor=18181b
[php-version-href]: https://www.php.net/
[downloads-src]: https://img.shields.io/packagist/dt/kiwilan/php-ebook.svg?style=flat-square&colorA=18181B&colorB=777BB4
[downloads-src]: https://img.shields.io/packagist/dt/kiwilan/php-ebook.svg?style=flat&colorA=18181B&colorB=777BB4
[downloads-href]: https://packagist.org/packages/kiwilan/php-ebook
[license-src]: https://img.shields.io/github/license/kiwilan/php-ebook.svg?style=flat-square&colorA=18181B&colorB=777BB4
[license-src]: https://img.shields.io/github/license/kiwilan/php-ebook.svg?style=flat&colorA=18181B&colorB=777BB4
[license-href]: https://github.com/kiwilan/php-ebook/blob/main/README.md
[tests-src]: https://img.shields.io/github/actions/workflow/status/kiwilan/php-ebook/run-tests.yml?branch=main&label=tests&style=flat-square&colorA=18181B
[tests-src]: https://img.shields.io/github/actions/workflow/status/kiwilan/php-ebook/run-tests.yml?branch=main&label=tests&style=flat&colorA=18181B
[tests-href]: https://packagist.org/packages/kiwilan/php-ebook
[codecov-src]: https://codecov.io/gh/kiwilan/php-ebook/branch/main/graph/badge.svg?token=P9XIK2KV9G
[codecov-src]: https://img.shields.io/codecov/c/gh/kiwilan/php-ebook/main?style=flat&colorA=18181B&colorB=777BB4
[codecov-href]: https://codecov.io/gh/kiwilan/php-ebook
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kiwilan/php-ebook",
"description": "PHP package to read metadata and extract covers from eBooks, comics and audiobooks.",
"version": "2.2.0",
"version": "2.2.01",
"keywords": [
"php",
"ebook",
Expand Down Expand Up @@ -39,7 +39,7 @@
],
"require": {
"php": "^8.1",
"kiwilan/php-archive": "^2.1.02",
"kiwilan/php-archive": "^2.2.0",
"kiwilan/php-audio": "^3.0.01",
"kiwilan/php-xml-reader": "^1.0.11"
},
Expand All @@ -63,7 +63,7 @@
"scripts": {
"test": "vendor/bin/pest",
"test-filter": "vendor/bin/pest --filter",
"test-parallel": "vendor/bin/pest --parallel",
"test-parallel": "vendor/bin/pest --parallel --coverage",
"test-coverage": "vendor/bin/pest --coverage --min=90",
"test-coverage-parallel": "vendor/bin/pest --parallel --coverage",
"analyse": "vendor/bin/phpstan analyse",
Expand Down
56 changes: 38 additions & 18 deletions src/Ebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use Kiwilan\Ebook\Formats\Audio\AudiobookModule;
use Kiwilan\Ebook\Formats\Cba\CbaModule;
use Kiwilan\Ebook\Formats\Djvu\DjvuModule;
use Kiwilan\Ebook\Formats\EbookMetadata;
use Kiwilan\Ebook\Formats\EbookModule;
use Kiwilan\Ebook\Formats\EbookParser;
use Kiwilan\Ebook\Formats\Epub\EpubModule;
use Kiwilan\Ebook\Formats\Fb2\Fb2Module;
use Kiwilan\Ebook\Formats\Mobi\MobiModule;
Expand Down Expand Up @@ -81,8 +81,8 @@ protected function __construct(
protected bool $isAudio = false,
protected bool $isMobi = false,
protected bool $isBadFile = false,
protected ?EbookMetadata $metadata = null,
protected bool $hasMetadata = false,
protected ?EbookParser $parser = null,
protected bool $hasParser = false,
) {
}

Expand All @@ -109,9 +109,9 @@ public static function read(string $path): ?self
return null;
}

$self->metadata = EbookMetadata::make($format);
$self->parser = EbookParser::make($format);
$self->convertEbook();
$self->cover = $self->metadata->getModule()->toCover();
$self->cover = $self->parser->getModule()->toCover();
$self->metaTitle = MetaTitle::make($self);

$time = microtime(true) - $start;
Expand Down Expand Up @@ -255,7 +255,7 @@ private function pdf(): EbookModule

private function convertEbook(): self
{
$ebook = $this->metadata->getModule()->toEbook();
$ebook = $this->parser->getModule()->toEbook();

$this->title = $ebook->getTitle();
$this->metaTitle = $ebook->getMetaTitle();
Expand All @@ -278,7 +278,7 @@ private function convertEbook(): self
private function convertCounts(): self
{
$this->countsParsed = true;
$counts = $this->metadata->getModule()->toCounts();
$counts = $this->parser->getModule()->toCounts();

$this->wordsCount = $counts->getWordsCount();
$this->pagesCount = $counts->getPagesCount();
Expand Down Expand Up @@ -348,7 +348,7 @@ public function getAuthors(): array
*
* @param int|null $limit Limit the length of the description.
*/
public function getDescription(int $limit = null): ?string
public function getDescription(?int $limit = null): ?string
{
if ($limit) {
return $this->limitLength($this->description, $limit);
Expand Down Expand Up @@ -432,7 +432,7 @@ public function getVolume(): ?int
/**
* Copyright of the book.
*/
public function getCopyright(int $limit = null): ?string
public function getCopyright(?int $limit = null): ?string
{
if ($limit) {
return $this->limitLength($this->copyright, $limit);
Expand Down Expand Up @@ -526,11 +526,21 @@ public function isArchive(): bool
}

/**
* Whether the ebook has metadata.
* Whether the ebook has parser.
*
* @deprecated Use `hasParser()` instead.
*/
public function hasMetadata(): bool
{
return $this->hasMetadata;
return $this->hasParser;
}

/**
* Whether the ebook has parser.
*/
public function hasParser(): bool
{
return $this->hasParser;
}

/**
Expand All @@ -542,11 +552,21 @@ public function getFormat(): ?EbookFormatEnum
}

/**
* Metadata of the ebook.
* Parser of the ebook.
*
* @deprecated Use `getParser()` instead.
*/
public function getMetadata(): ?EbookParser
{
return $this->parser;
}

/**
* Parser of the ebook.
*/
public function getMetadata(): ?EbookMetadata
public function getParser(): ?EbookParser
{
return $this->metadata;
return $this->parser;
}

/**
Expand Down Expand Up @@ -823,14 +843,14 @@ public function setPagesCount(?int $pagesCount): self
return $this;
}

public function setHasMetadata(bool $hasMetadata): self
public function setHasParser(bool $hasParser): self
{
$this->hasMetadata = $hasMetadata;
$this->hasParser = $hasParser;

return $this;
}

public function setExtra(mixed $value, string $key = null): self
public function setExtra(mixed $value, ?string $key = null): self
{
if (! $key) {
$this->extras[] = $value;
Expand Down Expand Up @@ -875,7 +895,7 @@ public function toArray(): array
'basename' => $this->basename,
'extension' => $this->extension,
'format' => $this->format,
'metadata' => $this->metadata?->toArray(),
'parser' => $this->parser?->toArray(),
'cover' => $this->cover?->toArray(),
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/EbookCover.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ protected function __construct(
) {
}

public static function make(string $path = null, string $contents = null): ?self
public static function make(?string $path = null, ?string $contents = null): ?self
{
if ($contents === null) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Formats/Audio/AudiobookModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function toEbook(): Ebook
'duration' => $audio->getDuration(),
]);

$this->ebook->setHasMetadata(true);
$this->ebook->setHasParser(true);

return $this->ebook;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Formats/Cba/CbaModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function make(Ebook $ebook): self
}

if ($self->type === 'cbam') {
$self->ebook->setHasMetadata(true);
$self->ebook->setHasParser(true);
$self->cbam = CbamTemplate::make($reader);
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public function toArray(): array
];
}

private function arrayableToBookAuthor(array $core, string $role = null): array
private function arrayableToBookAuthor(array $core, ?string $role = null): array
{
if (empty($core)) {
return [];
Expand Down Expand Up @@ -178,7 +178,7 @@ private function parseCbam(): Ebook
'comicMeta' => $comicMeta,
]);

$this->ebook->setHasMetadata(true);
$this->ebook->setHasParser(true);

return $this->ebook;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Formats/Djvu/DjvuModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@

use Kiwilan\Ebook\Ebook;
use Kiwilan\Ebook\EbookCover;
use Kiwilan\Ebook\Formats\Djvu\Parser\DjvuParser;
use Kiwilan\Ebook\Formats\EbookModule;

class DjvuModule extends EbookModule
{
protected ?DjvuParser $parser = null;

public static function make(Ebook $ebook): EbookModule
{
$self = new self($ebook);
$self->parser = DjvuParser::make($ebook);

return $self;
}

public function getParser(): ?DjvuParser
{
return $this->parser;
}

public function toEbook(): Ebook
{
return $this->ebook;
Expand Down
45 changes: 32 additions & 13 deletions src/Formats/EbookMetadata.php → src/Formats/EbookParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

use Kiwilan\Ebook\Formats\Audio\AudiobookModule;
use Kiwilan\Ebook\Formats\Cba\CbaModule;
use Kiwilan\Ebook\Formats\Djvu\DjvuModule;
use Kiwilan\Ebook\Formats\Epub\EpubModule;
use Kiwilan\Ebook\Formats\Fb2\Fb2Module;
use Kiwilan\Ebook\Formats\Mobi\MobiModule;
use Kiwilan\Ebook\Formats\Pdf\PdfModule;

class EbookMetadata
class EbookParser
{
protected function __construct(
protected EbookModule $module,
protected ?AudiobookModule $audiobook = null,
protected ?CbaModule $cba = null,
protected ?DjvuModule $djvu = null,
protected ?EpubModule $epub = null,
protected ?MobiModule $mobi = null,
protected ?Fb2Module $fb2 = null,
protected ?CbaModule $cba = null,
protected ?PdfModule $pdf = null,
protected ?AudiobookModule $audiobook = null,
protected ?string $type = null,
) {
}
Expand All @@ -27,6 +29,21 @@ public static function make(EbookModule $module): self
{
$self = new self($module);

if ($module instanceof AudiobookModule) {
$self->audiobook = $module;
$self->type = 'audiobook';
}

if ($module instanceof CbaModule) {
$self->cba = $module;
$self->type = 'cba';
}

if ($module instanceof DjvuModule) {
$self->djvu = $module;
$self->type = 'djvu';
}

if ($module instanceof EpubModule) {
$self->epub = $module;
$self->type = 'epub';
Expand All @@ -42,21 +59,11 @@ public static function make(EbookModule $module): self
$self->type = 'fb2';
}

if ($module instanceof CbaModule) {
$self->cba = $module;
$self->type = 'cba';
}

if ($module instanceof PdfModule) {
$self->pdf = $module;
$self->type = 'pdf';
}

if ($module instanceof AudiobookModule) {
$self->audiobook = $module;
$self->type = 'audiobook';
}

return $self;
}

Expand All @@ -75,6 +82,11 @@ public function getCba(): ?CbaModule
return $this->cba;
}

public function getDjvu(): ?DjvuModule
{
return $this->djvu;
}

public function getEpub(): ?EpubModule
{
return $this->epub;
Expand All @@ -100,6 +112,11 @@ public function getType(): ?string
return $this->type;
}

public function isDjvu(): bool
{
return $this->djvu !== null;
}

public function isEpub(): bool
{
return $this->epub !== null;
Expand Down Expand Up @@ -134,6 +151,8 @@ public function toArray(): array
{
return [
'epub' => $this->epub?->toArray(),
'fb2' => $this->fb2?->toArray(),
'djvu' => $this->djvu?->toArray(),
'mobi' => $this->mobi?->toArray(),
'cba' => $this->cba?->toArray(),
'pdf' => $this->pdf?->toArray(),
Expand Down
2 changes: 1 addition & 1 deletion src/Formats/Epub/EpubModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function create(): self
return $this;
}

$this->ebook->setHasMetadata(true);
$this->ebook->setHasParser(true);
$this->opf = OpfItem::make($xml, $this->ebook->getFilename());
$this->coverPath = $this->opf->getCoverPath();
$this->files = $this->opf->getContentFiles();
Expand Down
2 changes: 1 addition & 1 deletion src/Formats/Pdf/PdfModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function toEbook(): Ebook
$this->ebook->setTags($this->meta?->getKeywords());
$this->ebook->setPublishDate($this->meta?->getCreationDate());

$this->ebook->setHasMetadata(true);
$this->ebook->setHasParser(true);

return $this->ebook;
}
Expand Down
Loading

0 comments on commit 4e8d0e4

Please sign in to comment.