Skip to content

Commit

Permalink
Merge pull request #17 from kiwilan/develop
Browse files Browse the repository at this point in the history
Fix some `kiwilan/php-xml-reader` parsing error
  • Loading branch information
ewilan-riviere committed Jun 20, 2023
2 parents 3c1db6a + eaf3a1f commit cef6d57
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 45 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.20",
"version": "1.3.30",
"keywords": [
"php",
"ebook",
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 @@ -24,9 +24,9 @@ public static function make(Ebook $ebook): self
if (! $xml) {
return $self;
}
$metadata = XmlReader::make($xml)->content();
$reader = XmlReader::make($xml);

$root = $metadata['@root']['tagName'] ?? null;
$root = $reader->root();
$self->type = match ($root) {
'ComicInfo' => 'cbam',
'ComicBook' => 'cbml',
Expand All @@ -46,7 +46,7 @@ public static function make(Ebook $ebook): self

if ($self->type === 'cbam') {
$self->ebook->setHasMetadata(true);
$self->cbam = CbamMetadata::make($metadata);
$self->cbam = CbamMetadata::make($reader);
}

return $self;
Expand Down
7 changes: 3 additions & 4 deletions src/Formats/Cba/CbaTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Kiwilan\Ebook\Formats\Cba;

use Kiwilan\XmlReader\XmlReader;

abstract class CbaTemplate
{
/**
* @param array<string, mixed> $metadata
*/
abstract public static function make(array $metadata): self;
abstract public static function make(XmlReader $xml): self;

/**
* @return string[]
Expand Down
17 changes: 7 additions & 10 deletions src/Formats/Cba/CbamMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use Kiwilan\Ebook\Enums\AgeRatingEnum;
use Kiwilan\Ebook\Enums\MangaEnum;
use Kiwilan\XmlReader\XmlReader;

/**
* @docs https://anansi-project.github.io/docs/comicinfo/schemas/v2.0
Expand Down Expand Up @@ -110,17 +111,13 @@ class CbamMetadata extends CbaTemplate
protected string $metadataFilename = 'ComicInfo.xml';

protected function __construct(
protected array $metadata,
protected XmlReader $xml,
) {
}

/**
* @param array<string, mixed> $metadata
*/
public static function make(array $metadata): self
public static function make(XmlReader $xml): self
{
$metadata = $metadata['ComicInfo'] ?? $metadata;
$self = new self($metadata);
$self = new self($xml);
$self->parse();

return $self;
Expand Down Expand Up @@ -188,7 +185,7 @@ private function parse(): void
$this->mainCharacterOrTeam = $this->extract('MainCharacterOrTeam');
$this->review = $this->extract('Review');

$pages = $this->metadata['Pages'] ?? null;
$pages = $this->xml->search('Pages');

if ($pages && array_key_exists('Page', $pages)) {
$pages = $pages['Page'];
Expand All @@ -206,14 +203,14 @@ private function parse(): void

private function extract(string $key): ?string
{
$string = $this->metadata[$key] ?? null;
$string = $this->xml->search($key);

if (! $string) {
return null;
}

if (is_array($string)) {
$string = $string['@content'] ?? null;
$string = XmlReader::getContent($string) ?? null;
}

return $this->normalizeString($string);
Expand Down
32 changes: 8 additions & 24 deletions src/Formats/Epub/EpubContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class EpubContainer
protected ?string $version = null;

protected function __construct(
protected array $xml,
protected XmlReader $xml,
) {
}

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

$self = new self($xml);
$self->opfPath = $self->parseOpfPath();
$self->version = $self->parseVersion();
$self->version = $self->xml->version();

if (! $self->opfPath) {
throw new \Exception("Can't parse opf path");
Expand All @@ -45,40 +45,24 @@ public function version(): ?string

private function parseOpfPath(): ?string
{
$container = $this->xml ?? null;
$rootfile = $this->xml->search('rootfile');

if (! $container) {
if (! $rootfile) {
return null;
}

if (! isset($container['rootfiles']['rootfile'])) {
return null;
}
$rootfile = reset($rootfile);
$rootAttr = XmlReader::getAttributes($rootfile);

$root = $container['rootfiles']['rootfile'];
if (! array_key_exists('@attributes', $root)) {
if (! $rootAttr) {
return null;
}

$rootAttr = $root['@attributes'];
$fullPath = $rootAttr['full-path'] ?? null;

return $fullPath;
}

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

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

$attr = $container['@attributes'];

return $attr['version'] ?? null;
}

public function toArray(): array
{
return [
Expand Down
6 changes: 3 additions & 3 deletions tests/CbaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

it('can parse ComicInfo basic', function () {
$metadata = XmlReader::make(file_get_contents(COMIC_INFO_BASIC));
$cba = CbamMetadata::make($metadata->content());
$cba = CbamMetadata::make($metadata);

expect($cba->title())->toBe('Grise Bouille, Tome I');
expect($cba->series())->toBe('Grise Bouille');
Expand Down Expand Up @@ -107,7 +107,7 @@
expect($comicMeta->alternateCount())->toBeNull();
expect($comicMeta->alternateNumber())->toBeNull();
expect($comicMeta->seriesGroup())->toBe('Fantastic Four');
expect($comicMeta->count())->toBeNull();
expect($comicMeta->count())->toBe(24);
expect($comicMeta->volume())->toBeNull();
expect($comicMeta->storyArc())->toBeNull();
expect($comicMeta->storyArcNumber())->toBeNull();
Expand Down Expand Up @@ -165,7 +165,7 @@
expect($cbam->alternateSeries())->toBeString();
expect($cbam->alternateNumber())->toBeNull();
expect($cbam->alternateCount())->toBeNull();
expect($cbam->count())->toBeNull();
expect($cbam->count())->toBe(24);
expect($cbam->volume())->toBeNull();
expect($cbam->storyArc())->toBeNull();
expect($cbam->storyArcNumber())->toBeNull();
Expand Down

0 comments on commit cef6d57

Please sign in to comment.