From 3522eef026693efde8aae65c6c5602e0dfc7af86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ewilan=20Rivi=C3=A8re?= Date: Tue, 20 Jun 2023 12:27:46 +0200 Subject: [PATCH 1/2] up deps `kiwilan/php-xml-reader` --- composer.json | 2 +- src/Formats/Cba/CbamMetadata.php | 2 +- src/Formats/Epub/EpubChapter.php | 6 +- src/Formats/Epub/EpubContainer.php | 2 +- src/Formats/Epub/NcxMetadata.php | 32 +++++----- src/Formats/Epub/OpfMetadata.php | 94 +++++++++++++++--------------- 6 files changed, 66 insertions(+), 72 deletions(-) diff --git a/composer.json b/composer.json index ae546f5..1875233 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "php": "^8.1", "kiwilan/php-archive": "^1.4.02", "kiwilan/php-audio": "^2.0.0", - "kiwilan/php-xml-reader": "^0.1.10" + "kiwilan/php-xml-reader": "^0.2.10" }, "require-dev": { "laravel/pint": "^1.7", diff --git a/src/Formats/Cba/CbamMetadata.php b/src/Formats/Cba/CbamMetadata.php index 51832e5..54e00b9 100644 --- a/src/Formats/Cba/CbamMetadata.php +++ b/src/Formats/Cba/CbamMetadata.php @@ -213,7 +213,7 @@ private function extract(string $key): ?string } if (is_array($string)) { - $string = $string['_value'] ?? null; + $string = $string['@content'] ?? null; } return $this->normalizeString($string); diff --git a/src/Formats/Epub/EpubChapter.php b/src/Formats/Epub/EpubChapter.php index a74e2b2..5afd993 100644 --- a/src/Formats/Epub/EpubChapter.php +++ b/src/Formats/Epub/EpubChapter.php @@ -8,9 +8,9 @@ class EpubChapter { protected function __construct( - protected string $label, - protected string $source, - protected string $content, + protected ?string $label = null, + protected ?string $source = null, + protected ?string $content = null, ) { } diff --git a/src/Formats/Epub/EpubContainer.php b/src/Formats/Epub/EpubContainer.php index 565715b..4afef4f 100644 --- a/src/Formats/Epub/EpubContainer.php +++ b/src/Formats/Epub/EpubContainer.php @@ -45,7 +45,7 @@ public function version(): ?string private function parseOpfPath(): ?string { - $container = $this->xml['container'] ?? null; + $container = $this->xml ?? null; if (! $container) { return null; diff --git a/src/Formats/Epub/NcxMetadata.php b/src/Formats/Epub/NcxMetadata.php index e2e86ec..ca374de 100644 --- a/src/Formats/Epub/NcxMetadata.php +++ b/src/Formats/Epub/NcxMetadata.php @@ -22,22 +22,21 @@ class NcxMetadata protected ?string $lang = 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->head = $self->setHead(); - $ncx = $xml['ncx'] ?? null; - $docTitle = $ncx['docTitle'] ?? null; + $docTitle = $self->xml->search('docTitle'); if ($docTitle) { - $docTitle = $docTitle['text']['_value'] ?? null; + $docTitle = $docTitle['text'] ?? null; } $self->docTitle = $docTitle; @@ -46,10 +45,8 @@ public static function make(string $content): self usort($self->navPoints, fn (NcxMetadataNavPoint $a, NcxMetadataNavPoint $b) => $a->playOrder() <=> $b->playOrder()); } - if (array_key_exists('@attributes', $ncx)) { - $self->version = $ncx['@attributes']['version'] ?? null; - $self->lang = $ncx['@attributes']['lang'] ?? null; - } + $self->version = $xml->rootAttribute('version'); + $self->lang = $xml->rootAttribute('lang'); return $self; } @@ -59,7 +56,7 @@ public static function make(string $content): self */ private function setHead(): ?array { - $ncx = $this->xml['ncx'] ?? null; + $ncx = $this->xml->content(); if (! array_key_exists('head', $ncx)) { return null; @@ -71,7 +68,7 @@ private function setHead(): ?array $head = []; foreach ($ncx['head']['meta'] as $item) { - $attributes = $item['@attributes'] ?? null; + $attributes = XmlReader::getAttributes($item) ?? null; if (! $attributes) { continue; } @@ -84,18 +81,15 @@ private function setHead(): ?array private function setNavPoints(): ?array { - $ncx = $this->xml['ncx'] ?? null; - - if (! array_key_exists('navMap', $ncx)) { - return null; - } + $navMap = $this->xml->search('navMap'); + $navPoint = $this->xml->search('navPoint'); - if (! array_key_exists('navPoint', $ncx['navMap'])) { + if (! $navPoint || ! $navMap) { return null; } $navPoints = []; - foreach ($ncx['navMap']['navPoint'] as $item) { + foreach ($navPoint as $item) { $navPoints[] = NcxMetadataNavPoint::make($item); } @@ -198,7 +192,7 @@ public static function make(array $xml): self { $self = new self(); - $self->label = $xml['navLabel']['text']['_value'] ?? null; + $self->label = $xml['navLabel']['text'] ?? null; $self->src = $xml['content']['@attributes']['src'] ?? null; $attributes = $xml['@attributes'] ?? null; diff --git a/src/Formats/Epub/OpfMetadata.php b/src/Formats/Epub/OpfMetadata.php index 0b35495..41e3554 100644 --- a/src/Formats/Epub/OpfMetadata.php +++ b/src/Formats/Epub/OpfMetadata.php @@ -59,17 +59,27 @@ class OpfMetadata /** @var string[] */ protected array $contentFiles = []; + protected function __construct( + protected XmlReader $xml, + ) { + } + public static function make(string $content, string $filename): self { - $xml = XmlReader::make($content)->content(); - $self = new self(); + $xml = XmlReader::make($content); + $self = new self($xml); + + $content = $xml->content(); + $self->epubVersion = $self->xml->rootAttribute('version'); + $metadata = $content['metadata'] ?? []; + $manifest = $content['manifest'] ?? []; + $spine = $content['spine'] ?? []; + $guide = $content['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->metadata = is_array($metadata) ? $metadata : []; + $self->manifest = is_array($manifest) ? $manifest : []; + $self->spine = is_array($spine) ? $spine : []; + $self->guide = is_array($guide) ? $guide : []; $self->filename = $filename; $self->parseMetadata(); @@ -103,22 +113,20 @@ private function parseMetadata(): self private function parseMetadataNode(string $key): ?string { - $core = $this->metadata[$key] ?? null; + $core = $this->xml->search($key) ?? null; return $this->parseNode($core); } private function parseNode(mixed $core): ?string { - if (is_string($core)) { - return $core; - } + $value = XmlReader::getContent($core); - if (is_array($core)) { - return $core['_value'] ?? null; + if (! is_string($value)) { + return null; } - return null; + return $value; } private function findCover(): ?string @@ -137,10 +145,11 @@ private function findCover(): ?string $extensionsAllowed = ['jpg', 'jpeg', 'png']; foreach ($core as $item) { - $id = $item['@attributes']['id'] ?? null; + $attributes = XmlReader::getAttributes($item); + $id = $attributes['id'] ?? null; if ($id && str_contains($id, 'cover')) { - $href = $item['@attributes']['href'] ?? null; + $href = $attributes['href'] ?? null; $extension = pathinfo($href, PATHINFO_EXTENSION); if (! in_array($extension, $extensionsAllowed)) { @@ -150,7 +159,7 @@ private function findCover(): ?string $items[] = [ 'id' => $id, 'href' => $href, - 'media-type' => $item['@attributes']['media-type'] ?? null, + 'media-type' => $attributes['media-type'] ?? null, ]; } } @@ -329,28 +338,18 @@ private function setDcSubjects(): array $items = $core; } - if (array_key_exists('_value', $items)) { - $items = [$items]; - } - - $temp = []; - foreach ($items as $item) { - $temp[] = $item['_value'] ?? null; - } - $items = $temp; - return $items; } private function setDcDate(): ?DateTime { - $core = $this->metadata['dc:date'] ?? null; + $core = $this->xml->search('dc:date'); if (! $core) { return null; } - $core = $core['_value'] ?? null; + $core = XmlReader::getContent($core) ?? null; try { $date = new DateTime($core, new DateTimeZone('UTC')); @@ -370,7 +369,7 @@ private function setDcDate(): ?DateTime */ private function setDcCreators(): array { - $core = $this->metadata['dc:creator'] ?? null; + $core = $this->xml->search('dc:creator'); if (! $core) { return []; @@ -380,10 +379,11 @@ private function setDcCreators(): array $items = []; foreach ($core as $item) { - $name = $item['_value']; + $name = XmlReader::getContent($item); + $attributes = XmlReader::getAttributes($item); $items[$name] = new BookAuthor( name: $name, - role: $item['@attributes']['role'] ?? null, + role: $attributes['role'] ?? null, ); } @@ -395,7 +395,7 @@ private function setDcCreators(): array */ private function setDcContributors(): array { - $core = $this->metadata['dc:contributor']['_value'] ?? null; + $core = $this->xml->search('dc:contributor'); if (! $core) { return []; @@ -406,11 +406,11 @@ private function setDcContributors(): array foreach ($core as $item) { if (is_string($item)) { - $item = ['_value' => $item]; + $item = ['@content' => $item]; } $items[] = new BookContributor( - content: $item['_value'], - role: $item['@attributes']['role'] ?? null, + content: XmlReader::getContent($item), + role: XmlReader::getAttributes($item)['role'] ?? null, ); } @@ -422,7 +422,7 @@ private function setDcContributors(): array */ private function setDcRights(): array { - $core = $this->metadata['dc:rights'] ?? null; + $core = $this->xml->search('dc:rights'); if (! $core) { return []; @@ -436,9 +436,9 @@ private function setDcRights(): array foreach ($core as $item) { if (is_string($item)) { - $item = ['_value' => $item]; + $item = ['@content' => $item]; } - $items[] = $item['_value']; + $items[] = $item['@content']; } return $items; @@ -449,7 +449,7 @@ private function setDcRights(): array */ private function setDcIdentifiers(): array { - $core = $this->metadata['dc:identifier'] ?? null; + $core = $this->xml->search('dc:identifier'); if (! $core) { return []; @@ -459,8 +459,8 @@ private function setDcIdentifiers(): array $items = []; foreach ($core as $item) { - $value = $item['_value'] ?? null; - $scheme = $item['@attributes']['scheme'] ?? null; + $value = XmlReader::getContent($item) ?? null; + $scheme = XmlReader::getAttributes($item)['scheme'] ?? null; $identifier = new BookIdentifier( value: $value, scheme: $scheme, @@ -477,7 +477,7 @@ private function setDcIdentifiers(): array */ private function setMeta(): array { - $core = $this->metadata['meta'] ?? null; + $core = $this->xml->search('meta', strict: true); if (! $core) { return []; @@ -507,8 +507,8 @@ private function multipleItems(mixed $items): array $isMultiple = array_key_exists(0, $items); if (! $isMultiple) { - $content = $items['_value'] ?? null; - $attr = $items['@attributes'] ?? null; + $content = XmlReader::getContent($items) ?? null; + $attr = XmlReader::getAttributes($items) ?? null; // Check if bad multiple creators `Jean M. Auel, Philippe Rouard` exists if (str_contains($content, ',')) { @@ -521,7 +521,7 @@ private function multipleItems(mixed $items): array if (is_array($content)) { foreach ($content as $item) { $temp[] = [ - '_value' => $item, + '@content' => $item, '@attributes' => $attr, ]; } From 716ac4114de2ee6c66091492b98dd09ced6b1c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ewilan=20Rivi=C3=A8re?= Date: Tue, 20 Jun 2023 12:28:20 +0200 Subject: [PATCH 2/2] 1.3.20 - Update dependency `kiwilan/php-xml-reader` --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1875233..2ec0573 100644 --- a/composer.json +++ b/composer.json @@ -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.10", + "version": "1.3.20", "keywords": [ "php", "ebook",