Skip to content

Commit 7e3edaa

Browse files
Refactor EpubChapter methods and update tests to use new getters
1 parent 3f347c4 commit 7e3edaa

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/Formats/Epub/Parser/EpubChapter.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,49 @@ private static function findByFilename(array $html, string $filename)
5555
return false;
5656
}
5757

58+
/**
59+
* Get the chapter label, e.g. `Chapter 1`
60+
*/
61+
public function getLabel(): string
62+
{
63+
return $this->label;
64+
}
65+
66+
/**
67+
* Get the source file name, e.g. `OEBPS/epub_c01_r1.htm`
68+
*/
69+
public function getSource(): string
70+
{
71+
return $this->source;
72+
}
73+
74+
/**
75+
* Get the chapter content, e.g. `<h1>Chapter 1</h1><p>Content...</p>`
76+
*/
77+
public function getContent(): string
78+
{
79+
return $this->content;
80+
}
81+
82+
/**
83+
* @deprecated Use `getLabel()` instead.
84+
*/
5885
public function label(): string
5986
{
6087
return $this->label;
6188
}
6289

90+
/**
91+
* @deprecated Use `getSource()` instead.
92+
*/
6393
public function source(): string
6494
{
6595
return $this->source;
6696
}
6797

98+
/**
99+
* @deprecated Use `getContent()` instead.
100+
*/
68101
public function content(): string
69102
{
70103
return $this->content;

tests/EpubChaptersTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
expect($chapters)->toBeArray();
1111
expect($chapters)->toHaveCount(41);
1212
expect($chapters[0])->toBeInstanceOf(EpubChapter::class);
13-
expect($chapters[0]->label())->toBe('Cover');
14-
expect($chapters[0]->source())->toBe('titlepage.xhtml');
15-
expect($chapters[0]->content())->toBeString();
13+
expect($chapters[0]->getLabel())->toBe('Cover');
14+
expect($chapters[0]->getSource())->toBe('titlepage.xhtml');
15+
expect($chapters[0]->getContent())->toBeString();
1616
});

tests/EpubTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Kiwilan\Ebook\Ebook;
4+
use Kiwilan\Ebook\Formats\Epub\EpubModule;
45
use Kiwilan\Ebook\Formats\Epub\Parser\EpubChapter;
56
use Kiwilan\Ebook\Formats\Epub\Parser\EpubContainer;
67
use Kiwilan\Ebook\Formats\Epub\Parser\EpubHtml;
@@ -240,3 +241,18 @@
240241
expect($html)->toBeArray()
241242
->each(fn (Pest\Expectation $expectation) => expect($expectation->value)->toBeInstanceOf(EpubHtml::class));
242243
});
244+
245+
it('can get epub chapters', function () {
246+
$ebook = Ebook::read(EPUB);
247+
248+
$epub = $ebook->getParser()->getEpub();
249+
expect($epub)->toBeInstanceOf(EpubModule::class);
250+
$chapters = $epub->getChapters();
251+
expect($chapters)->toBeArray()
252+
->each(fn (Pest\Expectation $expectation) => expect($expectation->value)->toBeInstanceOf(EpubChapter::class));
253+
254+
$firstChapter = $chapters[0];
255+
expect($firstChapter->getLabel())->toBe('Cover');
256+
expect($firstChapter->getSource())->toBe('titlepage.xhtml');
257+
expect($firstChapter->getContent())->toBeString();
258+
});

0 commit comments

Comments
 (0)