Skip to content

Commit

Permalink
Merge pull request #19 from kiwilan/develop
Browse files Browse the repository at this point in the history
1.3.31
  • Loading branch information
ewilan-riviere authored Jun 22, 2023
2 parents bb7ccbc + cc84f05 commit 5f6cf5d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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.3.30",
"version": "1.3.31",
"keywords": [
"php",
"ebook",
Expand Down
24 changes: 22 additions & 2 deletions src/Ebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public function toXml(string $path): ?string
*/
public function title(): ?string
{

return $this->title;
}

Expand Down Expand Up @@ -284,8 +285,12 @@ public function authors(): array
/**
* Description of the book.
*/
public function description(): ?string
public function description(?int $limit = null): ?string
{
if ($limit) {
return $this->limitLength($this->description, $limit);
}

return $this->description;
}

Expand All @@ -294,6 +299,7 @@ public function description(): ?string
*/
public function publisher(): ?string
{

return $this->publisher;
}

Expand Down Expand Up @@ -338,6 +344,7 @@ public function tags(): array
*/
public function series(): ?string
{

return $this->series;
}

Expand All @@ -352,8 +359,12 @@ public function volume(): ?int
/**
* Copyright of the book.
*/
public function copyright(): ?string
public function copyright(?int $limit = null): ?string
{
if ($limit) {
return $this->limitLength($this->copyright, $limit);
}

return $this->copyright;
}

Expand Down Expand Up @@ -515,6 +526,15 @@ public function hasCover(): bool
return $this->cover !== null;
}

private function limitLength(string $string, int $length): string
{
if (mb_strlen($string) <= $length) {
return $string;
}

return mb_substr($string, 0, $length - 1).'';
}

public function setTitle(?string $title): self
{
$this->title = $title;
Expand Down
28 changes: 26 additions & 2 deletions src/Formats/Epub/NcxMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static function make(string $content): self
$self->docTitle = $docTitle;

$self->navPoints = $self->setNavPoints();

if (is_array($self->navPoints)) {
usort($self->navPoints, fn (NcxMetadataNavPoint $a, NcxMetadataNavPoint $b) => $a->playOrder() <=> $b->playOrder());
}
Expand All @@ -67,8 +68,10 @@ private function setHead(): ?array
}

$head = [];

foreach ($ncx['head']['meta'] as $item) {
$attributes = XmlReader::getAttributes($item) ?? null;

if (! $attributes) {
continue;
}
Expand All @@ -89,6 +92,7 @@ private function setNavPoints(): ?array
}

$navPoints = [];

foreach ($navPoint as $item) {
$navPoints[] = NcxMetadataNavPoint::make($item);
}
Expand Down Expand Up @@ -130,9 +134,9 @@ public function lang(): ?string
public function toArray(): array
{
return [
'head' => $this->head,
'head' => array_map(fn (NcxMetadataHead $item) => $item->toArray(), $this->head),
'docTitle' => $this->docTitle,
'navPoints' => $this->navPoints,
'navPoints' => array_map(fn (NcxMetadataNavPoint $item) => $item->toArray(), $this->navPoints),
'version' => $this->version,
'lang' => $this->lang,
];
Expand Down Expand Up @@ -175,6 +179,14 @@ public function content(): ?string
{
return $this->content;
}

public function toArray(): array
{
return [
'name' => $this->name,
'content' => $this->content,
];
}
}

class NcxMetadataNavPoint
Expand All @@ -196,6 +208,7 @@ public static function make(array $xml): self
$self->src = $xml['content']['@attributes']['src'] ?? null;

$attributes = $xml['@attributes'] ?? null;

if (! $attributes) {
return $self;
}
Expand Down Expand Up @@ -226,4 +239,15 @@ public function src(): ?string
{
return $this->src;
}

public function toArray(): array
{
return [
'id' => $this->id,
'playOrder' => $this->playOrder,
'label' => $this->label,
'src' => $this->src,
'class' => $this->class,
];
}
}
9 changes: 5 additions & 4 deletions src/Formats/Epub/OpfMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,18 +541,19 @@ public function toArray(): array
return [
'epubVersion' => $this->epubVersion,
'dcTitle' => $this->dcTitle,
'dcCreators' => $this->dcCreators,
'dcContributors' => $this->dcContributors,
'dcCreators' => array_map(fn (BookAuthor $creator) => $creator->toArray(), $this->dcCreators),
'dcContributors' => array_map(fn (BookContributor $contributor) => $contributor->toArray(), $this->dcContributors),
'dcDescription' => $this->dcDescription,
'dcPublisher' => $this->dcPublisher,
'dcIdentifiers' => $this->dcIdentifiers,
'dcIdentifiers' => array_map(fn (BookIdentifier $identifier) => $identifier->toArray(), $this->dcIdentifiers),
'dcDate' => $this->dcDate,
'dcSubject' => $this->dcSubject,
'dcLanguage' => $this->dcLanguage,
'meta' => $this->meta,
'meta' => array_map(fn (BookMeta $meta) => $meta->toArray(), $this->meta),
'coverPath' => $this->coverPath,
'dcRights' => $this->dcRights,
'contentFiles' => $this->contentFiles,
'raw' => $this->xml->toArray(),
];
}

Expand Down

0 comments on commit 5f6cf5d

Please sign in to comment.