From 29304b0294c38824553f4de93d35b37ae7e5e1fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ewilan=20Rivi=C3=A8re?= Date: Thu, 15 Jun 2023 08:44:24 +0200 Subject: [PATCH] 1.0.12 - `MetaTitle` add `uniqueFilename` --- composer.json | 2 +- src/Tools/MetaTitle.php | 28 ++++++++++++++++++++++++++++ tests/MetaTitleTest.php | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d2eb370..7f1e710 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.0.11", + "version": "1.0.12", "keywords": [ "php", "ebook", diff --git a/src/Tools/MetaTitle.php b/src/Tools/MetaTitle.php index ff6ad90..f5710b8 100644 --- a/src/Tools/MetaTitle.php +++ b/src/Tools/MetaTitle.php @@ -20,6 +20,7 @@ protected function __construct( protected ?string $serieSlugLang = null, protected ?string $slugSortWithSerie = null, + protected ?string $uniqueFilename = null, ) { } @@ -74,6 +75,7 @@ private function setMetaTitle(Ebook $ebook): static $this->serieSlug = $this->setSlug($series); $this->serieSlugSort = $this->generateSortTitle($series, $language); $this->serieSlugLang = $this->generateSlug($series, $ebook->extension(), $language); + $this->uniqueFilename = $this->generateUniqueFilename($ebook); return $this; } @@ -135,6 +137,14 @@ public function slugSortWithSerie(): string return $this->slugSortWithSerie; } + /** + * Get unique filename, like `jean-m-auel-earths-children-01-clan-of-the-cave-bear-en`. + */ + public function uniqueFilename(): string + { + return $this->uniqueFilename; + } + /** * Try to get sort title. * Example: `collier-de-la-reine` from `Le Collier de la Reine`. @@ -196,6 +206,24 @@ private function generateSlug(string $title, ?string $type, ?string $language): return $this->setSlug($title.' '.$type.' '.$language); } + /** + * Generate unique filename. + */ + private function generateUniqueFilename(Ebook $ebook): string + { + $author = $this->setSlug($ebook->authorMain()); + $series = $this->setSlug($ebook->series()); + $volume = (string) $ebook->volume(); + $volume = $volume = strlen($volume) < 2 ? '0'.$volume : $volume; + $title = $this->setSlug($ebook->title()); + $language = $this->setSlug($ebook->language()); + + $filename = "{$author}-{$series}-{$volume}-{$title}-{$language}"; + $filename = $this->setSlug($filename); + + return $filename; + } + public function toArray(): array { return [ diff --git a/tests/MetaTitleTest.php b/tests/MetaTitleTest.php index 3d80a2e..2a46e93 100644 --- a/tests/MetaTitleTest.php +++ b/tests/MetaTitleTest.php @@ -19,4 +19,5 @@ expect($meta->serieSlugSort())->toBe('a-comme-association'); expect($meta->serieSlugLang())->toBe('a-comme-association-epub-fr'); expect($meta->slugSortWithSerie())->toBe('a-comme-association-01_pale-lumiere-des-tenebres'); + expect($meta->uniqueFilename())->toBe('jean-m-auel-a-comme-association-01-la-pale-lumiere-des-tenebres-fr'); });