diff --git a/composer.json b/composer.json index 1d1cff3..1de6224 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, comics and audiobooks.", - "version": "2.3.6", + "version": "2.3.7", "keywords": [ "php", "ebook", diff --git a/src/Ebook.php b/src/Ebook.php index 76f9479..fc97a2d 100755 --- a/src/Ebook.php +++ b/src/Ebook.php @@ -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, '.', ''); @@ -739,7 +739,7 @@ public function setMetaTitle(Ebook $ebook): self return $this; } - $this->metaTitle = MetaTitle::make($ebook); + $this->metaTitle = MetaTitle::fromEbook($ebook); return $this; } diff --git a/src/Models/MetaTitle.php b/src/Models/MetaTitle.php index dc055e0..234431a 100644 --- a/src/Models/MetaTitle.php +++ b/src/Models/MetaTitle.php @@ -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, @@ -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, @@ -345,7 +386,7 @@ private function parse(Ebook $ebook): static } $this->slugSimple = $this->generateSlug([$title]); - if (! $ebook->getSeries()) { + if (! $this->series) { return $this; } diff --git a/tests/MetaTitleTest.php b/tests/MetaTitleTest.php index 945c2ce..09b39c8 100644 --- a/tests/MetaTitleTest.php +++ b/tests/MetaTitleTest.php @@ -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'); @@ -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'); @@ -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'); });