Skip to content

Commit

Permalink
Merge pull request #16 from kiwilan/develop
Browse files Browse the repository at this point in the history
Update dependency `kiwilan/php-xml-reader`
  • Loading branch information
ewilan-riviere committed Jun 20, 2023
2 parents 2bd5ddf + 716ac41 commit 40c16b8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 73 deletions.
4 changes: 2 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.3.10",
"version": "1.3.20",
"keywords": [
"php",
"ebook",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Formats/Cba/CbamMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Formats/Epub/EpubChapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Formats/Epub/EpubContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 13 additions & 19 deletions src/Formats/Epub/NcxMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
94 changes: 47 additions & 47 deletions src/Formats/Epub/OpfMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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,
];
}
}
Expand Down Expand Up @@ -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'));
Expand All @@ -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 [];
Expand All @@ -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,
);
}

Expand All @@ -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 [];
Expand All @@ -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,
);
}

Expand All @@ -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 [];
Expand All @@ -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;
Expand All @@ -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 [];
Expand All @@ -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,
Expand All @@ -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 [];
Expand Down Expand Up @@ -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, ',')) {
Expand All @@ -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,
];
}
Expand Down

0 comments on commit 40c16b8

Please sign in to comment.