Skip to content

Commit

Permalink
0.32.0
Browse files Browse the repository at this point in the history
- Add no metadata case with title as filename
  • Loading branch information
ewilan-riviere committed May 25, 2023
1 parent 189b349 commit 10e8844
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion 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 (.epub, .cbz, .cbr, .cb7, .cbt, .pdf).",
"version": "0.31.0",
"version": "0.32.0",
"keywords": [
"php",
"ebook",
Expand Down
2 changes: 1 addition & 1 deletion src/Ebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
31 changes: 10 additions & 21 deletions src/Epub/OpfMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class OpfMetadata

protected ?int $epubVersion = null;

protected ?string $filename = null;

protected ?string $dcTitle = null;

/** @var BookCreator[] */
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);

Expand Down
1 change: 0 additions & 1 deletion tests/CbaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/EpubOpfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions tests/EpubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
1 change: 1 addition & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
Binary file added tests/media/epub-no-meta.epub
Binary file not shown.

0 comments on commit 10e8844

Please sign in to comment.