Skip to content

Commit

Permalink
Merge pull request #68 from kiwilan/develop
Browse files Browse the repository at this point in the history
v2.3.7
  • Loading branch information
ewilan-riviere authored Feb 5, 2024
2 parents f928d21 + 361b850 commit 1eed732
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 22 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.6",
"version": "2.3.7",
"keywords": [
"php",
"ebook",
Expand Down
4 changes: 2 additions & 2 deletions src/Ebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static function read(string $path): ?self
$self->parser = EbookParser::make($format);
$self->convertEbook();
$self->cover = $self->parser->getModule()->toCover();
$self->metaTitle = MetaTitle::make($self);
$self->metaTitle = MetaTitle::fromEbook($self);

$time = microtime(true) - $start;
$self->execTime = (float) number_format((float) $time, 5, '.', '');
Expand Down Expand Up @@ -739,7 +739,7 @@ public function setMetaTitle(Ebook $ebook): self
return $this;
}

$this->metaTitle = MetaTitle::make($ebook);
$this->metaTitle = MetaTitle::fromEbook($ebook);

return $this;
}
Expand Down
73 changes: 57 additions & 16 deletions src/Models/MetaTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ class MetaTitle
];

protected function __construct(
protected ?string $title = null,
protected ?string $language = null,
protected ?string $series = null,
protected ?string $volume = null,
protected ?string $author = null,
protected ?string $year = null,
protected ?string $extension = null,

protected ?string $slug = null,
protected ?string $slugSimple = null,
protected ?string $seriesSlug = null,
Expand All @@ -292,39 +300,72 @@ protected function __construct(
}

/**
* Create a new MetaTitle instance.
* Create a new MetaTitle instance from an Ebook.
*/
public static function make(Ebook $ebook): ?self
public static function fromEbook(Ebook $ebook): ?self
{
if (! $ebook->getTitle()) {
return null;
}

$self = new self();
$self = new self(
title: $ebook->getTitle(),
language: $ebook->getLanguage(),
series: $ebook->getSeries(),
volume: (string) $ebook->getVolume(),
author: $ebook->getAuthorMain()?->getName(),
year: $ebook->getPublishDate()?->format('Y'),
extension: $ebook->getExtension(),
);
$self->parse();

return $self;
}

$self->parse($ebook);
/**
* Create a new MetaTitle instance from data.
*/
public static function fromData(
string $title,
?string $language = null,
?string $series = null,
string|int|null $volume = null,
?string $author = null,
string|int|null $year = null,
?string $extension = null,
): self {
$self = new self(
title: $title,
language: $language,
series: $series,
volume: (string) $volume,
author: $author,
year: (string) $year,
extension: $extension,
);
$self->parse();

return $self;
}

private function parse(Ebook $ebook): static
private function parse(): static
{
$title = $this->generateSlug($ebook->getTitle());
$language = $ebook->getLanguage() ? $this->generateSlug($ebook->getLanguage()) : null;
$series = $ebook->getSeries() ? $this->generateSlug($ebook->getSeries()) : null;
$volume = $ebook->getVolume() ? str_pad((string) $ebook->getVolume(), 2, '0', STR_PAD_LEFT) : null;
$author = $ebook->getAuthorMain()?->getName() ? $this->generateSlug($ebook->getAuthorMain()->getName()) : null;
$year = $ebook->getPublishDate()?->format('Y') ? $this->generateSlug($ebook->getPublishDate()->format('Y')) : null;
$extension = strtolower($ebook->getExtension());
$title = $this->generateSlug($this->title);
$language = $this->language ? $this->generateSlug($this->language) : null;
$series = $this->series ? $this->generateSlug($this->series) : null;
$volume = $this->volume ? str_pad((string) $this->volume, 2, '0', STR_PAD_LEFT) : null;
$author = $this->author ? $this->generateSlug($this->author) : null;
$year = $this->year ? $this->generateSlug($this->year) : null;
$extension = strtolower($this->extension);

$titleDeterminer = $this->removeDeterminers($ebook->getTitle(), $ebook->getLanguage());
$seriesDeterminer = $this->removeDeterminers($ebook->getSeries(), $ebook->getLanguage());
$titleDeterminer = $this->removeDeterminers($this->title, $this->language);
$seriesDeterminer = $this->removeDeterminers($this->series, $this->language);

if (! $title) {
return $this;
}

if ($ebook->getSeries()) {
if ($this->series) {
$this->slug = $this->generateSlug([
$seriesDeterminer,
$language,
Expand All @@ -345,7 +386,7 @@ private function parse(Ebook $ebook): static
}
$this->slugSimple = $this->generateSlug([$title]);

if (! $ebook->getSeries()) {
if (! $this->series) {
return $this;
}

Expand Down
21 changes: 18 additions & 3 deletions tests/MetaTitleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$ebook->setSeries('A comme Association');
$ebook->setLanguage('fr');
$ebook->setAuthorMain(new BookAuthor('Pierre Bottero'));
$meta = MetaTitle::make($ebook);
$meta = MetaTitle::fromEbook($ebook);

expect($meta->getSlug())->toBe('a-comme-association-fr-01-pale-lumiere-des-tenebres-pierre-bottero-1980-epub');
expect($meta->getSlugSimple())->toBe('la-pale-lumiere-des-tenebres');
Expand All @@ -24,7 +24,7 @@
$ebook->setSeries('The Lord of the Rings');
$ebook->setLanguage('en');
$ebook->setAuthorMain(new BookAuthor('J. R. R. Tolkien'));
$meta = MetaTitle::make($ebook);
$meta = MetaTitle::fromEbook($ebook);

expect($meta->getSlug())->toBe('lord-of-the-rings-en-01-fellowship-of-the-ring-j-r-r-tolkien-1980-epub');
expect($meta->getSlugSimple())->toBe('the-fellowship-of-the-ring');
Expand All @@ -36,10 +36,25 @@
$ebook->setSeries(null);
$ebook->setLanguage('en');
$ebook->setAuthorMain(new BookAuthor('Andy Weir'));
$meta = MetaTitle::make($ebook);
$meta = MetaTitle::fromEbook($ebook);

expect($meta->getSlug())->toBe('artemis-en-andy-weir-1980-epub');
expect($meta->getSlugSimple())->toBe('artemis');
expect($meta->getSeriesSlug())->toBeNull();
expect($meta->getSeriesSlugSimple())->toBeNull();

$meta = MetaTitle::fromData(
title: 'The Fellowship of the Ring',
volume: 1,
series: 'The Lord of the Rings',
language: 'en',
author: 'J. R. R. Tolkien',
year: 1980,
extension: 'epub',
);

expect($meta->getSlug())->toBe('lord-of-the-rings-en-01-fellowship-of-the-ring-j-r-r-tolkien-1980-epub');
expect($meta->getSlugSimple())->toBe('the-fellowship-of-the-ring');
expect($meta->getSeriesSlug())->toBe('lord-of-the-rings-en-j-r-r-tolkien-epub');
expect($meta->getSeriesSlugSimple())->toBe('the-lord-of-the-rings');
});

0 comments on commit 1eed732

Please sign in to comment.