Skip to content

Commit a658733

Browse files
author
Kallef Alexandre
committed
several improvements, allowing to insert publisher, author and others
1 parent b746fcf commit a658733

File tree

1 file changed

+129
-13
lines changed

1 file changed

+129
-13
lines changed

src/Epub.php

Lines changed: 129 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class Epub
1919

2020
protected string $title;
2121

22-
protected string $language;
22+
protected string $language = 'en';
2323

24-
protected \DateTime $dcTermsModified;
24+
protected $dcTermsModified;
2525

2626
/**
2727
* @var NavegationMaker
@@ -43,11 +43,27 @@ class Epub
4343
*/
4444
protected array $chaptersFiles = [];
4545

46-
public function __construct(string $title, string $language = 'en', \DateTime $dcTermsModified = null)
46+
/**
47+
* The book's creator
48+
* @var string
49+
*/
50+
protected string $creator = '';
51+
52+
/**
53+
* The book's rights
54+
* @var string
55+
*/
56+
protected string $rights = '';
57+
58+
/**
59+
* This is the book's publisher
60+
* @var string
61+
*/
62+
protected string $publisher = '';
63+
64+
public function __construct(string $title)
4765
{
4866
$this->title = $title;
49-
$this->language = $language;
50-
$this->dcTermsModified = $dcTermsModified ?: new \DateTime('now');
5167
}
5268

5369
public function nav(string $basePath = 'EPUB/xhtml', string $filename = null): NavegationMaker
@@ -102,13 +118,28 @@ protected function generateEpub(): void
102118
$this->generateContainer();
103119
}
104120

105-
protected function getPackageMaker(): PackageMaker
121+
public function getPackage(): PackageMaker
106122
{
107123
if (null === $this->packageMaker) {
108124
$this->packageMaker = new PackageMaker($this->getTitle(), null, 'EPUB');
109125

110126
$this->packageMaker->createMetadataItem('dc:language', $this->language);
111-
$this->packageMaker->createMetadataItem('meta', $this->dcTermsModified->format('Y-m-d\TH:i:sp'), ['property' => 'dcterms:modified']);
127+
$this->packageMaker->createMetadataItem('meta', $this->getDcTermsModified()->format('Y-m-d\TH:i:sp'), ['property' => 'dcterms:modified']);
128+
129+
if ($this->getCreator()) {
130+
$this->packageMaker->createMetadataItem('dc:creator', $this->getCreator(), [
131+
'opf:file-as' => $this->getCreator(),
132+
'opf:role' => "aut",
133+
]);
134+
}
135+
136+
if ($this->getPublisher()) {
137+
$this->packageMaker->createMetadataItem('dc:publisher', $this->getPublisher());
138+
}
139+
140+
if ($this->getRights()) {
141+
$this->packageMaker->createMetadataItem('dc:rights', $this->getRights());
142+
}
112143

113144
$coverFile = $this->coverMaker->makeFile();
114145

@@ -130,7 +161,7 @@ protected function getPackageMaker(): PackageMaker
130161
);
131162

132163
$this->packageMaker->createMetadataItem('meta', null, [
133-
'name' => 'cover',
164+
'name' => 'cover',
134165
'contet' => $this->coverMaker->getImage()->getFilename(),
135166
]);
136167

@@ -140,7 +171,7 @@ protected function getPackageMaker(): PackageMaker
140171
$this->packageMaker->createSpineItemRef($manifestItem->getId());
141172
}
142173

143-
$manifestItem = ManifestItem::fromFile($this->navegation->makeFile(), 'EPUB', 'application/xhtml+xml', ['properties' => 'nav']);
174+
$manifestItem = ManifestItem::fromFile($this->nav()->makeFile(), 'EPUB', 'application/xhtml+xml', ['properties' => 'nav']);
144175
$this->packageMaker->appendManifestItem($manifestItem);
145176
}
146177

@@ -149,7 +180,7 @@ protected function getPackageMaker(): PackageMaker
149180

150181
protected function generateContainer(): void
151182
{
152-
$container = new ContainerMaker($this->getPackageMaker()->makeFile());
183+
$container = new ContainerMaker($this->getPackage()->makeFile());
153184
$container->makeFile();
154185
}
155186

@@ -175,10 +206,10 @@ public function save(string $path, string $filename = null): string
175206
return FileManager::getInstance()->compressAllTo($this->getFilename($filename), $path);
176207
}
177208

178-
public function download(string $filename = null)
209+
public function download(string $filename = null): void
179210
{
180211
$this->generateEpub();
181-
return FileManager::getInstance()->download($this->getFilename($filename));
212+
FileManager::getInstance()->download($this->getFilename($filename));
182213
}
183214

184215
/**
@@ -196,9 +227,94 @@ public function setTitle(string $title): void
196227
{
197228
$this->title = $title;
198229
}
199-
230+
200231
protected function getFilename(string $filename = null): string
201232
{
202233
return $filename ?: Str::slugify($this->getTitle()) . '.epub';
203234
}
235+
236+
/**
237+
* @return string
238+
*/
239+
public function getLanguage(): string
240+
{
241+
return $this->language;
242+
}
243+
244+
/**
245+
* @param string $language
246+
*/
247+
public function setLanguage(string $language): self
248+
{
249+
$this->language = $language;
250+
return $this;
251+
}
252+
253+
/**
254+
* @return \DateTime
255+
*/
256+
public function getDcTermsModified(): \DateTime
257+
{
258+
return $this->dcTermsModified ?: new \DateTime('now');
259+
}
260+
261+
/**
262+
* @param \DateTime $dcTermsModified
263+
*/
264+
public function setDcTermsModified(\DateTime $dcTermsModified): self
265+
{
266+
$this->dcTermsModified = $dcTermsModified;
267+
return $this;
268+
}
269+
270+
/**
271+
* @return string
272+
*/
273+
public function getCreator(): string
274+
{
275+
return $this->creator;
276+
}
277+
278+
/**
279+
* @param string $creator
280+
*/
281+
public function setCreator(string $creator): self
282+
{
283+
$this->creator = $creator;
284+
return $this;
285+
}
286+
287+
/**
288+
* @return string
289+
*/
290+
public function getRights(): string
291+
{
292+
return $this->rights;
293+
}
294+
295+
/**
296+
* @param string $rights
297+
*/
298+
public function setRights(string $rights): self
299+
{
300+
$this->rights = $rights;
301+
return $this;
302+
}
303+
304+
/**
305+
* @return string
306+
*/
307+
public function getPublisher(): string
308+
{
309+
return $this->publisher;
310+
}
311+
312+
/**
313+
* @param string $publisher
314+
*/
315+
public function setPublisher(string $publisher): self
316+
{
317+
$this->publisher = $publisher;
318+
return $this;
319+
}
204320
}

0 commit comments

Comments
 (0)