Skip to content

Commit

Permalink
Merge pull request #69 from kiwilan/develop
Browse files Browse the repository at this point in the history
v2.3.8
  • Loading branch information
ewilan-riviere authored Mar 6, 2024
2 parents 827384e + 8d6fe15 commit 946f750
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 7 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, comics and audiobooks.",
"version": "2.3.7",
"version": "2.3.8",
"keywords": [
"php",
"ebook",
Expand Down
25 changes: 25 additions & 0 deletions src/Ebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public static function read(string $path): ?self
$self->convertEbook();
$self->cover = $self->parser->getModule()->toCover();
$self->metaTitle = MetaTitle::fromEbook($self);
$self->clean();

$time = microtime(true) - $start;
$self->execTime = (float) number_format((float) $time, 5, '.', '');
Expand Down Expand Up @@ -255,6 +256,30 @@ private function pdf(): EbookModule
return PdfModule::make($this);
}

private function clean(): self
{
$authors = [];
foreach ($this->authors as $author) {
if (! $author->getName()) {
continue;
}

$authors[] = $author;
}

$this->authors = $authors;

if ($this->authorMain && ! $this->authorMain->getName()) {
$this->authorMain = null;

if (count($this->authors) > 0) {
$this->authorMain = reset($this->authors);
}
}

return $this;
}

private function convertEbook(): self
{
$ebook = $this->parser->getModule()->toEbook();
Expand Down
4 changes: 2 additions & 2 deletions src/Formats/Epub/EpubModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public function toEbook(): Ebook
$this->ebook->setTags($tags);

$rating = null;
if (! empty($this->opf->getMeta())) {
foreach ($this->opf->getMeta() as $meta) {
if (! empty($this->opf->getMetaItems())) {
foreach ($this->opf->getMetaItems() as $meta) {
if ($meta->getName() === 'calibre:series') {
$this->ebook->setSeries($meta->getContents());
}
Expand Down
24 changes: 23 additions & 1 deletion src/Formats/Epub/Parser/OpfItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,33 @@ public function getDcLanguage(): ?string
}

/**
* @deprecated Use `getMetaItems` instead.
*
* @return BookMeta[]
*/
public function getMeta(): array
{
return $this->meta;
return $this->getMetaItems();
}

/**
* @return BookMeta[]
*/
public function getMetaItems(): array
{
$items = [];
foreach ($this->meta as $item) {
$items[$item->getName()] = $item;
}

return $items;
}

public function getMetaItem(string $key): ?BookMeta
{
$meta = array_filter($this->meta, fn (BookMeta $item) => $item->getName() === $key);

return array_shift($meta);
}

public function getCoverPath(): ?string
Expand Down
52 changes: 49 additions & 3 deletions tests/OpfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Kiwilan\Ebook\Formats\Epub\Parser\EpubContainer;
use Kiwilan\Ebook\Formats\Epub\Parser\OpfItem;
use Kiwilan\Ebook\Models\BookMeta;
use Kiwilan\XmlReader\XmlReader;

it('can parse epub container', function (string $path) {
Expand Down Expand Up @@ -38,11 +39,56 @@
expect($opf->getDcIdentifiers())->toBeArray();
expect($opf->getDcSubject())->toBeArray();
expect($opf->getDcLanguage())->toBeString();
expect($opf->getMeta())->toBeArray();
expect($opf->getMetaItems())->toBeArray();
expect($opf->getCoverPath())->toBeString();
expect($opf->getEpubVersion())->toBeGreaterThanOrEqual(2);
})->with([EPUB_OPF_EPUB2, EPUB_OPF_EPUB3, EPUB_OPF_INSURGENT, EPUB_OPF_LAGUERREETERNELLE, EPUB_OPF_EPEEETMORT, EPUB_OPF_NOT_FORMATTED]);

it('can parse epub opf meta items', function () {
$opf = OpfItem::make(file_get_contents(EPUB_OPF_EPUB2), EPUB_OPF_EPUB2);

$meta = $opf->getMetaItems();

// calibre:title_sort
// calibre:series
// calibre:series_index
// calibre:timestamp
// calibre:rating
// cover
// calibre:author_link_map

$title_sort = $opf->getMetaItem('calibre:title_sort');
$series = $opf->getMetaItem('calibre:series');
$series_index = $opf->getMetaItem('calibre:series_index');
$timestamp = $opf->getMetaItem('calibre:timestamp');
$rating = $opf->getMetaItem('calibre:rating');
$cover = $opf->getMetaItem('cover');
$author_link_map = $opf->getMetaItem('calibre:author_link_map');
$not_exist = $opf->getMetaItem('not_exist');

expect($meta)->toBeArray();
expect(get_class($title_sort))->toBe(BookMeta::class);
expect(get_class($series))->toBe(BookMeta::class);
expect(get_class($series_index))->toBe(BookMeta::class);
expect(get_class($timestamp))->toBe(BookMeta::class);
expect(get_class($rating))->toBe(BookMeta::class);
expect(get_class($cover))->toBe(BookMeta::class);
expect(get_class($author_link_map))->toBe(BookMeta::class);
expect($not_exist)->toBeNull();

expect($title_sort->getContents())->toBeString();
expect($series->getContents())->toBeString();
expect($series_index->getContents())->toBeString();
expect($timestamp->getContents())->toBeString();
expect($rating->getContents())->toBeString();
expect($cover->getContents())->toBeString();
expect($author_link_map->getContents())->toBeString();

expect($series->getContents())->toBe('Les Enfants de la Terre');
expect($series_index->getContents())->toBe('1.0');
expect($timestamp->getContents())->toBe('2023-03-25T10:32:21+00:00');
});

it('can parse epub opf alt', function () {
$opf = OpfItem::make(file_get_contents(EPUB_OPF_EPUB3_ALT), EPUB_OPF_EPUB3_ALT);

Expand All @@ -60,7 +106,7 @@
expect($opf->getDcDate())->toBeNull();
expect($opf->getDcSubject())->toBeArray();
expect($opf->getDcLanguage())->toBeString();
expect($opf->getMeta())->toBeArray();
expect($opf->getMetaItems())->toBeArray();
expect($opf->getCoverPath())->toBeString();
expect($opf->getEpubVersion())->toBeGreaterThanOrEqual(2);
});
Expand All @@ -79,7 +125,7 @@
expect($opf->getDcIdentifiers())->toBeArray();
expect($opf->getDcSubject())->toBeArray();
expect($opf->getDcLanguage())->toBeString();
expect($opf->getMeta())->toBeArray();
expect($opf->getMetaItems())->toBeArray();
expect($opf->getCoverPath())->toBeString();
expect($opf->getEpubVersion())->toBeGreaterThanOrEqual(2);
});
Expand Down

0 comments on commit 946f750

Please sign in to comment.