diff --git a/composer.json b/composer.json index 98a18e5..8c4ddec 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "kiwilan/php-ebook", "description": "PHP package to read metadata and extract covers from eBooks (.epub, .cbz, .cbr, .cb7, .cbt, .pdf).", - "version": "0.31.0", + "version": "0.32.0", "keywords": [ "php", "ebook", diff --git a/src/Ebook.php b/src/Ebook.php index e7865b2..8161efa 100755 --- a/src/Ebook.php +++ b/src/Ebook.php @@ -70,7 +70,7 @@ private function epub(): self if (! $opf) { return $this; } - $opf = OpfMetadata::make($opf); + $opf = OpfMetadata::make($opf, $this->filename); $this->metadata = $opf; $this->book = $opf->toBook(); diff --git a/src/Epub/OpfMetadata.php b/src/Epub/OpfMetadata.php index 123b0a0..19a7e29 100644 --- a/src/Epub/OpfMetadata.php +++ b/src/Epub/OpfMetadata.php @@ -23,6 +23,8 @@ class OpfMetadata protected ?int $epubVersion = null; + protected ?string $filename = null; + protected ?string $dcTitle = null; /** @var BookCreator[] */ @@ -59,7 +61,7 @@ protected function __construct( ) { } - public static function make(string $content): self + public static function make(string $content, string $filename): self { $xml = XmlReader::toArray($content); $self = new self(); @@ -69,6 +71,7 @@ public static function make(string $content): self $self->manifest = $xml['manifest'] ?? []; $self->spine = $xml['spine'] ?? []; $self->guide = $xml['guide'] ?? []; + $self->filename = $filename; $self->parseMetadata(); $self->coverPath = $self->findCover(); @@ -81,34 +84,20 @@ public function toBook(): BookEntity { $book = BookEntity::make(); - $book->setTitle($this->dcTitle); + $altTitle = explode('.', $this->filename); + $altTitle = $altTitle[0] ?? 'untitled'; + $book->setTitle($this->dcTitle ?? $altTitle); $authors = array_values($this->dcCreators); $book->setAuthorFirst($authors[0] ?? null); $book->setAuthors($authors); - $book->setDescription(strip_tags($this->dcDescription)); + if ($this->dcDescription) { + $book->setDescription(strip_tags($this->dcDescription)); + } $book->setContributor(! empty($this->dcContributors) ? implode(', ', $this->dcContributors) : null); $book->setRights(! empty($this->dcRights) ? implode(', ', $this->dcRights) : null); $book->setPublisher($this->dcPublisher); $book->setIdentifiers($this->dcIdentifiers); - - if (! empty($this->dcIdentifiers)) { - foreach ($this->dcIdentifiers as $identifier) { - // if ($identifier->type() === 'google') { - // $this->identifierGoogle = $identifier->content(); - // } - // if ($identifier->type() === 'amazon') { - // $this->identifierAmazon = $identifier->content(); - // } - // if ($identifier->type() === 'isbn10') { - // $this->identifierIsbn10 = $identifier->content(); - // } - // if ($identifier->type() === 'isbn13') { - // $this->identifierIsbn13 = $identifier->content(); - // } - } - } - $book->setDate($this->dcDate); $book->setLanguage($this->dcLanguage); diff --git a/tests/CbaTest.php b/tests/CbaTest.php index 14ab88a..fc3e53c 100644 --- a/tests/CbaTest.php +++ b/tests/CbaTest.php @@ -143,7 +143,6 @@ expect($metadata->alternateSeries())->toBe('Empyre'); expect($metadata->seriesGroup())->toBe('Fantastic Four'); - dump($metadata->ageRating()); expect($metadata->ageRating())->toBe(AgeRatingEnum::TEEN); expect($metadata->manga())->toBe(MangaEnum::NO); expect($metadata->pageCount())->toBe(24); diff --git a/tests/EpubOpfTest.php b/tests/EpubOpfTest.php index 0746e4e..772d019 100644 --- a/tests/EpubOpfTest.php +++ b/tests/EpubOpfTest.php @@ -25,7 +25,7 @@ }); it('can parse epub opf', function (string $path) { - $opf = OpfMetadata::make(file_get_contents($path)); + $opf = OpfMetadata::make(file_get_contents($path), $path); expect($opf)->tobeInstanceOf(OpfMetadata::class); expect($path)->toBeReadableFile(); @@ -44,7 +44,7 @@ })->with([EPUB_OPF_EPUB2, EPUB_OPF_EPUB3]); it('can parse epub opf alt', function () { - $opf = OpfMetadata::make(file_get_contents(EPUB_OPF_EPUB3_ALT)); + $opf = OpfMetadata::make(file_get_contents(EPUB_OPF_EPUB3_ALT), EPUB_OPF_EPUB3_ALT); expect($opf->metadata())->toBeArray(); expect($opf->manifest())->toBeArray(); diff --git a/tests/EpubTest.php b/tests/EpubTest.php index 7a52472..bbc14a1 100644 --- a/tests/EpubTest.php +++ b/tests/EpubTest.php @@ -65,3 +65,9 @@ expect($meta->toArray())->toBeArray(); expect($meta->__toString())->toBeString(); }); + +it('can extract alt metadata', function () { + $ebook = Ebook::read(EPUB_NO_META); + + expect($ebook->book()->title())->toBe('epub-no-meta'); +}); diff --git a/tests/Pest.php b/tests/Pest.php index 5c2c4cb..c94aa7a 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -24,6 +24,7 @@ define('EPUB_OPF_EPUB3_ALT', __DIR__.'/media/opf-epub3-alt.opf'); define('EPUB', __DIR__.'/media/epub.epub'); +define('EPUB_NO_META', __DIR__.'/media/epub-no-meta.epub'); define('EPUB_MULTIPLE_CREATORS', __DIR__.'/media/epub-multiple-creators.epub'); define('EPUB_BAD_MULTIPLE_CREATORS', __DIR__.'/media/epub-bad-multiple-creators.epub'); define('BOOKS_ITEMS', [ diff --git a/tests/media/epub-no-meta.epub b/tests/media/epub-no-meta.epub new file mode 100644 index 0000000..d459be1 Binary files /dev/null and b/tests/media/epub-no-meta.epub differ