Skip to content

Commit

Permalink
Merge pull request #77 from kiwilan/develop
Browse files Browse the repository at this point in the history
v2.5.10
  • Loading branch information
ewilan-riviere committed May 26, 2024
2 parents e4f8a24 + d129ba9 commit a770c97
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 99 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.5.0",
"version": "2.5.10",
"keywords": [
"php",
"ebook",
Expand Down
62 changes: 6 additions & 56 deletions src/Formats/Audio/AudiobookModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ private function create(): self
$audio = $this->ebook->getAudio();

$authors = $audio->getArtist() ?? $audio->getAlbumArtist();
$genres = $this->parseGenres($audio->getGenre());

$genres = EbookUtils::parseStringWithSeperator($audio->getGenre());
$genres = array_map('ucfirst', $genres);

$series = $audio->getTag('series') ?? $audio->getTag('mvnm');
$series_part = $audio->getTag('series-part') ?? $audio->getTag('mvin');
$series_part = $this->parseTag($series_part);
Expand All @@ -51,12 +54,12 @@ private function create(): self
}

$this->audio = [
'authors' => $this->parseAuthors($authors),
'authors' => EbookUtils::parseStringWithSeperator($authors),
'title' => $audio->getAlbum() ?? $audio->getTitle(),
'subtitle' => $this->parseTag($audio->getTag('subtitle'), false),
'publisher' => $audio->getTag('encoded_by'),
'publish_year' => $audio->getYear(),
'narrators' => $this->parseAuthors($narrators),
'narrators' => EbookUtils::parseStringWithSeperator($narrators),
'description' => $this->parseTag($audio->getDescription(), false),
'lyrics' => $this->parseTag($audio->getLyrics()),
'comment' => $this->parseTag($audio->getComment()),
Expand Down Expand Up @@ -186,59 +189,6 @@ public function __toString(): string
return $this->toJson();
}

/**
* @return string[]
*/
private function parseGenres(?string $genres): array
{
if (! $genres) {
return [];
}

$items = [];
if (str_contains($genres, ';')) {
$items = explode(';', $genres);
} elseif (str_contains($genres, '/')) {
$items = explode('/', $genres);
} elseif (str_contains($genres, '//')) {
$items = explode('//', $genres);
} elseif (str_contains($genres, ',')) {
$items = explode(',', $genres);
} else {
$items = [$genres];
}

$items = array_map('trim', $items);
$items = array_map('ucfirst', $items);

return $items;
}

/**
* @return string[]
*/
private function parseAuthors(?string $authors): array
{
if (! $authors) {
return [];
}

$items = [];
if (str_contains($authors, ',')) {
$items = explode(',', $authors);
} elseif (str_contains($authors, ';')) {
$items = explode(';', $authors);
} elseif (str_contains($authors, '&')) {
$items = explode('&', $authors);
} elseif (str_contains($authors, 'and')) {
$items = explode('and', $authors);
} else {
$items = [$authors];
}

return array_map('trim', $items);
}

private function parseTag(?string $tag, bool $flat = true): ?string
{
if (! $tag) {
Expand Down
14 changes: 9 additions & 5 deletions src/Formats/Epub/Parser/OpfItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Kiwilan\Ebook\Models\BookContributor;
use Kiwilan\Ebook\Models\BookIdentifier;
use Kiwilan\Ebook\Models\BookMeta;
use Kiwilan\Ebook\Utils\EbookUtils;
use Kiwilan\XmlReader\XmlReader;

/**
Expand Down Expand Up @@ -64,7 +65,7 @@ protected function __construct(
) {
}

public static function make(string $content, string $filename): self
public static function make(string $content, ?string $filename = null): self
{
$xml = XmlReader::make($content);
$self = new self($xml);
Expand Down Expand Up @@ -364,6 +365,8 @@ private function setDcSubjects(): array
$items = $core;
}

$items = EbookUtils::parseStringWithSeperator($items);

return $items;
}

Expand Down Expand Up @@ -411,6 +414,10 @@ private function setDcCreators(): array
continue;
}
$attributes = XmlReader::parseAttributes($item);
// remove `\n` and `\r` from the name
$name = preg_replace('/\s+/', ' ', $name);
$name = trim($name);

$items[$name] = new BookAuthor(
name: $name,
role: $attributes['role'] ?? null,
Expand Down Expand Up @@ -549,10 +556,7 @@ private function multipleItems(mixed $items): array
$attr = XmlReader::parseAttributes($items);

// Check if bad multiple creators `Jean M. Auel, Philippe Rouard` exists
if (is_string($content) && str_contains($content, ',')) {
$content = explode(',', $content);
$content = array_map('trim', $content);
}
$content = EbookUtils::parseStringWithSeperator($content);

$temp = [];
// If bad multiple creators exists
Expand Down
7 changes: 7 additions & 0 deletions src/Formats/Mobi/MobiModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Kiwilan\Ebook\Formats\Mobi\Parser\MobiReader;
use Kiwilan\Ebook\Models\BookAuthor;
use Kiwilan\Ebook\Models\BookIdentifier;
use Kiwilan\Ebook\Utils\EbookUtils;

/**
* @docs https://stackoverflow.com/questions/11817047/php-library-to-parse-mobi
Expand All @@ -33,7 +34,13 @@ public function toEbook(): Ebook
return $this->ebook;
}

$authors = [];
foreach ($this->parser->get(MobiReader::AUTHOR_100, true) as $author) {
$authors[] = $author;
}

$authors = EbookUtils::parseStringWithSeperator($authors);
foreach ($authors as $author) {
$this->ebook->setAuthor(new BookAuthor($author));
}

Expand Down
16 changes: 4 additions & 12 deletions src/Formats/Pdf/PdfModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Kiwilan\Ebook\EbookCover;
use Kiwilan\Ebook\Formats\EbookModule;
use Kiwilan\Ebook\Models\BookAuthor;
use Kiwilan\Ebook\Utils\EbookUtils;

class PdfModule extends EbookModule
{
Expand All @@ -27,16 +28,7 @@ public function toEbook(): Ebook
$author = $this->meta?->getAuthor();

if ($author !== null) {
$authors = [];
if (str_contains($author, ',')) {
$authors = explode(',', $author);
} elseif (str_contains($author, '&')) {
$authors = explode(',', $author);
} elseif (str_contains($author, 'and')) {
$authors = explode(',', $author);
} else {
$authors[] = $author;
}
$authors = EbookUtils::parseStringWithSeperator($author);

$creators = [];
foreach ($authors as $author) {
Expand All @@ -49,9 +41,9 @@ public function toEbook(): Ebook
}
$this->ebook->setDescription($this->meta?->getSubject());
$this->ebook->setPublisher($this->meta?->getCreator());
$this->ebook->setTags($this->meta?->getKeywords());
$keywords = EbookUtils::parseStringWithSeperator($this->meta?->getKeywords());
$this->ebook->setTags($keywords);
$this->ebook->setPublishDate($this->meta?->getCreationDate());

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

return $this->ebook;
Expand Down
Loading

0 comments on commit a770c97

Please sign in to comment.