diff --git a/composer.json b/composer.json index a5e4e73..ae546f5 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) and audiobooks (.mp3, .m4a, .m4b, .flac, .ogg).", - "version": "1.3.0", + "version": "1.3.10", "keywords": [ "php", "ebook", diff --git a/src/Formats/Epub/OpfMetadata.php b/src/Formats/Epub/OpfMetadata.php index 29f248e..0b35495 100644 --- a/src/Formats/Epub/OpfMetadata.php +++ b/src/Formats/Epub/OpfMetadata.php @@ -329,6 +329,10 @@ private function setDcSubjects(): array $items = $core; } + if (array_key_exists('_value', $items)) { + $items = [$items]; + } + $temp = []; foreach ($items as $item) { $temp[] = $item['_value'] ?? null; @@ -391,7 +395,7 @@ private function setDcCreators(): array */ private function setDcContributors(): array { - $core = $this->metadata['dc:contributor'] ?? null; + $core = $this->metadata['dc:contributor']['_value'] ?? null; if (! $core) { return []; @@ -492,8 +496,12 @@ private function setMeta(): array return $items; } - private function multipleItems(array $items): array + private function multipleItems(mixed $items): array { + if (! is_array($items)) { + $items = [$items]; + } + $core = $items; // Check if subarrays exists $isMultiple = array_key_exists(0, $items); diff --git a/tests/EpubOpfTest.php b/tests/EpubOpfTest.php index a39bded..5336320 100644 --- a/tests/EpubOpfTest.php +++ b/tests/EpubOpfTest.php @@ -64,3 +64,22 @@ expect($opf->coverPath())->toBeString(); expect($opf->epubVersion())->toBeGreaterThanOrEqual(2); }); + +it('can parse epub opf without tags', function () { + $opf = OpfMetadata::make(file_get_contents(EPUB_OPF_EPUB2_NO_TAGS), EPUB_OPF_EPUB2_NO_TAGS); + + expect($opf)->tobeInstanceOf(OpfMetadata::class); + expect(EPUB_OPF_EPUB2_NO_TAGS)->toBeReadableFile(); + expect($opf->dcTitle())->toBeString(); + expect($opf->dcCreators())->toBeArray(); + expect($opf->dcDescription())->toBeString(); + expect($opf->dcContributors())->toBeArray(); + expect($opf->dcRights())->toBeArray(); + expect($opf->dcPublisher())->toBeString(); + expect($opf->dcIdentifiers())->toBeArray(); + expect($opf->dcSubject())->toBeArray(); + expect($opf->dcLanguage())->toBeString(); + expect($opf->meta())->toBeArray(); + expect($opf->coverPath())->toBeString(); + expect($opf->epubVersion())->toBeGreaterThanOrEqual(2); +}); diff --git a/tests/EpubTest.php b/tests/EpubTest.php index d3a98cb..fe1ecc4 100644 --- a/tests/EpubTest.php +++ b/tests/EpubTest.php @@ -148,3 +148,10 @@ expect($ebook->execTime())->toBeLessThan(0.06); })->skip(PHP_OS_FAMILY === 'Windows', 'Skip on Windows'); + +it('can parse epub without tags', function () { + $ebook = Ebook::read(EPUB_ONE_TAG); + + expect($ebook->tags())->toBeArray(); + expect($ebook->tags()[0])->toBeString(); +}); diff --git a/tests/Pest.php b/tests/Pest.php index 01db483..f950a5b 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -20,10 +20,12 @@ define('EPUB_CONTAINER_EPUB3', __DIR__.'/media/container-epub3.xml'); define('EPUB_OPF_EPUB2', __DIR__.'/media/opf-epub2.opf'); +define('EPUB_OPF_EPUB2_NO_TAGS', __DIR__.'/media/opf-epub2-no-tags.opf'); define('EPUB_OPF_EPUB3', __DIR__.'/media/opf-epub3.opf'); define('EPUB_OPF_EPUB3_ALT', __DIR__.'/media/opf-epub3-alt.opf'); define('EPUB', __DIR__.'/media/test-epub.epub'); +define('EPUB_ONE_TAG', __DIR__.'/media/epub-one-tag.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'); diff --git a/tests/media/epub-one-tag.epub b/tests/media/epub-one-tag.epub new file mode 100644 index 0000000..e3a4540 Binary files /dev/null and b/tests/media/epub-one-tag.epub differ diff --git a/tests/media/opf-epub2-no-tags.opf b/tests/media/opf-epub2-no-tags.opf new file mode 100644 index 0000000..878935b --- /dev/null +++ b/tests/media/opf-epub2-no-tags.opf @@ -0,0 +1,81 @@ + + + + Le clan de l'ours des cavernes + Jean M. Auel + calibre (6.12.0) [https://calibre-ebook.com] + <div> + <p>Quelque part en Europe, 35 000 ans avant notre ère. Petite fille Cro-Magnon de cinq + ans, Ayla est séparée de ses parents à la suite d'un violent tremblement de terre. Elle est + recueillie par le clan de l'ours des cavernes, une tribu Neandertal qui l'adopte, non sans + réticence, ayant reconnu en elle la représentante d'une autre espèce, plus évoluée. + <br><br>Iza, la guérisseuse, Brun, le chef et Creb, le magicien lui enseignent les + règles de la vie communautaire, leurs rites, leurs peurs, leurs audaces. Mais Ayla, la + fillette blonde aux yeux bleus les surprend par sa puissance de raisonnement qui lui permet de + s'adapter, de réagir rapidement et de ne pas être totalement dépendante de son environnement. + Une différence qui ne tarde pas à faire d'elle une menace pour tout le clan, et à attiser la + convoitise de Brud, le fils du chef...</p></div> + Presses de la cité + a2cf2f25-4de2-4f77-82cc-0198352b0851 + 1980-01-13T21:00:00+00:00 + Fiction + fr + a2cf2f25-4de2-4f77-82cc-0198352b0851 + 63CTHAAACAAJ + 9782266122122 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file