2
0

EpubInfo.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Tests\Internal\Epub;
  10. use PhpZip\Exception\ZipException;
  11. /**
  12. * Class EpubInfo.
  13. *
  14. * @see http://idpf.org/epub/30/spec/epub30-publications.html
  15. */
  16. class EpubInfo
  17. {
  18. private ?string $title;
  19. private ?string $creator;
  20. private ?string $language;
  21. private ?string $publisher;
  22. private ?string $description;
  23. private ?string $rights;
  24. private ?string $date;
  25. private ?string $subject;
  26. /**
  27. * EpubInfo constructor.
  28. *
  29. * @param $xmlContents
  30. *
  31. * @throws ZipException
  32. */
  33. public function __construct($xmlContents)
  34. {
  35. $doc = new \DOMDocument();
  36. $doc->loadXML($xmlContents);
  37. $xpath = new \DOMXpath($doc);
  38. $xpath->registerNamespace('root', 'http://www.idpf.org/2007/opf');
  39. $metaDataNodeList = $xpath->query('//root:metadata');
  40. if (\count($metaDataNodeList) !== 1) {
  41. throw new ZipException('Invalid .opf file format');
  42. }
  43. $metaDataNode = $metaDataNodeList->item(0);
  44. $title = $xpath->evaluate('string(//dc:title)', $metaDataNode);
  45. $creator = $xpath->evaluate('string(//dc:creator)', $metaDataNode);
  46. $language = $xpath->evaluate('string(//dc:language)', $metaDataNode);
  47. $publisher = $xpath->evaluate('string(//dc:publisher)', $metaDataNode);
  48. $description = $xpath->evaluate('string(//dc:description)', $metaDataNode);
  49. $rights = $xpath->evaluate('string(//dc:rights)', $metaDataNode);
  50. $date = $xpath->evaluate('string(//dc:date)', $metaDataNode);
  51. $subject = $xpath->evaluate('string(//dc:subject)', $metaDataNode);
  52. $this->title = empty($title) ? null : $title;
  53. $this->creator = empty($creator) ? null : $creator;
  54. $this->language = empty($language) ? null : $language;
  55. $this->publisher = empty($publisher) ? null : $publisher;
  56. $this->description = empty($description) ? null : $description;
  57. $this->rights = empty($rights) ? null : $rights;
  58. $this->date = empty($date) ? null : $date;
  59. $this->subject = empty($subject) ? null : $subject;
  60. }
  61. public function getTitle(): ?string
  62. {
  63. return $this->title;
  64. }
  65. public function getCreator(): ?string
  66. {
  67. return $this->creator;
  68. }
  69. public function getLanguage(): ?string
  70. {
  71. return $this->language;
  72. }
  73. public function getPublisher(): ?string
  74. {
  75. return $this->publisher;
  76. }
  77. public function getDescription(): ?string
  78. {
  79. return $this->description;
  80. }
  81. public function getRights(): ?string
  82. {
  83. return $this->rights;
  84. }
  85. public function getDate(): ?string
  86. {
  87. return $this->date;
  88. }
  89. public function getSubject(): ?string
  90. {
  91. return $this->subject;
  92. }
  93. public function toArray(): array
  94. {
  95. return [
  96. 'title' => $this->title,
  97. 'creator' => $this->creator,
  98. 'language' => $this->language,
  99. 'publisher' => $this->publisher,
  100. 'description' => $this->description,
  101. 'rights' => $this->rights,
  102. 'date' => $this->date,
  103. 'subject' => $this->subject,
  104. ];
  105. }
  106. }