Skip to content

Commit

Permalink
Merge pull request #14 from kiwilan/develop
Browse files Browse the repository at this point in the history
1.3.0
  • Loading branch information
ewilan-riviere committed Jun 19, 2023
2 parents 57069a1 + cddaf3f commit 1206e71
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 172 deletions.
16 changes: 8 additions & 8 deletions .github/ISSUE_TEMPLATE/bug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ body:
attributes:
label: Package Version
description: What version of our Package are you running? Please be as specific as possible
placeholder: 2.0.0
placeholder: 1.2.0
validations:
required: true
- type: input
Expand All @@ -42,13 +42,13 @@ body:
- type: dropdown
id: operating-systems
attributes:
label: Which operating systems does with happen with?
description: You may select more than one.
multiple: true
options:
- macOS
- Windows
- Linux
label: Which operating systems does with happen with?
description: You may select more than one.
multiple: true
options:
- macOS
- Windows
- Linux
- type: textarea
id: notes
attributes:
Expand Down
7 changes: 0 additions & 7 deletions .github/workflows/update-changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,3 @@ jobs:
branch: main
commit_message: Update CHANGELOG
file_pattern: CHANGELOG.md

# - name: Merge on develop
# run: |
# git checkout origin/develop
# git merge origin/main
# git push
# shell: bash
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[![tests][tests-src]][tests-href]
[![codecov][codecov-src]][codecov-href]

PHP package to read metadata and extract covers from eBooks (`.epub`, `.cbz`, `.cbr`, `.cb7`, `.cbt`, `.pdf`) and audiobooks (`mp3`, `.m4a`, `.m4b`, `.flac`, `.ogg`).
PHP package to read metadata and extract covers from eBooks (`.epub`, `.cbz`, `.cbr`, `.cb7`, `.cbt`, `.pdf`) and audiobooks (`.mp3`, `.m4a`, `.m4b`, `.flac`, `.ogg`).

_Supports Linux, macOS and Windows._

Expand Down Expand Up @@ -253,8 +253,7 @@ Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed re
## Credits

- [`spatie`](https://github.com/spatie) for `spatie/package-skeleton-php`
- [`kiwilan/php-archive`](https://github.com/kiwilan/php-archive)
- [`kiwilan/php-audio`](https://github.com/kiwilan/php-audio)
- [`kiwilan`](https://github.com/kiwilan) for `kiwilan/php-archive`, `kiwilan/php-audio`, `kiwilan/php-xml-reader`

## License

Expand Down
5 changes: 3 additions & 2 deletions 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.2.0",
"version": "1.3.0",
"keywords": [
"php",
"ebook",
Expand Down Expand Up @@ -35,7 +35,8 @@
"require": {
"php": "^8.1",
"kiwilan/php-archive": "^1.4.02",
"kiwilan/php-audio": "^2.0.0"
"kiwilan/php-audio": "^2.0.0",
"kiwilan/php-xml-reader": "^0.1.10"
},
"require-dev": {
"laravel/pint": "^1.7",
Expand Down
6 changes: 3 additions & 3 deletions src/Formats/Cba/CbaMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Kiwilan\Ebook\Formats\EbookModule;
use Kiwilan\Ebook\Tools\BookAuthor;
use Kiwilan\Ebook\Tools\ComicMeta;
use Kiwilan\Ebook\XmlReader;
use Kiwilan\XmlReader\XmlReader;

class CbaMetadata extends EbookModule
{
Expand All @@ -24,9 +24,9 @@ public static function make(Ebook $ebook): self
if (! $xml) {
return $self;
}
$metadata = XmlReader::toArray($xml);
$metadata = XmlReader::make($xml)->content();

$root = $metadata['@root'] ?? null;
$root = $metadata['@root']['tagName'] ?? null;
$self->type = match ($root) {
'ComicInfo' => 'cbam',
'ComicBook' => 'cbml',
Expand Down
5 changes: 5 additions & 0 deletions src/Formats/Cba/CbamMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ protected function __construct(
*/
public static function make(array $metadata): self
{
$metadata = $metadata['ComicInfo'] ?? $metadata;
$self = new self($metadata);
$self->parse();

Expand Down Expand Up @@ -211,6 +212,10 @@ private function extract(string $key): ?string
return null;
}

if (is_array($string)) {
$string = $string['_value'] ?? null;
}

return $this->normalizeString($string);
}

Expand Down
20 changes: 14 additions & 6 deletions src/Formats/Epub/EpubContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Kiwilan\Ebook\Formats\Epub;

use Kiwilan\Ebook\XmlReader;
use Kiwilan\XmlReader\XmlReader;

/**
* Transform `container.xml` file to an object.
Expand All @@ -20,7 +20,7 @@ protected function __construct(

public static function make(string $content): self
{
$xml = XmlReader::toArray($content);
$xml = XmlReader::make($content)->content();

$self = new self($xml);
$self->opfPath = $self->parseOpfPath();
Expand All @@ -45,11 +45,17 @@ public function version(): ?string

private function parseOpfPath(): ?string
{
if (! isset($this->xml['rootfiles']['rootfile'])) {
$container = $this->xml['container'] ?? null;

if (! $container) {
return null;
}

$root = $this->xml['rootfiles']['rootfile'];
if (! isset($container['rootfiles']['rootfile'])) {
return null;
}

$root = $container['rootfiles']['rootfile'];
if (! array_key_exists('@attributes', $root)) {
return null;
}
Expand All @@ -62,11 +68,13 @@ private function parseOpfPath(): ?string

private function parseVersion(): ?string
{
if (! isset($this->xml['@attributes'])) {
$container = $this->xml['container'] ?? null;

if (! isset($container['@attributes'])) {
return null;
}

$attr = $this->xml['@attributes'];
$attr = $container['@attributes'];

return $attr['version'] ?? null;
}
Expand Down
38 changes: 23 additions & 15 deletions src/Formats/Epub/NcxMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Kiwilan\Ebook\Formats\Epub;

use Kiwilan\Ebook\XmlReader;
use Kiwilan\XmlReader\XmlReader;

/**
* Transform `.ncx` file to an object.
Expand All @@ -28,23 +28,27 @@ protected function __construct(

public static function make(string $content): self
{
$xml = XmlReader::toArray($content);
$xml = XmlReader::make($content)->content();

$self = new self($xml);
$self->head = $self->setHead();

$docTitle = $xml['docTitle'] ?? null;
$ncx = $xml['ncx'] ?? null;
$docTitle = $ncx['docTitle'] ?? null;

if ($docTitle) {
$docTitle = $docTitle['text'] ?? null;
$docTitle = $docTitle['text']['_value'] ?? null;
}
$self->docTitle = $docTitle;

$self->navPoints = $self->setNavPoints();
usort($self->navPoints, fn (NcxMetadataNavPoint $a, NcxMetadataNavPoint $b) => $a->playOrder() <=> $b->playOrder());
if (is_array($self->navPoints)) {
usort($self->navPoints, fn (NcxMetadataNavPoint $a, NcxMetadataNavPoint $b) => $a->playOrder() <=> $b->playOrder());
}

if (array_key_exists('@attributes', $xml)) {
$self->version = $xml['@attributes']['version'] ?? null;
$self->lang = $xml['@attributes']['lang'] ?? null;
if (array_key_exists('@attributes', $ncx)) {
$self->version = $ncx['@attributes']['version'] ?? null;
$self->lang = $ncx['@attributes']['lang'] ?? null;
}

return $self;
Expand All @@ -55,16 +59,18 @@ public static function make(string $content): self
*/
private function setHead(): ?array
{
if (! array_key_exists('head', $this->xml)) {
$ncx = $this->xml['ncx'] ?? null;

if (! array_key_exists('head', $ncx)) {
return null;
}

if (! array_key_exists('meta', $this->xml['head'])) {
if (! array_key_exists('meta', $ncx['head'])) {
return null;
}

$head = [];
foreach ($this->xml['head']['meta'] as $item) {
foreach ($ncx['head']['meta'] as $item) {
$attributes = $item['@attributes'] ?? null;
if (! $attributes) {
continue;
Expand All @@ -78,16 +84,18 @@ private function setHead(): ?array

private function setNavPoints(): ?array
{
if (! array_key_exists('navMap', $this->xml)) {
$ncx = $this->xml['ncx'] ?? null;

if (! array_key_exists('navMap', $ncx)) {
return null;
}

if (! array_key_exists('navPoint', $this->xml['navMap'])) {
if (! array_key_exists('navPoint', $ncx['navMap'])) {
return null;
}

$navPoints = [];
foreach ($this->xml['navMap']['navPoint'] as $item) {
foreach ($ncx['navMap']['navPoint'] as $item) {
$navPoints[] = NcxMetadataNavPoint::make($item);
}

Expand Down Expand Up @@ -190,7 +198,7 @@ public static function make(array $xml): self
{
$self = new self();

$self->label = $xml['navLabel']['text'] ?? null;
$self->label = $xml['navLabel']['text']['_value'] ?? null;
$self->src = $xml['content']['@attributes']['src'] ?? null;

$attributes = $xml['@attributes'] ?? null;
Expand Down
41 changes: 25 additions & 16 deletions src/Formats/Epub/OpfMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Kiwilan\Ebook\Tools\BookContributor;
use Kiwilan\Ebook\Tools\BookIdentifier;
use Kiwilan\Ebook\Tools\BookMeta;
use Kiwilan\Ebook\XmlReader;
use Kiwilan\XmlReader\XmlReader;

/**
* Transform `.opf` file to an object.
Expand Down Expand Up @@ -61,14 +61,15 @@ class OpfMetadata

public static function make(string $content, string $filename): self
{
$xml = XmlReader::toArray($content);
$xml = XmlReader::make($content)->content();
$self = new self();

$self->epubVersion = $xml['@attributes']['version'] ?? null;
$self->metadata = $xml['metadata'] ?? [];
$self->manifest = $xml['manifest'] ?? [];
$self->spine = $xml['spine'] ?? [];
$self->guide = $xml['guide'] ?? [];
$package = $xml['package'] ?? [];
$self->epubVersion = $package['@attributes']['version'] ?? null;
$self->metadata = $package['metadata'] ?? [];
$self->manifest = $package['manifest'] ?? [];
$self->spine = $package['spine'] ?? [];
$self->guide = $package['guide'] ?? [];
$self->filename = $filename;

$self->parseMetadata();
Expand Down Expand Up @@ -114,7 +115,7 @@ private function parseNode(mixed $core): ?string
}

if (is_array($core)) {
return $core['@content'] ?? null;
return $core['_value'] ?? null;
}

return null;
Expand Down Expand Up @@ -328,6 +329,12 @@ private function setDcSubjects(): array
$items = $core;
}

$temp = [];
foreach ($items as $item) {
$temp[] = $item['_value'] ?? null;
}
$items = $temp;

return $items;
}

Expand All @@ -339,6 +346,8 @@ private function setDcDate(): ?DateTime
return null;
}

$core = $core['_value'] ?? null;

try {
$date = new DateTime($core, new DateTimeZone('UTC'));
} catch (\Throwable $th) {
Expand Down Expand Up @@ -367,7 +376,7 @@ private function setDcCreators(): array
$items = [];

foreach ($core as $item) {
$name = $item['@content'];
$name = $item['_value'];
$items[$name] = new BookAuthor(
name: $name,
role: $item['@attributes']['role'] ?? null,
Expand All @@ -393,10 +402,10 @@ private function setDcContributors(): array

foreach ($core as $item) {
if (is_string($item)) {
$item = ['@content' => $item];
$item = ['_value' => $item];
}
$items[] = new BookContributor(
content: $item['@content'],
content: $item['_value'],
role: $item['@attributes']['role'] ?? null,
);
}
Expand All @@ -423,9 +432,9 @@ private function setDcRights(): array

foreach ($core as $item) {
if (is_string($item)) {
$item = ['@content' => $item];
$item = ['_value' => $item];
}
$items[] = $item['@content'];
$items[] = $item['_value'];
}

return $items;
Expand All @@ -446,7 +455,7 @@ private function setDcIdentifiers(): array
$items = [];

foreach ($core as $item) {
$value = $item['@content'] ?? null;
$value = $item['_value'] ?? null;
$scheme = $item['@attributes']['scheme'] ?? null;
$identifier = new BookIdentifier(
value: $value,
Expand Down Expand Up @@ -490,7 +499,7 @@ private function multipleItems(array $items): array
$isMultiple = array_key_exists(0, $items);

if (! $isMultiple) {
$content = $items['@content'] ?? null;
$content = $items['_value'] ?? null;
$attr = $items['@attributes'] ?? null;

// Check if bad multiple creators `Jean M. Auel, Philippe Rouard` exists
Expand All @@ -504,7 +513,7 @@ private function multipleItems(array $items): array
if (is_array($content)) {
foreach ($content as $item) {
$temp[] = [
'@content' => $item,
'_value' => $item,
'@attributes' => $attr,
];
}
Expand Down
Loading

0 comments on commit 1206e71

Please sign in to comment.