Skip to content

Commit 361b850

Browse files
v2.3.7
`MetaTitle::class`: add `fromData()` static method to generate a `MetaTitle` object from a raw data and rename `make()` to `fromEbook()`.
1 parent 0e8c55d commit 361b850

File tree

4 files changed

+78
-22
lines changed

4 files changed

+78
-22
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "kiwilan/php-ebook",
33
"description": "PHP package to read metadata and extract covers from eBooks, comics and audiobooks.",
4-
"version": "2.3.6",
4+
"version": "2.3.7",
55
"keywords": [
66
"php",
77
"ebook",

src/Ebook.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static function read(string $path): ?self
113113
$self->parser = EbookParser::make($format);
114114
$self->convertEbook();
115115
$self->cover = $self->parser->getModule()->toCover();
116-
$self->metaTitle = MetaTitle::make($self);
116+
$self->metaTitle = MetaTitle::fromEbook($self);
117117

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

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

744744
return $this;
745745
}

src/Models/MetaTitle.php

+57-16
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ class MetaTitle
284284
];
285285

286286
protected function __construct(
287+
protected ?string $title = null,
288+
protected ?string $language = null,
289+
protected ?string $series = null,
290+
protected ?string $volume = null,
291+
protected ?string $author = null,
292+
protected ?string $year = null,
293+
protected ?string $extension = null,
294+
287295
protected ?string $slug = null,
288296
protected ?string $slugSimple = null,
289297
protected ?string $seriesSlug = null,
@@ -292,39 +300,72 @@ protected function __construct(
292300
}
293301

294302
/**
295-
* Create a new MetaTitle instance.
303+
* Create a new MetaTitle instance from an Ebook.
296304
*/
297-
public static function make(Ebook $ebook): ?self
305+
public static function fromEbook(Ebook $ebook): ?self
298306
{
299307
if (! $ebook->getTitle()) {
300308
return null;
301309
}
302310

303-
$self = new self();
311+
$self = new self(
312+
title: $ebook->getTitle(),
313+
language: $ebook->getLanguage(),
314+
series: $ebook->getSeries(),
315+
volume: (string) $ebook->getVolume(),
316+
author: $ebook->getAuthorMain()?->getName(),
317+
year: $ebook->getPublishDate()?->format('Y'),
318+
extension: $ebook->getExtension(),
319+
);
320+
$self->parse();
321+
322+
return $self;
323+
}
304324

305-
$self->parse($ebook);
325+
/**
326+
* Create a new MetaTitle instance from data.
327+
*/
328+
public static function fromData(
329+
string $title,
330+
?string $language = null,
331+
?string $series = null,
332+
string|int|null $volume = null,
333+
?string $author = null,
334+
string|int|null $year = null,
335+
?string $extension = null,
336+
): self {
337+
$self = new self(
338+
title: $title,
339+
language: $language,
340+
series: $series,
341+
volume: (string) $volume,
342+
author: $author,
343+
year: (string) $year,
344+
extension: $extension,
345+
);
346+
$self->parse();
306347

307348
return $self;
308349
}
309350

310-
private function parse(Ebook $ebook): static
351+
private function parse(): static
311352
{
312-
$title = $this->generateSlug($ebook->getTitle());
313-
$language = $ebook->getLanguage() ? $this->generateSlug($ebook->getLanguage()) : null;
314-
$series = $ebook->getSeries() ? $this->generateSlug($ebook->getSeries()) : null;
315-
$volume = $ebook->getVolume() ? str_pad((string) $ebook->getVolume(), 2, '0', STR_PAD_LEFT) : null;
316-
$author = $ebook->getAuthorMain()?->getName() ? $this->generateSlug($ebook->getAuthorMain()->getName()) : null;
317-
$year = $ebook->getPublishDate()?->format('Y') ? $this->generateSlug($ebook->getPublishDate()->format('Y')) : null;
318-
$extension = strtolower($ebook->getExtension());
353+
$title = $this->generateSlug($this->title);
354+
$language = $this->language ? $this->generateSlug($this->language) : null;
355+
$series = $this->series ? $this->generateSlug($this->series) : null;
356+
$volume = $this->volume ? str_pad((string) $this->volume, 2, '0', STR_PAD_LEFT) : null;
357+
$author = $this->author ? $this->generateSlug($this->author) : null;
358+
$year = $this->year ? $this->generateSlug($this->year) : null;
359+
$extension = strtolower($this->extension);
319360

320-
$titleDeterminer = $this->removeDeterminers($ebook->getTitle(), $ebook->getLanguage());
321-
$seriesDeterminer = $this->removeDeterminers($ebook->getSeries(), $ebook->getLanguage());
361+
$titleDeterminer = $this->removeDeterminers($this->title, $this->language);
362+
$seriesDeterminer = $this->removeDeterminers($this->series, $this->language);
322363

323364
if (! $title) {
324365
return $this;
325366
}
326367

327-
if ($ebook->getSeries()) {
368+
if ($this->series) {
328369
$this->slug = $this->generateSlug([
329370
$seriesDeterminer,
330371
$language,
@@ -345,7 +386,7 @@ private function parse(Ebook $ebook): static
345386
}
346387
$this->slugSimple = $this->generateSlug([$title]);
347388

348-
if (! $ebook->getSeries()) {
389+
if (! $this->series) {
349390
return $this;
350391
}
351392

tests/MetaTitleTest.php

+18-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
$ebook->setSeries('A comme Association');
1313
$ebook->setLanguage('fr');
1414
$ebook->setAuthorMain(new BookAuthor('Pierre Bottero'));
15-
$meta = MetaTitle::make($ebook);
15+
$meta = MetaTitle::fromEbook($ebook);
1616

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

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

4141
expect($meta->getSlug())->toBe('artemis-en-andy-weir-1980-epub');
4242
expect($meta->getSlugSimple())->toBe('artemis');
4343
expect($meta->getSeriesSlug())->toBeNull();
4444
expect($meta->getSeriesSlugSimple())->toBeNull();
45+
46+
$meta = MetaTitle::fromData(
47+
title: 'The Fellowship of the Ring',
48+
volume: 1,
49+
series: 'The Lord of the Rings',
50+
language: 'en',
51+
author: 'J. R. R. Tolkien',
52+
year: 1980,
53+
extension: 'epub',
54+
);
55+
56+
expect($meta->getSlug())->toBe('lord-of-the-rings-en-01-fellowship-of-the-ring-j-r-r-tolkien-1980-epub');
57+
expect($meta->getSlugSimple())->toBe('the-fellowship-of-the-ring');
58+
expect($meta->getSeriesSlug())->toBe('lord-of-the-rings-en-j-r-r-tolkien-epub');
59+
expect($meta->getSeriesSlugSimple())->toBe('the-lord-of-the-rings');
4560
});

0 commit comments

Comments
 (0)