diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..967315d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +* text=auto +*.css linguist-vendored +*.scss linguist-vendored +*.js linguist-vendored +CHANGELOG.md export-ignore diff --git a/src/AddExtensionIfNotExist.php b/src/AddExtensionIfNotExist.php index cca221e..c9d4a4a 100644 --- a/src/AddExtensionIfNotExist.php +++ b/src/AddExtensionIfNotExist.php @@ -6,19 +6,19 @@ class AddExtensionIfNotExist { private Epub $epub; - function __construct(&$epub) - { - $this->epub = &$epub; - } + function __construct(&$epub) + { + $this->epub = &$epub; + } - public function addExtension() - { - foreach ($this->epub->getImages() as $image) { + public function addExtension() + { + foreach ($this->epub->getImages() as $image) { - if (empty($image->getExtension())) { + if (empty($image->getExtension())) { - $image->rename($image->getFileName() . '.' . mb_strtolower($image->guessExtension())); - } - } - } + $image->rename($image->getFileName() . '.' . mb_strtolower($image->guessExtension())); + } + } + } } diff --git a/src/AddSectionsIds.php b/src/AddSectionsIds.php index 1254588..19e79fb 100644 --- a/src/AddSectionsIds.php +++ b/src/AddSectionsIds.php @@ -6,92 +6,92 @@ class AddSectionsIds { - private int $counter; + private int $counter; private Epub $epub; - function __construct($epub) - { - $this->epub = &$epub; - $this->counter = 0; - } + function __construct($epub) + { + $this->epub = &$epub; + $this->counter = 0; + } - public function init() - { - foreach ($this->epub->getSectionsList() as &$section) { + public function init() + { + foreach ($this->epub->getSectionsList() as &$section) { - $this->counter++; + $this->counter++; - $body = $section->body(); + $body = $section->body(); - $section_id = 'section-' . $this->counter; + $section_id = 'section-' . $this->counter; - if ($body->hasAttribute('id')) { - $old_section_id = $body->getAttribute('id'); + if ($body->hasAttribute('id')) { + $old_section_id = $body->getAttribute('id'); - $this->replaceHash($old_section_id, $section_id); - } + $this->replaceHash($old_section_id, $section_id); + } - $body->setAttribute('id', $section_id); - } + $body->setAttribute('id', $section_id); + } unset($section); foreach ($this->epub->getSectionsList() as $section) { - $nodes = $section->xpath()->query("//*[local-name()='a'][@href]"); + $nodes = $section->xpath()->query("//*[local-name()='a'][@href]"); - // находим все ссылки в главе - if ($nodes->length) { - foreach ($nodes as $node) { + // находим все ссылки в главе + if ($nodes->length) { + foreach ($nodes as $node) { - $href = Url::fromString(urldecode($node->getAttribute('href'))); + $href = Url::fromString(urldecode($node->getAttribute('href'))); - if (trim($href->getFragment()) == '') { + if (trim($href->getFragment()) == '') { - $absolutePath = $href->getPathRelativelyToAnotherUrl($section->getPath()); + $absolutePath = $href->getPathRelativelyToAnotherUrl($section->getPath()); - if ($this->findSection($absolutePath)) { - $body_id = $this->findSection($absolutePath)->body()->getAttribute('id'); + if ($this->findSection($absolutePath)) { + $body_id = $this->findSection($absolutePath)->body()->getAttribute('id'); - $node->setAttribute('href', $href->withFragment($body_id)); - } - } - } - } - } - } + $node->setAttribute('href', $href->withFragment($body_id)); + } + } + } + } + } + } - public function replaceHash($oldHash, $newHash) - { - foreach ($this->epub->getSectionsList() as $section) { + public function replaceHash($oldHash, $newHash) + { + foreach ($this->epub->getSectionsList() as $section) { - $nodes = $section->xpath()->query("//*[local-name()='a'][@href]"); + $nodes = $section->xpath()->query("//*[local-name()='a'][@href]"); - // находим все ссылки в главе - if ($nodes->length) { - foreach ($nodes as $node) { + // находим все ссылки в главе + if ($nodes->length) { + foreach ($nodes as $node) { - $href = urldecode($node->getAttribute('href')); + $href = urldecode($node->getAttribute('href')); - if (trim(Url::fromString($href)->getFragment()) == $oldHash) { + if (trim(Url::fromString($href)->getFragment()) == $oldHash) { - $href = (string)Url::fromString($href) - ->withFragment($newHash); + $href = (string)Url::fromString($href) + ->withFragment($newHash); - $node->setAttribute('href', $href); - } - } - } - } - } + $node->setAttribute('href', $href); + } + } + } + } + } - public function findSection($absolutePath) - { - foreach ($this->epub->getSectionsList() as $section) { + public function findSection($absolutePath) + { + foreach ($this->epub->getSectionsList() as $section) { - if ($section->getPath() == $absolutePath) - return $section; - } - return null; - } + if ($section->getPath() == $absolutePath) + return $section; + } + return null; + } } diff --git a/src/Container.php b/src/Container.php index 1c151c3..6ae6572 100644 --- a/src/Container.php +++ b/src/Container.php @@ -9,70 +9,70 @@ class Container extends File { - private DOMDocument $dom; + private DOMDocument $dom; /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function __construct(Epub $epub, $path = null) - { - parent::__construct($epub, $path); + { + parent::__construct($epub, $path); - if (!empty($path)) { - $this->dom = new DOMDocument(); - $this->dom->loadXML(trim($epub->zipFile->getEntryContents($path))); - } else { - $this->dom = new DOMDocument("1.0", "utf-8"); - $this->dom->formatOutput = true; + if (!empty($path)) { + $this->dom = new DOMDocument(); + $this->dom->loadXML(trim($epub->zipFile->getEntryContents($path))); + } else { + $this->dom = new DOMDocument("1.0", "utf-8"); + $this->dom->formatOutput = true; - $container = $this->dom->createElement('container'); - $container->setAttribute('version', '1.0'); - $container->setAttribute('xmlns', 'urn:oasis:names:tc:opendocument:xmlns:container'); - $this->dom->appendChild($container); + $container = $this->dom->createElement('container'); + $container->setAttribute('version', '1.0'); + $container->setAttribute('xmlns', 'urn:oasis:names:tc:opendocument:xmlns:container'); + $this->dom->appendChild($container); - $rootfiles = $this->dom->createElement('rootfiles'); - $container->appendChild($rootfiles); - } - } + $rootfiles = $this->dom->createElement('rootfiles'); + $container->appendChild($rootfiles); + } + } - public function setPath($path) - { - $this->path = $path; - $this->epub->files[$path] = $this; - $this->epub->container = $this; - } + public function setPath($path) + { + $this->path = $path; + $this->epub->files[$path] = $this; + $this->epub->container = $this; + } - public function xpath(): DOMXpath + public function xpath(): DOMXpath { return new DOMXpath($this->dom()); - } + } - public function dom(): DOMDocument + public function dom(): DOMDocument { - return $this->dom; - } + return $this->dom; + } - public function container(): DOMElement + public function container(): DOMElement { - return $this->dom()->getElementsByTagName('container')->item(0); - } + return $this->dom()->getElementsByTagName('container')->item(0); + } - public function appendRootFile($fullPath, $mediaType): DOMNode + public function appendRootFile($fullPath, $mediaType): DOMNode { - $file = $this->dom()->createElement('rootfile'); - $file->setAttribute('full-path', $fullPath); - $file->setAttribute('media-type', $mediaType); - return $this->rootfiles()->appendChild($file); - } + $file = $this->dom()->createElement('rootfile'); + $file->setAttribute('full-path', $fullPath); + $file->setAttribute('media-type', $mediaType); + return $this->rootfiles()->appendChild($file); + } - public function rootfiles(): DOMElement + public function rootfiles(): DOMElement { - return $this->dom()->getElementsByTagName('rootfiles')->item(0); - } + return $this->dom()->getElementsByTagName('rootfiles')->item(0); + } - public function getContent(): string + public function getContent(): string { - return $this->dom()->saveXML(); - } + return $this->dom()->saveXML(); + } } diff --git a/src/Epub.php b/src/Epub.php index e71da23..e71b46e 100644 --- a/src/Epub.php +++ b/src/Epub.php @@ -12,174 +12,174 @@ class Epub { - public Opf $opf; - public Ncx $ncx; - public ZipFile $zipFile; - public string $default_opf_path; - public string $default_ncx_path; - public string $default_container_path; - public string $default_folder; - public array $files = []; - public Container $container; - private UnifyTagIds $unifyTagIds; - public bool $ignoreMissingFiles = false; - - function __construct() - { - $this->zipFile = new ZipFile; - $this->default_folder = 'OEBPS'; - $this->default_container_path = 'META-INF/container.xml'; - $this->default_opf_path = $this->default_folder . '/content.opf'; - $this->default_ncx_path = $this->default_folder . '/toc.ncx'; - } + public Opf $opf; + public Ncx $ncx; + public ZipFile $zipFile; + public string $default_opf_path; + public string $default_ncx_path; + public string $default_container_path; + public string $default_folder; + public array $files = []; + public Container $container; + public bool $ignoreMissingFiles = false; + private UnifyTagIds $unifyTagIds; + + function __construct() + { + $this->zipFile = new ZipFile; + $this->default_folder = 'OEBPS'; + $this->default_container_path = 'META-INF/container.xml'; + $this->default_opf_path = $this->default_folder . '/content.opf'; + $this->default_ncx_path = $this->default_folder . '/toc.ncx'; + } /** * @throws \PhpZip\Exception\ZipException * @noinspection PhpUnusedParameterInspection */ public function setFile($path, $flags = null) - { - $this->files = []; - - if (is_resource($path)) - $this->zipFile->openFromStream($path); - elseif (strlen($path) < 255 and file_exists($path)) - $this->zipFile->openFile($path); - else - $this->zipFile->openFromString($path); - - $fullPath = $this->container() - ->rootfiles() - ->getElementsByTagName('rootfile') - ->item(0) - ->getAttribute('full-path'); - - if (!empty($fullPath)) { - $this->opf = new Opf($this, $fullPath); - } + { + $this->files = []; + + if (is_resource($path)) + $this->zipFile->openFromStream($path); + elseif (strlen($path) < 255 and file_exists($path)) + $this->zipFile->openFile($path); + else + $this->zipFile->openFromString($path); + + $fullPath = $this->container() + ->rootfiles() + ->getElementsByTagName('rootfile') + ->item(0) + ->getAttribute('full-path'); + + if (!empty($fullPath)) { + $this->opf = new Opf($this, $fullPath); + } - $this->loadFiles(); - } + $this->loadFiles(); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function container(): ?Container - { - if (empty($this->container)) { - $files = $this->findFiles('/META\-INF\/container\.xml/iu'); - - if (empty($files[0])) { - return null; - } else { - $this->container = new Container($this, $files[0]); - $this->files[$files[0]] = $this->container; - } - } - - return $this->container; - } - - /** - * Возвращаем все пути к файлам совпадающие с регулярным выражением - * - * @param string $pattern - * @return array $files - */ - public function findFiles(string $pattern): array { - $matcher = $this->zipFile->matcher(); + if (empty($this->container)) { + $files = $this->findFiles('/META\-INF\/container\.xml/iu'); + + if (empty($files[0])) { + return null; + } else { + $this->container = new Container($this, $files[0]); + $this->files[$files[0]] = $this->container; + } + } - foreach ($matcher->all()->getMatches() as $file) { - if (preg_match($pattern, $file, $matches)) { - $files[] = $file; - } - } + return $this->container; + } - return empty($files) ? [] : $files; - } + /** + * Возвращаем все пути к файлам совпадающие с регулярным выражением + * + * @param string $pattern + * @return array $files + */ + public function findFiles(string $pattern): array + { + $matcher = $this->zipFile->matcher(); + + foreach ($matcher->all()->getMatches() as $file) { + if (preg_match($pattern, $file, $matches)) { + $files[] = $file; + } + } + + return empty($files) ? [] : $files; + } /** * @throws \PhpZip\Exception\ZipException * @throws \PhpZip\Exception\ZipEntryNotFoundException */ public function loadFiles() - { - $query = "//*[local-name()='item'][@href]"; + { + $query = "//*[local-name()='item'][@href]"; - foreach ($this->opf()->xpath()->query($query, $this->opf()->spine()) as $node) { - $this->loadFile($node); - } - } + foreach ($this->opf()->xpath()->query($query, $this->opf()->spine()) as $node) { + $this->loadFile($node); + } + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ - private function loadFile(DOMElement $node) - { - $id = $node->getAttribute('id'); - - $mimeType = $node->getAttribute('media-type'); - - $href = urldecode($node->getAttribute('href')); - - $fullPath = (string)Url::fromString($href) - ->getPathRelativelyToAnotherUrl($this->opf()->getPath()) - ->withoutFragment(); - - if ($mimeType == 'application/xhtml+xml') { - $file = new Section($this, $fullPath); - - if ($itemref = $this->opf()->xpath()->query('//*[local-name()=\'itemref\'][@idref="' . $id . '"]', $this->opf()->spine())->item(0)) { - $linear = trim($itemref->getAttribute('linear')); - - if (!empty($linear)) - $file->setLinear($linear); - } - - } elseif ($mimeType == 'application/x-dtbncx+xml') { - $file = new Ncx($this, $fullPath); - } elseif (preg_match('/image\/([[:alpha:]]+)/iu', $mimeType)) { - $file = new Image($this, $fullPath); - } elseif ($mimeType == 'text/css') { - $file = new Css($this, $fullPath); - } else { - $file = new File($this, $fullPath); - } - - if ($file->isFoundInZip()) { - $file->loadContent(); - $this->files[$fullPath] = $file; - } else { - if (!$this->ignoreMissingFiles) { - $file->loadContent(); - $this->files[$fullPath] = $file; - } else { - $file->delete(); - } - } - } + public function opf(): ?Opf + { + if (empty($this->opf)) { + + $files = $this->findFiles('/([[:graph:]]+)\.opf/iu'); + + if (empty($files[0])) { + return null; + } else + $this->opf = new Opf($this, $this->default_opf_path); + } + + return $this->opf; + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ - public function opf(): ?Opf - { - if (empty($this->opf)) { + private function loadFile(DOMElement $node) + { + $id = $node->getAttribute('id'); + + $mimeType = $node->getAttribute('media-type'); + + $href = urldecode($node->getAttribute('href')); - $files = $this->findFiles('/([[:graph:]]+)\.opf/iu'); + $fullPath = (string)Url::fromString($href) + ->getPathRelativelyToAnotherUrl($this->opf()->getPath()) + ->withoutFragment(); + + if ($mimeType == 'application/xhtml+xml') { + $file = new Section($this, $fullPath); + + if ($itemref = $this->opf()->xpath()->query('//*[local-name()=\'itemref\'][@idref="' . $id . '"]', $this->opf()->spine())->item(0)) { + $linear = trim($itemref->getAttribute('linear')); + + if (!empty($linear)) + $file->setLinear($linear); + } - if (empty($files[0])) { - return null; - } else - $this->opf = new Opf($this, $this->default_opf_path); - } + } elseif ($mimeType == 'application/x-dtbncx+xml') { + $file = new Ncx($this, $fullPath); + } elseif (preg_match('/image\/([[:alpha:]]+)/iu', $mimeType)) { + $file = new Image($this, $fullPath); + } elseif ($mimeType == 'text/css') { + $file = new Css($this, $fullPath); + } else { + $file = new File($this, $fullPath); + } - return $this->opf; - } + if ($file->isFoundInZip()) { + $file->loadContent(); + $this->files[$fullPath] = $file; + } else { + if (!$this->ignoreMissingFiles) { + $file->loadContent(); + $this->files[$fullPath] = $file; + } else { + $file->delete(); + } + } + } /** * @throws \PhpZip\Exception\ZipException @@ -187,219 +187,219 @@ public function opf(): ?Opf */ public function getNcxFullPath(): bool|string { - if (!empty($this->opf())) { - $item = $this->opf()->xpath() - ->query('//*[@media-type="application/x-dtbncx+xml"]', $this->opf()->manifest()) - ->item(0); + if (!empty($this->opf())) { + $item = $this->opf()->xpath() + ->query('//*[@media-type="application/x-dtbncx+xml"]', $this->opf()->manifest()) + ->item(0); - if (!empty($item)) { - $href = $item->getAttribute('href'); + if (!empty($item)) { + $href = $item->getAttribute('href'); - return (string)Url::fromString($href)->getPathRelativelyToAnotherUrl($this->opf()->getPath()); - } - } + return (string)Url::fromString($href)->getPathRelativelyToAnotherUrl($this->opf()->getPath()); + } + } - return false; - } + return false; + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function createOpf($path = null): Opf - { - if (empty($path)) - $path = $this->default_opf_path; + { + if (empty($path)) + $path = $this->default_opf_path; - $opf = new Opf($this); - $opf->setPath($path); + $opf = new Opf($this); + $opf->setPath($path); - $this->container()->appendRootFile($this->default_opf_path, "application/oebps-package+xml"); + $this->container()->appendRootFile($this->default_opf_path, "application/oebps-package+xml"); - return $opf; - } + return $opf; + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function createContainer($path = null): Container - { - if (empty($path)) - $path = $this->default_container_path; + { + if (empty($path)) + $path = $this->default_container_path; - $container = new Container($this); - $container->setPath($path); + $container = new Container($this); + $container->setPath($path); - return $container; - } + return $container; + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function createNcx($path = null): Ncx - { - if (empty($path)) - $path = $this->default_ncx_path; + { + if (empty($path)) + $path = $this->default_ncx_path; - $ncx = new Ncx($this); - $ncx->setPath($path); + $ncx = new Ncx($this); + $ncx->setPath($path); - $href = Url::fromString($path)->getRelativePathUrl($this->opf()->getPath()); + $href = Url::fromString($path)->getRelativePathUrl($this->opf()->getPath()); - $this->opf()->appendToManifest('ncx', $href, 'application/x-dtbncx+xml'); + $this->opf()->appendToManifest('ncx', $href, 'application/x-dtbncx+xml'); - return $ncx; - } + return $ncx; + } - public function ncx(): ?Ncx - { - foreach ($this->files as $file) { - if ($file instanceof Ncx) - return $file; - } + public function ncx(): ?Ncx + { + foreach ($this->files as $file) { + if ($file instanceof Ncx) + return $file; + } - return null; - } + return null; + } - public function getSectionsList(): array - { - $sections = []; + public function getSectionsList(): array + { + $sections = []; - foreach ($this->files as $path => $item) { - if ($item instanceof Section) { - $sections[$path] = $item; - } - } + foreach ($this->files as $path => $item) { + if ($item instanceof Section) { + $sections[$path] = $item; + } + } - return $sections; - } + return $sections; + } /** * @throws \PhpZip\Exception\ZipException * @throws \PhpZip\Exception\ZipEntryNotFoundException */ public function getSectionsListInOrder(): array - { - $sections = []; + { + $sections = []; - foreach ($this->opf()->xpath()->query("//*[local-name()='itemref']", $this->opf()->spine()) as $item) { - $idref = $item->getAttribute('idref'); + foreach ($this->opf()->xpath()->query("//*[local-name()='itemref']", $this->opf()->spine()) as $item) { + $idref = $item->getAttribute('idref'); - $href = $this->opf() - ->getManifestItemById($idref) - ->item(0) - ->getAttribute('href'); + $href = $this->opf() + ->getManifestItemById($idref) + ->item(0) + ->getAttribute('href'); - $fullPath = (string)Url::fromString($href) - ->getPathRelativelyToAnotherUrl($this->opf()->getPath()) - ->withoutFragment(); + $fullPath = (string)Url::fromString($href) + ->getPathRelativelyToAnotherUrl($this->opf()->getPath()) + ->withoutFragment(); - $sections[] = $this->files[$fullPath]; - } + $sections[] = $this->files[$fullPath]; + } - return $sections; - } + return $sections; + } - public function getImages(): array - { - $images = []; + public function getImages(): array + { + $images = []; - foreach ($this->files as $path => $item) { - if ($item instanceof Image) - $images[$path] = $item; - } + foreach ($this->files as $path => $item) { + if ($item instanceof Image) + $images[$path] = $item; + } - return $images; - } + return $images; + } /** * @throws \Exception */ public function getFirstFoundFile(string $pattern) - { - $files = $this->findFiles($pattern); + { + $files = $this->findFiles($pattern); + + if (!count($files)) + throw new Exception('Can not find file with ' . $pattern . ' pattern'); + + return $files[0]; + } + + public function unifyTagIds(): UnifyTagIds + { + if (!isset($this->unifyTagIds)) { - if (!count($files)) - throw new Exception('Can not find file with ' . $pattern . ' pattern'); + $this->unifyTagIds = new UnifyTagIds($this, 'u-'); + } - return $files[0]; - } + return $this->unifyTagIds; + } - public function unifyTagIds(): UnifyTagIds + public function unifyImagesNames(): UnifyImagesNames { - if (!isset($this->unifyTagIds)) { + if (!isset($this->unifyImagesNames)) { - $this->unifyTagIds = new UnifyTagIds($this, 'u-'); - } + $this->unifyImagesNames = new UnifyImagesNames($this); + } - return $this->unifyTagIds; - } + return $this->unifyImagesNames; + } - public function unifyImagesNames(): UnifyImagesNames + public function addExtensionIfNotExist(): AddExtensionIfNotExist { - if (!isset($this->unifyImagesNames)) { + if (!isset($this->addExtensionIfNotExist)) { - $this->unifyImagesNames = new UnifyImagesNames($this); - } + $this->addExtensionIfNotExist = new AddExtensionIfNotExist($this); + } - return $this->unifyImagesNames; - } + return $this->addExtensionIfNotExist; + } - public function addExtensionIfNotExist(): AddExtensionIfNotExist + public function addSectionsIds(): AddSectionsIds { - if (!isset($this->addExtensionIfNotExist)) { + if (!isset($this->addSectionsIds)) { - $this->addExtensionIfNotExist = new AddExtensionIfNotExist($this); - } + $this->addSectionsIds = new AddSectionsIds($this); + } + + return $this->addSectionsIds; + } + + public function getSectionByFilePath($path) + { + if (isset($this->files[$path])) + return $this->files[$path]; + else + return null; + } - return $this->addExtensionIfNotExist; - } + public function getImageByFilePath($path) + { + if (isset($this->files[$path])) + return $this->files[$path]; + else + return null; + } - public function addSectionsIds(): AddSectionsIds + public function getFileByPath($path) { - if (!isset($this->addSectionsIds)) { - - $this->addSectionsIds = new AddSectionsIds($this); - } - - return $this->addSectionsIds; - } - - public function getSectionByFilePath($path) - { - if (isset($this->files[$path])) - return $this->files[$path]; - else - return null; - } - - public function getImageByFilePath($path) - { - if (isset($this->files[$path])) - return $this->files[$path]; - else - return null; - } - - public function getFileByPath($path) - { - if (isset($this->files[$path])) - return $this->files[$path]; - else - return null; - } - - public function getAllFilesInArchive(): array + if (isset($this->files[$path])) + return $this->files[$path]; + else + return null; + } + + public function getAllFilesInArchive(): array { - return $this->zipFile->getListFiles(); - } + return $this->zipFile->getListFiles(); + } - public function getAllFilesList(): array + public function getAllFilesList(): array { - return array_keys($this->files); - } + return array_keys($this->files); + } /** * @throws \PhpZip\Exception\ZipException @@ -408,17 +408,17 @@ public function getAllFilesList(): array */ public function outputAsString($storeMethod = 'ZipArchive'): bool|string { - if ($storeMethod == 'ZipFile') { - $zipFile = new ZipFile; - $zipFile->setCompressionLevel(ZipCompressionLevel::MAXIMUM); - $zipFile->addFromString('mimetype', 'application/epub+zip', ZipCompressionMethod::STORED); + if ($storeMethod == 'ZipFile') { + $zipFile = new ZipFile; + $zipFile->setCompressionLevel(ZipCompressionLevel::MAXIMUM); + $zipFile->addFromString('mimetype', 'application/epub+zip', ZipCompressionMethod::STORED); - foreach ($this->files as $path => $file) { - $zipFile->addFromString($path, $file->getContent()); - } + foreach ($this->files as $path => $file) { + $zipFile->addFromString($path, $file->getContent()); + } - return $zipFile->outputAsString(); - } else { + return $zipFile->outputAsString(); + } else { $tmp = tmpfile(); $fileName = stream_get_meta_data($tmp)['uri']; @@ -435,5 +435,5 @@ public function outputAsString($storeMethod = 'ZipArchive'): bool|string return file_get_contents($fileName); } - } + } } diff --git a/src/EpubDescription.php b/src/EpubDescription.php index 43400f8..b7ed50d 100644 --- a/src/EpubDescription.php +++ b/src/EpubDescription.php @@ -12,18 +12,18 @@ class EpubDescription extends Epub */ public function getCover(): ?Image { - $id = $this->opf()->getMetaDataContentByName('cover'); + $id = $this->opf()->getMetaDataContentByName('cover'); - $nodeList = $this->opf()->getManifestItemById($id); + $nodeList = $this->opf()->getManifestItemById($id); - if ($nodeList->length) { - $href = urldecode($nodeList->item(0)->getAttribute('href')); + if ($nodeList->length) { + $href = urldecode($nodeList->item(0)->getAttribute('href')); - return new Image($this, Url::fromString($href)->getPathRelativelyToAnotherUrl($this->opf()->getPath())); - } + return new Image($this, Url::fromString($href)->getPathRelativelyToAnotherUrl($this->opf()->getPath())); + } - return null; - } + return null; + } /** * @throws \PhpZip\Exception\ZipException @@ -31,18 +31,18 @@ public function getCover(): ?Image */ public function getTitle(): string { - $title = $this->opf()->getDublinCoreValueByName('title'); + $title = $this->opf()->getDublinCoreValueByName('title'); - if (empty($title)) { - $title = $this->opf()->getMetaDataContentByName('calibre:title_sort'); - } + if (empty($title)) { + $title = $this->opf()->getMetaDataContentByName('calibre:title_sort'); + } - if (empty($title)) { - $title = $this->opf()->getMetaDataContentByName('FB2.publish-info.book-name'); - } + if (empty($title)) { + $title = $this->opf()->getMetaDataContentByName('FB2.publish-info.book-name'); + } - return trim($title); - } + return trim($title); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException @@ -50,8 +50,8 @@ public function getTitle(): string */ public function getPublisher(): string { - return trim($this->opf()->getDublinCoreValueByName('publisher') ?? ''); - } + return trim($this->opf()->getDublinCoreValueByName('publisher') ?? ''); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException @@ -59,8 +59,8 @@ public function getPublisher(): string */ public function getPublishCity(): string { - return trim($this->opf()->getMetaDataContentByName('FB2.publish-info.city') ?? ''); - } + return trim($this->opf()->getMetaDataContentByName('FB2.publish-info.city') ?? ''); + } /** * @throws \PhpZip\Exception\ZipException @@ -68,36 +68,36 @@ public function getPublishCity(): string */ public function getPublishYear(): ?int { - $year = $this->opf()->getMetaDataContentByName('FB2.publish-info.year'); + $year = $this->opf()->getMetaDataContentByName('FB2.publish-info.year'); - if (empty($year)) { - foreach ($this->opf()->getDublinCoreByName('date') as $node) { - if ($node->hasAttribute('opf:event') and $node->getAttribute('opf:event') == 'original-publication') { - $year = (string)$node->nodeValue; - } - } - } + if (empty($year)) { + foreach ($this->opf()->getDublinCoreByName('date') as $node) { + if ($node->hasAttribute('opf:event') and $node->getAttribute('opf:event') == 'original-publication') { + $year = (string)$node->nodeValue; + } + } + } - if (empty($year)) { - foreach ($this->opf()->getDublinCoreByName('date') as $node) { - if ($node->hasAttribute('opf:event') and $node->getAttribute('opf:event') != 'modification') { - $year = (string)$node->nodeValue; - } - } - } + if (empty($year)) { + foreach ($this->opf()->getDublinCoreByName('date') as $node) { + if ($node->hasAttribute('opf:event') and $node->getAttribute('opf:event') != 'modification') { + $year = (string)$node->nodeValue; + } + } + } - if (!empty($year)) { + if (!empty($year)) { - $year = trim($year); + $year = trim($year); - if (!is_numeric($year)) { - $year = date_parse($year)['year']; - } + if (!is_numeric($year)) { + $year = date_parse($year)['year']; + } - return (integer)$year; - } else - return null; - } + return (integer)$year; + } else + return null; + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException @@ -105,8 +105,8 @@ public function getPublishYear(): ?int */ public function getLanguage(): string { - return mb_strtolower($this->opf()->getDublinCoreValueByName('language')); - } + return mb_strtolower($this->opf()->getDublinCoreValueByName('language')); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException @@ -114,8 +114,8 @@ public function getLanguage(): string */ public function getAnnotation(): ?string { - return $this->opf()->getDublinCoreValueByName('description'); - } + return $this->opf()->getDublinCoreValueByName('description'); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException @@ -123,8 +123,8 @@ public function getAnnotation(): ?string */ public function getRightsHolder(): ?string { - return $this->opf()->getDublinCoreValueByName('rightsholder'); - } + return $this->opf()->getDublinCoreValueByName('rightsholder'); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException @@ -132,32 +132,32 @@ public function getRightsHolder(): ?string */ public function getCreatedDate(): ?int { - $year = $this->opf()->getDublinCoreValueByName('created'); + $year = $this->opf()->getDublinCoreValueByName('created'); - if (!empty($year)) - return (integer)$year; - else - return null; - } + if (!empty($year)) + return (integer)$year; + else + return null; + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function getISBN() - { - foreach ($this->opf()->getDublinCoreByName('identifier') as $node) { + { + foreach ($this->opf()->getDublinCoreByName('identifier') as $node) { - if ($node->hasAttribute('id') and $node->getAttribute('id') == 'pub-identifier') { + if ($node->hasAttribute('id') and $node->getAttribute('id') == 'pub-identifier') { - preg_match('/isbn:([\-0-9]+)/iu', $node->nodeValue, $matches); + preg_match('/isbn:([\-0-9]+)/iu', $node->nodeValue, $matches); - return $matches[1] ?? ''; - } - } + return $matches[1] ?? ''; + } + } return null; - } + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException @@ -165,23 +165,23 @@ public function getISBN() */ public function getAuthors(): array { - $authors = []; + $authors = []; - foreach ($this->opf()->getDublinCoreByName('creator') as $node) { + foreach ($this->opf()->getDublinCoreByName('creator') as $node) { - if ($node->hasAttribute('opf:role') and $node->getAttribute('opf:role') == 'aut') { - $authors[] = $node->nodeValue; - } - } + if ($node->hasAttribute('opf:role') and $node->getAttribute('opf:role') == 'aut') { + $authors[] = $node->nodeValue; + } + } - if (empty($authors)) { - foreach ($this->opf()->getDublinCoreByName('creator') as $node) { - $authors[] = $node->nodeValue; - } - } + if (empty($authors)) { + foreach ($this->opf()->getDublinCoreByName('creator') as $node) { + $authors[] = $node->nodeValue; + } + } - return empty($authors) ? [] : $authors; - } + return empty($authors) ? [] : $authors; + } /** * @throws \PhpZip\Exception\ZipException @@ -189,36 +189,36 @@ public function getAuthors(): array */ public function getTranslators(): array { - $translators = []; + $translators = []; - foreach ($this->opf()->getDublinCoreByName('contributor') as $node) { - if ($node->hasAttribute('opf:role') and $node->getAttribute('opf:role') == 'trl') { - $translators[] = $node->nodeValue; - } - } + foreach ($this->opf()->getDublinCoreByName('contributor') as $node) { + if ($node->hasAttribute('opf:role') and $node->getAttribute('opf:role') == 'trl') { + $translators[] = $node->nodeValue; + } + } - foreach ($this->opf()->getDublinCoreByName('creator') as $node) { - if ($node->hasAttribute('opf:role') and $node->getAttribute('opf:role') == 'trl') { - $translators[] = $node->nodeValue; - } - } + foreach ($this->opf()->getDublinCoreByName('creator') as $node) { + if ($node->hasAttribute('opf:role') and $node->getAttribute('opf:role') == 'trl') { + $translators[] = $node->nodeValue; + } + } - foreach ($this->opf()->getMetaDataByName('FB2.book-info.translator') as $node) { - if ($node->hasAttribute('content')) { - $translators[] = $node->getAttribute('content'); - } - } + foreach ($this->opf()->getMetaDataByName('FB2.book-info.translator') as $node) { + if ($node->hasAttribute('content')) { + $translators[] = $node->getAttribute('content'); + } + } - foreach ($this->opf()->getMetaDataByName('FB2.title-info.translator') as $node) { - if ($node->hasAttribute('content')) { - $translators[] = $node->getAttribute('content'); - } - } + foreach ($this->opf()->getMetaDataByName('FB2.title-info.translator') as $node) { + if ($node->hasAttribute('content')) { + $translators[] = $node->getAttribute('content'); + } + } - $translators = array_unique($translators); + $translators = array_unique($translators); - return empty($translators) ? [] : $translators; - } + return empty($translators) ? [] : $translators; + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException @@ -226,12 +226,12 @@ public function getTranslators(): array */ public function getGenres(): array { - foreach ($this->opf()->getDublinCoreByName('subject') as $node) { - $genres[] = $node->nodeValue; - } + foreach ($this->opf()->getDublinCoreByName('subject') as $node) { + $genres[] = $node->nodeValue; + } - return empty($genres) ? [] : $genres; - } + return empty($genres) ? [] : $genres; + } /** * @throws \PhpZip\Exception\ZipException @@ -239,50 +239,50 @@ public function getGenres(): array */ public function getSequences(): array { - $c = 0; - $sequences = []; + $c = 0; + $sequences = []; - $query = "*[local-name()='meta'][@content][@name='calibre:series']"; + $query = "*[local-name()='meta'][@content][@name='calibre:series']"; - foreach ($this->opf()->xpath()->query($query, $this->opf()->metaData()) as $node) { + foreach ($this->opf()->xpath()->query($query, $this->opf()->metaData()) as $node) { - $name = $node->getAttribute('content'); + $name = $node->getAttribute('content'); - foreach ($this->opf()->xpath()->query("following-sibling::*[local-name()='meta']", $node) as $sibling) { - if ($sibling->getAttribute('name') == 'calibre:series') { - break; - } + foreach ($this->opf()->xpath()->query("following-sibling::*[local-name()='meta']", $node) as $sibling) { + if ($sibling->getAttribute('name') == 'calibre:series') { + break; + } - if ($sibling->getAttribute('name') == 'calibre:series_index') { - $number = $sibling->getAttribute('content'); - } - } + if ($sibling->getAttribute('name') == 'calibre:series_index') { + $number = $sibling->getAttribute('content'); + } + } - $sequences[$c]['name'] = trim($name); + $sequences[$c]['name'] = trim($name); - if (!empty($number)) - $sequences[$c]['number'] = trim($number); + if (!empty($number)) + $sequences[$c]['number'] = trim($number); - $c++; - unset($name); - unset($number); - } + $c++; + unset($name); + unset($number); + } - foreach ($this->opf()->getMetaDataByName('FB2.book-info.sequence') as $node) { - if ($node->hasAttribute('content')) { - preg_match('/(.*);(?:.*)number=([0-9]+)/iu', $node->getAttribute('content'), $match); + foreach ($this->opf()->getMetaDataByName('FB2.book-info.sequence') as $node) { + if ($node->hasAttribute('content')) { + preg_match('/(.*);(?:.*)number=([0-9]+)/iu', $node->getAttribute('content'), $match); - if (!empty($match[1])) { - $sequences[$c] = [ - 'name' => trim($match[1]), - 'number' => intval($match[2]) ?? null - ]; + if (!empty($match[1])) { + $sequences[$c] = [ + 'name' => trim($match[1]), + 'number' => intval($match[2]) ?? null + ]; - $c++; - } - } - } + $c++; + } + } + } - return empty($sequences) ? [] : $sequences; - } + return empty($sequences) ? [] : $sequences; + } } diff --git a/src/File.php b/src/File.php index e03b58c..a08dce7 100644 --- a/src/File.php +++ b/src/File.php @@ -4,134 +4,134 @@ class File { - protected string $path; - protected string $content; - protected Epub $epub; + protected string $path; + protected string $content; + protected Epub $epub; - function __construct(Epub &$epub, $path = null) - { - $this->epub = &$epub; + function __construct(Epub &$epub, $path = null) + { + $this->epub = &$epub; - if (!empty($path)) - $this->setPath($path); - } + if (!empty($path)) + $this->setPath($path); + } - public function getStream() - { + public function getStream() + { - } + } - public function getPathinfo() - { - return pathinfo($this->getPath()); - } + public function getPathinfo() + { + return pathinfo($this->getPath()); + } - public function getPath(): string + public function getPath(): string { - return $this->path; - } + return $this->path; + } - public function setPath($path) - { - $this->path = $path; - $this->epub->files[$path] = $this; - } + public function setPath($path) + { + $this->path = $path; + $this->epub->files[$path] = $this; + } - public function getDirname(): string + public function getDirname(): string { - return dirname($this->getPath()); - } + return dirname($this->getPath()); + } - public function getBaseName(): string + public function getBaseName(): string { - return basename($this->getPath()); - } + return basename($this->getPath()); + } - public function getFileName() - { - return pathinfo($this->getPath(), PATHINFO_FILENAME); - } + public function getFileName() + { + return pathinfo($this->getPath(), PATHINFO_FILENAME); + } - public function getExtension() - { - return pathinfo($this->getPath(), PATHINFO_EXTENSION); - } + public function getExtension() + { + return pathinfo($this->getPath(), PATHINFO_EXTENSION); + } - public function getSize(): int + public function getSize(): int { - return strlen($this->getContent()); - } + return strlen($this->getContent()); + } - public function getContent(): string + public function getContent(): string { - return $this->content; - } + return $this->content; + } - public function setContent($content) - { - $this->content = $content; - } + public function setContent($content) + { + $this->content = $content; + } - public function isExists(): bool - { - return isset($this->epub->files[$this->getPath()]); - } + public function isExists(): bool + { + return isset($this->epub->files[$this->getPath()]); + } - public function isFoundInZip(): bool - { - return $this->epub->zipFile->hasEntry($this->getPath()); - } + public function isFoundInZip(): bool + { + return $this->epub->zipFile->hasEntry($this->getPath()); + } - public function delete() - { - //$this->epub->zipFile->deleteFromName($this->path); - unset($this->epub->files[$this->getPath()]); - } + public function delete() + { + //$this->epub->zipFile->deleteFromName($this->path); + unset($this->epub->files[$this->getPath()]); + } - public function getMd5(): string + public function getMd5(): string { - return md5($this->getContent()); - } + return md5($this->getContent()); + } /** * @throws \PhpZip\Exception\ZipException */ public function rename(string $newName) - { - $from = $this->getPath(); + { + $from = $this->getPath(); - $this->epub->zipFile->rename($from, $newName); + $this->epub->zipFile->rename($from, $newName); - $this->setPath($newName); - } + $this->setPath($newName); + } /** * @throws \PhpZip\Exception\ZipException */ public function save() - { - $this->epub->zipFile->addFromString($this->getPath(), $this->getContent()); - } + { + $this->epub->zipFile->addFromString($this->getPath(), $this->getContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function loadContent() - { - $this->content = $this->epub->zipFile->getEntryContents($this->path); - } + { + $this->content = $this->epub->zipFile->getEntryContents($this->path); + } /** * @throws \PhpZip\Exception\ZipException */ public function writeInArchive() - { - $this->epub->zipFile->addFromString($this->getPath(), $this->content); - } - - public function getEpub(): Epub - { - return $this->epub; - } + { + $this->epub->zipFile->addFromString($this->getPath(), $this->content); + } + + public function getEpub(): Epub + { + return $this->epub; + } } diff --git a/src/Image.php b/src/Image.php index 1a2b02e..656a716 100644 --- a/src/Image.php +++ b/src/Image.php @@ -8,68 +8,68 @@ class Image extends File { - protected Epub $epub; - protected string $content; - private Imagick $imagick; - private string $id; - private string $href; - - function __construct(&$epub, string $path = null) - { - parent::__construct($epub, $path); - } - - public function isValid(): bool + protected Epub $epub; + protected string $content; + private Imagick $imagick; + private string $id; + private string $href; + + function __construct(&$epub, string $path = null) + { + parent::__construct($epub, $path); + } + + public function isValid(): bool { - try { - $this->getImagick(); - } catch (ImagickException) { - return false; - } - return true; - } + try { + $this->getImagick(); + } catch (ImagickException) { + return false; + } + return true; + } /** * @throws \ImagickException */ public function getImagick(): Imagick { - if (empty($this->imagick)) { - $this->imagick = new Imagick(); - $this->imagick->readImageBlob($this->content); - } + if (empty($this->imagick)) { + $this->imagick = new Imagick(); + $this->imagick->readImageBlob($this->content); + } - return $this->imagick; - } + return $this->imagick; + } - public function setContent($content) - { - $this->content = $content; - } + public function setContent($content) + { + $this->content = $content; + } /** * @throws \ImagickException */ public function getWidth(): int { - return $this->getImagick()->getImageWidth(); - } + return $this->getImagick()->getImageWidth(); + } /** * @throws \ImagickException */ public function getHeight(): int { - return $this->getImagick()->getImageHeight(); - } + return $this->getImagick()->getImageHeight(); + } /** * @throws \ImagickException */ public function guessExtension(): string { - return strtolower($this->getImagick()->getImageFormat()); - } + return strtolower($this->getImagick()->getImageFormat()); + } /** * @throws \ImagickException @@ -77,84 +77,84 @@ public function guessExtension(): string * @throws \PhpZip\Exception\ZipException */ public function addToManifest() - { - $this->epub->opf() - ->appendToManifest($this->getId(), $this->getHref(), $this->getContentType()); - } + { + $this->epub->opf() + ->appendToManifest($this->getId(), $this->getHref(), $this->getContentType()); + } - public function getId(): string + public function getId(): string { - return $this->id; - } + return $this->id; + } - public function setId($id) - { - $this->id = $id; - } + public function setId($id) + { + $this->id = $id; + } - public function getHref(): string + public function getHref(): string { - return $this->href; - } + return $this->href; + } - public function setHref($href) - { - $this->href = $href; - } + public function setHref($href) + { + $this->href = $href; + } /** * @throws \ImagickException */ public function getContentType(): string { - return $this->getImagick()->getImageMimeType(); - } + return $this->getImagick()->getImageMimeType(); + } - public function rename(string $newName): bool + public function rename(string $newName): bool { - $oldPath = $this->getPath(); + $oldPath = $this->getPath(); - $newPath = (string)Url::fromString($this->getPath())->withBasename($newName); + $newPath = (string)Url::fromString($this->getPath())->withBasename($newName); - foreach ($this->epub->getSectionsList() as $section) { + foreach ($this->epub->getSectionsList() as $section) { - $imagesNodes = $section->xpath()->query("//*[local-name()='img'][@src]", $section->body()); + $imagesNodes = $section->xpath()->query("//*[local-name()='img'][@src]", $section->body()); - foreach ($imagesNodes as $imagesNode) { + foreach ($imagesNodes as $imagesNode) { - $src = $imagesNode->getAttribute('src'); + $src = $imagesNode->getAttribute('src'); - $image_url = Url::fromString($src); + $image_url = Url::fromString($src); - if ($this->getPath() == $image_url->getPathRelativelyToAnotherUrl($section->getPath())->withoutFragment()) { - $imagesNode->setAttribute('src', $image_url->withBasename($newName)); - } - } - } + if ($this->getPath() == $image_url->getPathRelativelyToAnotherUrl($section->getPath())->withoutFragment()) { + $imagesNode->setAttribute('src', $image_url->withBasename($newName)); + } + } + } - $query = "*[local-name()='item'][@media-type][@href][contains(@media-type,'image')]"; + $query = "*[local-name()='item'][@media-type][@href][contains(@media-type,'image')]"; - foreach ($this->epub->opf()->xpath()->query($query, $this->epub->opf()->manifest()) as $node) { + foreach ($this->epub->opf()->xpath()->query($query, $this->epub->opf()->manifest()) as $node) { - $image_url = Url::fromString($node->getAttribute('href')); + $image_url = Url::fromString($node->getAttribute('href')); - if ($this->getPath() == $image_url->getPathRelativelyToAnotherUrl($this->epub->opf()->getPath())->withoutFragment()) { + if ($this->getPath() == $image_url->getPathRelativelyToAnotherUrl($this->epub->opf()->getPath())->withoutFragment()) { - $node->setAttribute('href', $image_url->withBasename($newName)); - $node->setAttribute('id', $newName); - } - } + $node->setAttribute('href', $image_url->withBasename($newName)); + $node->setAttribute('id', $newName); + } + } - $this->setPath($newPath); + $this->setPath($newPath); - unset($this->epub->files[$oldPath]); + unset($this->epub->files[$oldPath]); - return true; - } + return true; + } - public function setPath($path) - { - $this->path = $path; - $this->epub->files[$path] = $this; - } + public function setPath($path) + { + $this->path = $path; + $this->epub->files[$path] = $this; + } } diff --git a/src/NavPoint.php b/src/NavPoint.php index 4892398..376c6a7 100644 --- a/src/NavPoint.php +++ b/src/NavPoint.php @@ -4,8 +4,8 @@ class NavPoint extends File { - public function __construct($epub, $path = null) - { + public function __construct($epub, $path = null) + { parent::__construct($epub, $path); - } + } } diff --git a/src/Ncx.php b/src/Ncx.php index 73c41eb..cc429d3 100644 --- a/src/Ncx.php +++ b/src/Ncx.php @@ -11,120 +11,120 @@ class Ncx extends File { - private string $xp = ""; - private DOMDocument $dom; + private string $xp = ""; + private DOMDocument $dom; /** @noinspection HttpUrlsUsage */ public function __construct($epub, $path = null) - { - parent::__construct($epub, $path); + { + parent::__construct($epub, $path); - if (!is_null($path)) { - $this->setPath($path); - $this->loadXML(trim($epub->zipFile->getEntryContents($path))); - } else { - $imp = new DOMImplementation; + if (!is_null($path)) { + $this->setPath($path); + $this->loadXML(trim($epub->zipFile->getEntryContents($path))); + } else { + $imp = new DOMImplementation; - $dtd = $imp->createDocumentType('ncx', '-//NISO//DTD ncx 2005-1//EN', 'http://www.daisy.org/z3986/2005/ncx-2005-1.dtd'); + $dtd = $imp->createDocumentType('ncx', '-//NISO//DTD ncx 2005-1//EN', 'http://www.daisy.org/z3986/2005/ncx-2005-1.dtd'); - $this->dom = $imp->createDocument(null, "ncx", $dtd); - $this->dom->encoding = "utf-8"; - $this->dom->formatOutput = true; + $this->dom = $imp->createDocument(null, "ncx", $dtd); + $this->dom->encoding = "utf-8"; + $this->dom->formatOutput = true; - $ncx = $this->dom->documentElement; + $ncx = $this->dom->documentElement; - $ncx->setAttribute("version", "2005-1"); - $ncx->setAttribute("xmlns", "http://www.daisy.org/z3986/2005/ncx/"); + $ncx->setAttribute("version", "2005-1"); + $ncx->setAttribute("xmlns", "http://www.daisy.org/z3986/2005/ncx/"); - $head = $this->dom->createElement('head'); - $ncx->appendChild($head); + $head = $this->dom->createElement('head'); + $ncx->appendChild($head); - $docTitle = $this->dom->createElement('docTitle'); - $title = $this->dom->createElement('text'); - $docTitle->appendChild($title); - $ncx->appendChild($docTitle); + $docTitle = $this->dom->createElement('docTitle'); + $title = $this->dom->createElement('text'); + $docTitle->appendChild($title); + $ncx->appendChild($docTitle); - $navMap = $this->dom->createElement('navMap'); - $ncx->appendChild($navMap); - } - } + $navMap = $this->dom->createElement('navMap'); + $ncx->appendChild($navMap); + } + } - public function setPath($path) - { - $this->path = $path; - $this->epub->files[$path] = $this; - $this->epub->ncx = $this; - } + public function setPath($path) + { + $this->path = $path; + $this->epub->files[$path] = $this; + $this->epub->ncx = $this; + } - public function loadXML($path) - { - $this->dom = new DOMDocument(); - $this->dom->loadXML($path); - } + public function loadXML($path) + { + $this->dom = new DOMDocument(); + $this->dom->loadXML($path); + } - public function head(): ?DOMElement + public function head(): ?DOMElement { - $head = $this->xpath()->query("*[local-name()='head']"); + $head = $this->xpath()->query("*[local-name()='head']"); - if (empty($head)) - return null; + if (empty($head)) + return null; - return $head->item(0); - } + return $head->item(0); + } - public function xpath(): DOMXpath - { - if (empty($this->xpath)) - $this->xpath = new DOMXpath($this->dom()); + public function xpath(): DOMXpath + { + if (empty($this->xpath)) + $this->xpath = new DOMXpath($this->dom()); - return $this->xpath; - } + return $this->xpath; + } - function dom(): DOMDocument - { - return $this->dom; - } + function dom(): DOMDocument + { + return $this->dom; + } - public function getNavPoints($parent): DOMNodeList + public function getNavPoints($parent): DOMNodeList { return $this->xpath()->query("*[local-name()='navPoint']", $parent); - } + } - public function getFileById($id) - { - $navPoint = $this->xpath()->query("//*[local-name()='navPoint'][@id='" . $id . "']", $this->navmap()); + public function getFileById($id) + { + $navPoint = $this->xpath()->query("//*[local-name()='navPoint'][@id='" . $id . "']", $this->navmap()); - $content = $this->xpath()->query("*[local-name()='content']", $navPoint->item(0)); + $content = $this->xpath()->query("*[local-name()='content']", $navPoint->item(0)); - $src = $content->item(0)->getAttribute('src'); + $src = $content->item(0)->getAttribute('src'); - $fullPath = (string)Url::fromString($src) - ->getPathRelativelyToAnotherUrl($this->getPath()) - ->withoutFragment(); + $fullPath = (string)Url::fromString($src) + ->getPathRelativelyToAnotherUrl($this->getPath()) + ->withoutFragment(); - return $this->epub->getFileByPath($fullPath); - } + return $this->epub->getFileByPath($fullPath); + } - public function navmap(): ?DOMElement + public function navmap(): ?DOMElement { - $navMap = $this->xpath()->query("*[local-name()='navMap']"); + $navMap = $this->xpath()->query("*[local-name()='navMap']"); - if (empty($navMap)) - return null; + if (empty($navMap)) + return null; - return $navMap->item(0); - } + return $navMap->item(0); + } - public function getTextById($id): string + public function getTextById($id): string { - $navPoint = $this->xpath()->query("//*[local-name()='navPoint'][@id='" . $id . "']", $this->navmap()); + $navPoint = $this->xpath()->query("//*[local-name()='navPoint'][@id='" . $id . "']", $this->navmap()); - $text = $this->xpath()->query("*[local-name()='navLabel']/*[local-name()='text']", $navPoint->item(0))->item(0); + $text = $this->xpath()->query("*[local-name()='navLabel']/*[local-name()='text']", $navPoint->item(0))->item(0); - return $text->nodeValue; - } + return $text->nodeValue; + } - public function findTitleBySrc($src): string + public function findTitleBySrc($src): string { $text = $this->xpath() ->query("//*[@src='" . $src . "']", $this->navmap())[0] @@ -132,121 +132,121 @@ public function findTitleBySrc($src): string ->query("*[local-name()='" . $this->xp . "navLabel']/*[local-name()='" . $this->xp . "text']")[0]; return (string)$text; - } + } - public function findNavPointBySrc($src) - { + public function findNavPointBySrc($src) + { return $this->xpath()->query("//*[@src='" . $src . "']", $this->navmap())[0] ->query("parent::*")[0]; - } - - public function findNavPointByBaseName($baseName) - { - foreach ($this->xpath()->query("//[local-name()='navPoint'][@src]", $this->navmap()) as $section) { - if (preg_match('/' . preg_quote($baseName, '/') . '/iu', $section->attributes()->src, $matches)) { - return $section; - } - } + } + + public function findNavPointByBaseName($baseName) + { + foreach ($this->xpath()->query("//[local-name()='navPoint'][@src]", $this->navmap()) as $section) { + if (preg_match('/' . preg_quote($baseName, '/') . '/iu', $section->attributes()->src, $matches)) { + return $section; + } + } return null; - } + } - public function findTitleByFullPath($searchableFullPath): ?string + public function findTitleByFullPath($searchableFullPath): ?string { - foreach ($this->xpath()->query("//*[local-name()='navPoint']", $this->navmap()) as $navPoint) { + foreach ($this->xpath()->query("//*[local-name()='navPoint']", $this->navmap()) as $navPoint) { - $src = $this->xpath()->query("*[local-name()='content'][@src]", $navPoint)->item(0)->getAttribute('src'); + $src = $this->xpath()->query("*[local-name()='content'][@src]", $navPoint)->item(0)->getAttribute('src'); - $fullPath = (string)Url::fromString($src) - ->getPathRelativelyToAnotherUrl($this->getPath()) - ->withoutFragment(); + $fullPath = (string)Url::fromString($src) + ->getPathRelativelyToAnotherUrl($this->getPath()) + ->withoutFragment(); - if ($fullPath == $searchableFullPath) { - return $this->xpath()->query("*[local-name()='navLabel']/*[local-name()='text']", $navPoint)->item(0)->nodeValue; - } - } + if ($fullPath == $searchableFullPath) { + return $this->xpath()->query("*[local-name()='navLabel']/*[local-name()='text']", $navPoint)->item(0)->nodeValue; + } + } return null; - } + } - public function getParentSrcBySrc($src): string + public function getParentSrcBySrc($src): string { - $src = $this->xpath() + $src = $this->xpath() ->query("//*[@src='" . $src . "']", $this->navmap())[0] - ->xpath("parent::*")[0] - ->xpath("parent::*")[0] - ->xpath("*[local-name()='content']")[0] - ->attributes()->src; + ->xpath("parent::*")[0] + ->xpath("parent::*")[0] + ->xpath("*[local-name()='content']")[0] + ->attributes()->src; - return (string)$src; - } + return (string)$src; + } /** @noinspection PhpUnusedParameterInspection */ public function appendNavMap($text, $src, $id, $playOrder, $parent = null): DOMElement { - $navPoint = $this->dom->createElement('navPoint'); - $navPoint->setAttribute('id', $id); + $navPoint = $this->dom->createElement('navPoint'); + $navPoint->setAttribute('id', $id); - $navLabel = $this->dom->createElement('navLabel'); - $navPoint->appendChild($navLabel); + $navLabel = $this->dom->createElement('navLabel'); + $navPoint->appendChild($navLabel); - $textNode = $this->dom->createElement('text'); - $textNode->appendChild($this->dom->createTextNode($text)); - $navLabel->appendChild($textNode); + $textNode = $this->dom->createElement('text'); + $textNode->appendChild($this->dom->createTextNode($text)); + $navLabel->appendChild($textNode); - $content = $this->dom->createElement('content'); - $content->setAttribute('src', $src); - $navPoint->appendChild($content); + $content = $this->dom->createElement('content'); + $content->setAttribute('src', $src); + $navPoint->appendChild($content); - if (empty($parent)) - $this->navmap()->appendChild($navPoint); - else - $parent->appendChild($navPoint); + if (empty($parent)) + $this->navmap()->appendChild($navPoint); + else + $parent->appendChild($navPoint); - return $navPoint; - } + return $navPoint; + } - function getTree(): array + function getTree(): array { - $navMap = $this->navmap(); + $navMap = $this->navmap(); - $navPoints = $this->xpath()->query("*[local-name()='navPoint']", $navMap); + $navPoints = $this->xpath()->query("*[local-name()='navPoint']", $navMap); - $array = []; + $array = []; - foreach ($navPoints as $navPoint) { - $array[] = $this->getSubNavPoints($navPoint); - } + foreach ($navPoints as $navPoint) { + $array[] = $this->getSubNavPoints($navPoint); + } - return $array; - } + return $array; + } - private function getSubNavPoints($navPoint): array + private function getSubNavPoints($navPoint): array { - $id = $navPoint->getAttribute('id'); - $text = $this->xpath()->query("*[local-name()='navLabel']/*[local-name()='text']", $navPoint)->item(0)->nodeValue; - $src = $this->xpath()->query("*[local-name()='content']", $navPoint)->item(0)->getAttribute('src'); - $playOrder = $this->xpath()->query("*[local-name()='content']", $navPoint)->item(0)->getAttribute('playOrder'); - - $childs = []; - - $subNavPoints = $this->xpath()->query("*[local-name()='navPoint']", $navPoint); - - if ($subNavPoints->length) { - foreach ($subNavPoints as $subNavPoint) { - $childs[] = $this->getSubNavPoints($subNavPoint); - } - } - - return [ - 'id' => $id, - 'text' => $text, - 'src' => $src, - 'playOrder' => $playOrder, - 'childs' => $childs - ]; - } - - public function getContent(): string + $id = $navPoint->getAttribute('id'); + $text = $this->xpath()->query("*[local-name()='navLabel']/*[local-name()='text']", $navPoint)->item(0)->nodeValue; + $src = $this->xpath()->query("*[local-name()='content']", $navPoint)->item(0)->getAttribute('src'); + $playOrder = $this->xpath()->query("*[local-name()='content']", $navPoint)->item(0)->getAttribute('playOrder'); + + $childs = []; + + $subNavPoints = $this->xpath()->query("*[local-name()='navPoint']", $navPoint); + + if ($subNavPoints->length) { + foreach ($subNavPoints as $subNavPoint) { + $childs[] = $this->getSubNavPoints($subNavPoint); + } + } + + return [ + 'id' => $id, + 'text' => $text, + 'src' => $src, + 'playOrder' => $playOrder, + 'childs' => $childs + ]; + } + + public function getContent(): string { - return $this->dom()->saveXML(); - } + return $this->dom()->saveXML(); + } } diff --git a/src/Opf.php b/src/Opf.php index 7810524..f5d3f6a 100644 --- a/src/Opf.php +++ b/src/Opf.php @@ -10,254 +10,254 @@ class Opf extends File { - private DOMDocument $dom; - private DOMElement $metaData; - private DOMElement $manifest; - private DOMElement $spine; - private DOMXpath $xpath; - private array $prefixes = []; - private string $dublinCoreNameSpace = "http://purl.org/dc/elements/1.1/"; + private DOMDocument $dom; + private DOMElement $metaData; + private DOMElement $manifest; + private DOMElement $spine; + private DOMXpath $xpath; + private array $prefixes = []; + private string $dublinCoreNameSpace = "http://purl.org/dc/elements/1.1/"; /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function __construct(Epub $epub, $path = null) - { - parent::__construct($epub, $path); - - if (!is_null($path)) { - $this->setPath($path); - - $this->dom = new DOMDocument(); - $this->dom->loadXML(trim($epub->zipFile->getEntryContents($path))); - - foreach ($this->xpath()->query('namespace::*', $this->metaData()) as $node) { - if ($node->prefix == 'dc') { - $this->dublinCoreNameSpace = $node->namespaceURI; - } - } - } else { - $this->dom = new DOMDocument('1.0', 'utf-8'); - $this->dom->formatOutput = true; - - $this->createPackage(); - $this->createMetaData(); - $this->createManifest(); - $this->createSpine(); - //$this->createGuide(); - } - } - - public function setPath($path) - { - $this->path = $path; - $this->epub->files[$path] = $this; - $this->epub->opf = $this; - } - - public function xpath(): DOMXpath { - if (empty($this->xpath)) { - $this->xpath = new DOMXpath($this->dom()); - } + parent::__construct($epub, $path); + + if (!is_null($path)) { + $this->setPath($path); + + $this->dom = new DOMDocument(); + $this->dom->loadXML(trim($epub->zipFile->getEntryContents($path))); + + foreach ($this->xpath()->query('namespace::*', $this->metaData()) as $node) { + if ($node->prefix == 'dc') { + $this->dublinCoreNameSpace = $node->namespaceURI; + } + } + } else { + $this->dom = new DOMDocument('1.0', 'utf-8'); + $this->dom->formatOutput = true; + + $this->createPackage(); + $this->createMetaData(); + $this->createManifest(); + $this->createSpine(); + //$this->createGuide(); + } + } + + public function setPath($path) + { + $this->path = $path; + $this->epub->files[$path] = $this; + $this->epub->opf = $this; + } + + public function xpath(): DOMXpath + { + if (empty($this->xpath)) { + $this->xpath = new DOMXpath($this->dom()); + } + + return $this->xpath; + } + + function dom(): DOMDocument + { + return $this->dom; + } + + function metaData(): ?DOMElement + { + $nodeList = $this->xpath()->query("*[local-name()='metadata']", $this->package()); + + if ($nodeList->length) { + $this->metaData = $nodeList->item(0); + } else { + return null; + } + + foreach ($this->xpath()->query('namespace::*', $this->metaData) as $node) { + + $prefix = $this->metaData->lookupPrefix($node->nodeValue); + $this->prefixes[$prefix] = $node->nodeValue; + } + + return $this->metaData; + } - return $this->xpath; - } + public function package(): DOMElement + { + return $this->dom()->documentElement; + } - function dom(): DOMDocument + public function createPackage() { - return $this->dom; - } + $package = $this->dom()->createElementNS('http://www.idpf.org/2007/opf', 'package'); + $package->setAttribute("version", '2.0'); + //$package->setAttribute("unique-identifier", 'BookId'); + $this->dom->appendChild($package); + } - function metaData(): ?DOMElement + public function createMetaData() { - $nodeList = $this->xpath()->query("*[local-name()='metadata']", $this->package()); + $this->metaData = $this->dom()->createElement('metadata'); + $this->package()->appendChild($this->metaData); + + $this->metaData->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:dc', $this->getDublinCoreNameSpace()); + $this->metaData->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:opf", "http://www.idpf.org/2007/opf"); + $this->metaData->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:calibre", "http://calibre.kovidgoyal.net/2009/metadata"); + $this->metaData->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); + $this->metaData->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:dcterms", "http://purl.org/dc/terms/"); + } - if ($nodeList->length) { - $this->metaData = $nodeList->item(0); - } else { - return null; - } + public function getDublinCoreNameSpace(): string + { + return $this->dublinCoreNameSpace; + } - foreach ($this->xpath()->query('namespace::*', $this->metaData) as $node) { + public function createManifest() + { + $this->manifest = $this->dom()->createElement('manifest'); + $this->package()->appendChild($this->manifest); + } - $prefix = $this->metaData->lookupPrefix($node->nodeValue); - $this->prefixes[$prefix] = $node->nodeValue; - } + public function createSpine() + { + $this->spine = $this->dom()->createElement('spine'); + $this->package()->appendChild($this->spine); - return $this->metaData; - } + $this->spine->setAttribute("toc", "ncx"); + } - public function package(): DOMElement + public function createGuide() { - return $this->dom()->documentElement; - } - - public function createPackage() - { - $package = $this->dom()->createElementNS('http://www.idpf.org/2007/opf', 'package'); - $package->setAttribute("version", '2.0'); - //$package->setAttribute("unique-identifier", 'BookId'); - $this->dom->appendChild($package); - } - - public function createMetaData() - { - $this->metaData = $this->dom()->createElement('metadata'); - $this->package()->appendChild($this->metaData); - - $this->metaData->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:dc', $this->getDublinCoreNameSpace()); - $this->metaData->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:opf", "http://www.idpf.org/2007/opf"); - $this->metaData->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:calibre", "http://calibre.kovidgoyal.net/2009/metadata"); - $this->metaData->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); - $this->metaData->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:dcterms", "http://purl.org/dc/terms/"); - } - - public function getDublinCoreNameSpace(): string + $guide = $this->dom()->createElement('guide'); + $this->package()->appendChild($guide); + } + + public function getNamespace($prefix) { - return $this->dublinCoreNameSpace; - } - - public function createManifest() - { - $this->manifest = $this->dom()->createElement('manifest'); - $this->package()->appendChild($this->manifest); - } - - public function createSpine() - { - $this->spine = $this->dom()->createElement('spine'); - $this->package()->appendChild($this->spine); - - $this->spine->setAttribute("toc", "ncx"); - } - - public function createGuide() - { - $guide = $this->dom()->createElement('guide'); - $this->package()->appendChild($guide); - } - - public function getNamespace($prefix) - { - return $this->getPrefixes()[$prefix]; - } - - public function getPrefixes(): array + return $this->getPrefixes()[$prefix]; + } + + public function getPrefixes(): array { - $this->metaData(); - return $this->prefixes; - } + $this->metaData(); + return $this->prefixes; + } - public function appendToMetaData($name, $value) - { - $meta = $this->dom->createElement('meta'); + public function appendToMetaData($name, $value) + { + $meta = $this->dom->createElement('meta'); - $meta->setAttribute('name', $name); - $meta->setAttribute('content', $value); + $meta->setAttribute('name', $name); + $meta->setAttribute('content', $value); - $this->metaData->appendChild($meta); - } + $this->metaData->appendChild($meta); + } - public function appendDublinCode($name, $value, $attributes = []) - { - $node = $this->dom->createElementNS($this->getDublinCoreNameSpace(), $name); + public function appendDublinCode($name, $value, $attributes = []) + { + $node = $this->dom->createElementNS($this->getDublinCoreNameSpace(), $name); - $node->appendChild($this->dom->createTextNode($value)); + $node->appendChild($this->dom->createTextNode($value)); - foreach ($attributes as $k => $v) { - $node->setAttribute($k, $v); - } + foreach ($attributes as $k => $v) { + $node->setAttribute($k, $v); + } - $this->metaData->appendChild($node); - } + $this->metaData->appendChild($node); + } - public function appendToManifest($id, $href, $mediaType) - { - $url = Url::fromString($href); + public function appendToManifest($id, $href, $mediaType) + { + $url = Url::fromString($href); - $item = $this->dom()->createElement('item'); - $item->setAttribute('id', $id); - $item->setAttribute('href', $url->urlencode()); - $item->setAttribute('media-type', $mediaType); - $this->manifest()->appendChild($item); - } + $item = $this->dom()->createElement('item'); + $item->setAttribute('id', $id); + $item->setAttribute('href', $url->urlencode()); + $item->setAttribute('media-type', $mediaType); + $this->manifest()->appendChild($item); + } - function manifest(): ?DOMElement + function manifest(): ?DOMElement { - $nodeList = $this->xpath()->query("*[local-name()='manifest']", $this->package()); + $nodeList = $this->xpath()->query("*[local-name()='manifest']", $this->package()); - if ($nodeList->length) { - $this->manifest = $nodeList->item(0); - } else { - return null; - } + if ($nodeList->length) { + $this->manifest = $nodeList->item(0); + } else { + return null; + } - return $this->manifest; - } + return $this->manifest; + } - public function appendToSpine($idref) - { - $itemref = $this->dom()->createElement('itemref'); - $itemref->setAttribute('idref', $idref); - $this->spine()->appendChild($itemref); - } + public function appendToSpine($idref) + { + $itemref = $this->dom()->createElement('itemref'); + $itemref->setAttribute('idref', $idref); + $this->spine()->appendChild($itemref); + } - function spine(): ?DOMElement + function spine(): ?DOMElement { - $nodeList = $this->xpath()->query("*[local-name()='spine']", $this->package()); + $nodeList = $this->xpath()->query("*[local-name()='spine']", $this->package()); - if ($nodeList->length) { - $this->spine = $nodeList->item(0); - } else { - return null; - } + if ($nodeList->length) { + $this->spine = $nodeList->item(0); + } else { + return null; + } - return $this->spine; - } + return $this->spine; + } - public function deleteDublinCoreByName($name) - { - foreach ($this->getDublinCoreByName($name) as $node) { - $node->parentNode->removeChild($node); - } - } + public function deleteDublinCoreByName($name) + { + foreach ($this->getDublinCoreByName($name) as $node) { + $node->parentNode->removeChild($node); + } + } - public function getDublinCoreByName($name): DOMNodeList + public function getDublinCoreByName($name): DOMNodeList { - return $this->xpath()->query("*[local-name()='" . $name . "']", $this->metaData()); - } + return $this->xpath()->query("*[local-name()='" . $name . "']", $this->metaData()); + } - public function getMetaDataByName($name): DOMNodeList + public function getMetaDataByName($name): DOMNodeList { - $query = '*[local-name()="meta"][@name="' . htmlspecialchars($name) . '"]'; - return $this->xpath()->query($query, $this->metaData()); - } + $query = '*[local-name()="meta"][@name="' . htmlspecialchars($name) . '"]'; + return $this->xpath()->query($query, $this->metaData()); + } - public function getMetaDataContentByName($name): ?string + public function getMetaDataContentByName($name): ?string { - $nodeList = $this->xpath()->query("*[local-name()='meta'][@name='" . $name . "'][@content]", $this->metaData()); - if ($nodeList->length) - return $nodeList->item(0)->getAttribute('content'); + $nodeList = $this->xpath()->query("*[local-name()='meta'][@name='" . $name . "'][@content]", $this->metaData()); + if ($nodeList->length) + return $nodeList->item(0)->getAttribute('content'); return null; - } + } - public function getDublinCoreValueByName($name): ?string + public function getDublinCoreValueByName($name): ?string { - $nodeList = $this->xpath()->query("*[local-name()='" . $name . "']", $this->metaData()); - if ($nodeList->length) - return $nodeList->item(0)->nodeValue; + $nodeList = $this->xpath()->query("*[local-name()='" . $name . "']", $this->metaData()); + if ($nodeList->length) + return $nodeList->item(0)->nodeValue; return null; - } + } - public function getManifestItemById($id): DOMNodeList + public function getManifestItemById($id): DOMNodeList { - return $this->xpath()->query("*[local-name()='item'][@id='" . $id . "']", $this->manifest()); - } + return $this->xpath()->query("*[local-name()='item'][@id='" . $id . "']", $this->manifest()); + } - public function getContent(): string + public function getContent(): string { - return $this->dom()->saveXML(); - } + return $this->dom()->saveXML(); + } } diff --git a/src/Section.php b/src/Section.php index 9f638ab..56534c6 100644 --- a/src/Section.php +++ b/src/Section.php @@ -11,399 +11,398 @@ class Section extends File { - public DOMDocument $dom; - public DOMXpath $xpath; - protected Epub $epub; - protected $linear = null; - protected $title_id = null; + public DOMDocument $dom; + public DOMXpath $xpath; + protected Epub $epub; + protected $linear = null; + protected $title_id = null; /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ function __construct(Epub $epub, $path = null) - { - parent::__construct($epub, $path); + { + parent::__construct($epub, $path); - if (!empty($path)) { - $html = $epub->zipFile->getEntryContents($path); + if (!empty($path)) { + $html = $epub->zipFile->getEntryContents($path); - $html = str_replace(' ', ' ', $html); + $html = str_replace(' ', ' ', $html); - $this->dom = new DOMDocument(); - $this->loadXML($html); - } else { - // создаем новый документ - $imp = new DOMImplementation; + $this->dom = new DOMDocument(); + $this->loadXML($html); + } else { + // создаем новый документ + $imp = new DOMImplementation; - $dtd = $imp->createDocumentType('html', '-//W3C//DTD XHTML 1.1//EN', - 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'); + $dtd = $imp->createDocumentType('html', '-//W3C//DTD XHTML 1.1//EN', + 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'); - $this->dom = $imp->createDocument(null, "html", $dtd); - $this->dom->encoding = "utf-8"; - $this->dom->documentElement->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml'); + $this->dom = $imp->createDocument(null, "html", $dtd); + $this->dom->encoding = "utf-8"; + $this->dom->documentElement->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml'); - $head = $this->dom->createElement('head'); + $head = $this->dom->createElement('head'); - $meta = $this->dom->createElement('meta'); - $meta->setAttribute('http-equiv', 'Content-Type'); - $meta->setAttribute('content', 'text/html; charset=utf-8'); - $head->appendChild($meta); + $meta = $this->dom->createElement('meta'); + $meta->setAttribute('http-equiv', 'Content-Type'); + $meta->setAttribute('content', 'text/html; charset=utf-8'); + $head->appendChild($meta); - $this->html()->appendChild($head); - $this->html()->appendChild($this->dom->createElement('body')); - } - } + $this->html()->appendChild($head); + $this->html()->appendChild($this->dom->createElement('body')); + } + } - public function loadXml($html) - { - $html = trim($html); - $html = str_replace(' ', ' ', $html); - $this->dom->loadXML($html); - } + public function loadXml($html) + { + $html = trim($html); + $html = str_replace(' ', ' ', $html); + $this->dom->loadXML($html); + } - public function html(): DOMElement + public function html(): DOMElement { - return $this->dom->documentElement; - } - - public function setBodyHtml($html) - { - $html = str_replace(' ', ' ', $html); - - if (empty($this->body())) { - $body = $this->dom()->createElement('body'); - $this->dom()->documentElement->appendChild($body); - } else { - $attributes = $this->body()->attributes; - $body = $this->dom()->createElement('body'); - $this->html()->replaceChild($body, $this->body()); - } - - if ($html == strip_tags($html)) - $html = '
' . $html . '
'; - /* - $dom = new \DOMDocument; - try { - $dom->loadXML('' . $html . ''); - } catch (\Exception $exception) { - $dom->loadHTML('' . $html . ''); - } - - if (is_object(libxml_get_last_error())) { - $dom->loadHTML('' . $html . ''); - } - */ - - $nodeList = $this->importXhtml($html); - - $this->clearBody(); - - while ($nodeList->length) { - $this->body()->appendChild($nodeList->item(0)); - } - /* - if ($dom->getElementsByTagName('body')->item(0)) { - $this->html()->replaceChild($this->dom()->importNode($dom->getElementsByTagName('body')->item(0), true), $body); - } else { - foreach ($dom->childNodes as $node) { - $body->appendChild($this->dom()->importNode($node, true)); - } - } - */ - - if (!empty($attributes)) { - foreach ($attributes as $attribute) { - $this->body()->setAttribute($attribute->nodeName, $attribute->value); - } - } - } - - public function body(): DOMElement + return $this->dom->documentElement; + } + + public function setBodyHtml($html) { - return $this->dom()->getElementsByTagName('body')->item(0); - } + $html = str_replace(' ', ' ', $html); + + if (empty($this->body())) { + $body = $this->dom()->createElement('body'); + $this->dom()->documentElement->appendChild($body); + } else { + $attributes = $this->body()->attributes; + $body = $this->dom()->createElement('body'); + $this->html()->replaceChild($body, $this->body()); + } + + if ($html == strip_tags($html)) + $html = '' . $html . '
'; + /* + $dom = new \DOMDocument; + try { + $dom->loadXML('' . $html . ''); + } catch (\Exception $exception) { + $dom->loadHTML('' . $html . ''); + } - public function dom(): DOMDocument + if (is_object(libxml_get_last_error())) { + $dom->loadHTML('' . $html . ''); + } + */ + + $nodeList = $this->importXhtml($html); + + $this->clearBody(); + + while ($nodeList->length) { + $this->body()->appendChild($nodeList->item(0)); + } + /* + if ($dom->getElementsByTagName('body')->item(0)) { + $this->html()->replaceChild($this->dom()->importNode($dom->getElementsByTagName('body')->item(0), true), $body); + } else { + foreach ($dom->childNodes as $node) { + $body->appendChild($this->dom()->importNode($node, true)); + } + } + */ + + if (!empty($attributes)) { + foreach ($attributes as $attribute) { + $this->body()->setAttribute($attribute->nodeName, $attribute->value); + } + } + } + + public function body(): DOMElement + { + return $this->dom()->getElementsByTagName('body')->item(0); + } + + public function dom(): DOMDocument { - return $this->dom; - } - - public function importXhtml($xhtml): DOMNodeList - { - $dom = new DOMDocument; - try { - $dom->loadXML('' . $xhtml . ''); - } catch (Exception $exception) { - $dom->loadHTML('' . $xhtml . ''); - } - - if (is_object(libxml_get_last_error())) { - $dom->loadHTML('' . $xhtml . ''); - } + return $this->dom; + } + + public function importXhtml($xhtml): DOMNodeList + { + $dom = new DOMDocument; + try { + $dom->loadXML('' . $xhtml . ''); + } catch (Exception $exception) { + $dom->loadHTML('' . $xhtml . ''); + } + + if (is_object(libxml_get_last_error())) { + $dom->loadHTML('' . $xhtml . ''); + } return $this->dom() ->importNode($dom->getElementsByTagName('body')->item(0), true) ->childNodes; - } + } - public function clearBody() - { - $childs = []; + public function clearBody() + { + $childs = []; - foreach ($this->body()->childNodes as $node) { - $childs[] = $node; - } + foreach ($this->body()->childNodes as $node) { + $childs[] = $node; + } - if (count($childs)) { - foreach ($childs as $node) { - $this->body()->removeChild($node); - } - } - } + if (count($childs)) { + foreach ($childs as $node) { + $this->body()->removeChild($node); + } + } + } - public function setPath($path) - { - $this->path = $path; - $this->epub->files[$path] = $this; - } + public function setPath($path) + { + $this->path = $path; + $this->epub->files[$path] = $this; + } - public function title(string $s = null): DOMElement - { - $titleNode = $this->dom()->getElementsByTagName('title')->item(0); + public function title(string $s = null): DOMElement + { + $titleNode = $this->dom()->getElementsByTagName('title')->item(0); - if (isset($s)) { - $oldTitleNode = $titleNode; + if (isset($s)) { + $oldTitleNode = $titleNode; - $titleNode = $this->dom()->createElement('title'); - $titleNode->appendChild($this->dom()->createTextNode($s)); + $titleNode = $this->dom()->createElement('title'); + $titleNode->appendChild($this->dom()->createTextNode($s)); - if (empty($oldTitleNode)) { + if (empty($oldTitleNode)) { - $this->head()->appendChild($titleNode); - } else { - $titleNode = $this->dom()->createElement('title'); - $titleNode->appendChild($this->dom()->createTextNode($s)); - $this->head()->replaceChild($titleNode, $oldTitleNode); - } - } + $this->head()->appendChild($titleNode); + } else { + $titleNode = $this->dom()->createElement('title'); + $titleNode->appendChild($this->dom()->createTextNode($s)); + $this->head()->replaceChild($titleNode, $oldTitleNode); + } + } - return $titleNode; - } + return $titleNode; + } - public function head(): DOMElement + public function head(): DOMElement { - $headNode = $this->dom()->getElementsByTagName('head')->item(0); + $headNode = $this->dom()->getElementsByTagName('head')->item(0); - if (empty($headNode)) { - $headNode = $this->dom()->createElement('head'); + if (empty($headNode)) { + $headNode = $this->dom()->createElement('head'); - $this->dom()->documentElement->appendChild($headNode); - } + $this->dom()->documentElement->appendChild($headNode); + } - return $headNode; - } + return $headNode; + } - public function getTitle(): string + public function getTitle(): string { - if ($this->epub->ncx()) { - $title = $this->epub->ncx()->findTitleByFullPath($this->getPath()); + if ($this->epub->ncx()) { + $title = $this->epub->ncx()->findTitleByFullPath($this->getPath()); - $title = $this->titleHandler($title); + $title = $this->titleHandler($title); - if ($title != '') { - return $title; - } - } + if ($title != '') { + return $title; + } + } - // пытаемся извлечь заголовок из тега h1 в body - $titleNode = $this->xpath()->query("//*[local-name()='body']//*[local-name()='h1'][@class='title']"); + // пытаемся извлечь заголовок из тега h1 в body + $titleNode = $this->xpath()->query("//*[local-name()='body']//*[local-name()='h1'][@class='title']"); - if ($titleNode->length) { - $titleNode = $titleNode->item(0); - $title = $this->titleHandler($titleNode->nodeValue); + if ($titleNode->length) { + $titleNode = $titleNode->item(0); + $title = $this->titleHandler($titleNode->nodeValue); - if ($title != '') { + if ($title != '') { - if ($id = $titleNode->getAttribute('id')) - $this->setTitleId($id); + if ($id = $titleNode->getAttribute('id')) + $this->setTitleId($id); - return $title; - } - } + return $title; + } + } - // пытаемся извлечь заголовок из первого найденного класса у которого класс начинается на title - $classElements = $this->xpath()->query("//*[local-name()='body']//*[@class]"); + // пытаемся извлечь заголовок из первого найденного класса у которого класс начинается на title + $classElements = $this->xpath()->query("//*[local-name()='body']//*[@class]"); - foreach ($classElements as $classElement) { + foreach ($classElements as $classElement) { - if (preg_match("/^title(\_\-[0-9])*$/iu", $classElement->getAttribute("class"))) { - $title = strip_tags($this->dom()->saveHTML($classElement)); - $title = $this->titleHandler($title); + if (preg_match("/^title(\_\-[0-9])*$/iu", $classElement->getAttribute("class"))) { + $title = strip_tags($this->dom()->saveHTML($classElement)); + $title = $this->titleHandler($title); - if ($title != '') { + if ($title != '') { - if ($id = $classElement->getAttribute('id')) - $this->setTitleId($id); + if ($id = $classElement->getAttribute('id')) + $this->setTitleId($id); - return $title; - } - } - } + return $title; + } + } + } - // пытаемся извлечь заголовок из тега h1 в body - $h1Elements = $this->xpath()->query("//*[local-name()='body']//*[local-name()='h1']"); + // пытаемся извлечь заголовок из тега h1 в body + $h1Elements = $this->xpath()->query("//*[local-name()='body']//*[local-name()='h1']"); - if ($h1Elements->length) { - $h1Element = $h1Elements->item(0); - $title = $this->titleHandler($h1Element->nodeValue); + if ($h1Elements->length) { + $h1Element = $h1Elements->item(0); + $title = $this->titleHandler($h1Element->nodeValue); - if ($title) { + if ($title) { - if (!empty($id = $h1Element->getAttribute('id'))) - $this->setTitleId($id); + if (!empty($id = $h1Element->getAttribute('id'))) + $this->setTitleId($id); - return $title; - } - } + return $title; + } + } - // пытаемся извлечь заголовок из тега title - $title = $this->xpath()->query("//*[local-name()='title']"); + // пытаемся извлечь заголовок из тега title + $title = $this->xpath()->query("//*[local-name()='title']"); - if ($title->length) { - $title = $title->item(0)->nodeValue; - $title = $this->titleHandler($title); - if ($title != '') { - return $title; - } - } + if ($title->length) { + $title = $title->item(0)->nodeValue; + $title = $this->titleHandler($title); + if ($title != '') { + return $title; + } + } - $title = $this->dom()->documentElement->nodeValue; + $title = $this->dom()->documentElement->nodeValue; - $title = $this->titleHandler($title); + $title = $this->titleHandler($title); - if (mb_strlen($title) > 40) - $title = trim(mb_substr($title, 0, 30)) . '...'; + if (mb_strlen($title) > 40) + $title = trim(mb_substr($title, 0, 30)) . '...'; - if ($title != '') { - return $title; - } + if ($title != '') { + return $title; + } - // если совсем никак не извлечь title то используем название файла + // если совсем никак не извлечь title то используем название файла - return $this->getBaseName(); - } + return $this->getBaseName(); + } - public function titleHandler($string): string - { - $string = (string)$string; - $string = html_entity_decode($string); + public function titleHandler($string): string + { + $string = (string)$string; + $string = html_entity_decode($string); - mb_substitute_character(0x20); - $string = mb_convert_encoding($string, "UTF-8", "auto"); - $string = mb_str_replace(chr(194) . chr(160), ' ', $string); + mb_substitute_character(0x20); + $string = mb_convert_encoding($string, "UTF-8", "auto"); + $string = mb_str_replace(chr(194) . chr(160), ' ', $string); - $string = preg_replace("/[[:space:]]/iu", " ", $string); - $string = preg_replace("/([[:space:]]{2,})/iu", " ", $string); + $string = preg_replace("/[[:space:]]/iu", " ", $string); + $string = preg_replace("/([[:space:]]{2,})/iu", " ", $string); return trim($string); - } + } - public function xpath(): DOMXpath + public function xpath(): DOMXpath { - $this->xpath = new DOMXpath($this->dom()); - return $this->xpath; - } + $this->xpath = new DOMXpath($this->dom()); + return $this->xpath; + } - public function getBodyContent(): string + public function getBodyContent(): string { - $content = ''; + $content = ''; - foreach ($this->body()->childNodes as $childNode) { - $str = $this->dom()->saveXML($childNode); - $str = str_replace(" ", ' ', $str); - $content .= trim($str); - } + foreach ($this->body()->childNodes as $childNode) { + $str = $this->dom()->saveXML($childNode); + $str = str_replace(" ", ' ', $str); + $content .= trim($str); + } - return $this->newLinesAndSpacesToOneSpace($content); - } + return $this->newLinesAndSpacesToOneSpace($content); + } - public function newLinesAndSpacesToOneSpace($content): string + public function newLinesAndSpacesToOneSpace($content): string { - return trim(preg_replace("/[[:space:]]+/iu", " ", $content)); - } + return trim(preg_replace("/[[:space:]]+/iu", " ", $content)); + } - public function getParentNavPointSrc(): ?string + public function getParentNavPointSrc(): ?string { - $navPoint = $this->epub->ncx()->findNavPointByBaseName($this->getBaseName()); + $navPoint = $this->epub->ncx()->findNavPointByBaseName($this->getBaseName()); - if (empty($navPoint)) - return null; + if (empty($navPoint)) + return null; - $parentNavPoint = $navPoint->xpath("parent::*")[0]->xpath("parent::*")[0]; + $parentNavPoint = $navPoint->xpath("parent::*")[0]->xpath("parent::*")[0]; - if (!empty($parentNavPoint)) { - $content = @$parentNavPoint->xpath("*[local-name()='content']")[0]; + if (!empty($parentNavPoint)) { + $content = @$parentNavPoint->xpath("*[local-name()='content']")[0]; - if (!empty($content)) { - return @basename($content->attributes()->src); - } - } + if (!empty($content)) { + return @basename($content->attributes()->src); + } + } return null; - } + } - public function setBodyId($id) - { - $this->body()->setAttribute('id', $id); - } + public function setBodyId($id) + { + $this->body()->setAttribute('id', $id); + } - public function getBodyId(): string + public function getBodyId(): string { - return $this->body()->getAttribute('id'); - } + return $this->body()->getAttribute('id'); + } - public function write() - { - $content = $this->dom->saveXml(); + public function write() + { + $content = $this->dom->saveXml(); - $this->setContent($content); - } + $this->setContent($content); + } - public function setContent($content) - { - parent::setContent($content); - } + public function setContent($content) + { + parent::setContent($content); + } - public function getContent($formatOutput = true): string + public function getContent($formatOutput = true): string { - $xml = $this->dom()->saveXml(); + $xml = $this->dom()->saveXml(); - if ($formatOutput) { - $dom = new DOMDocument('1.0'); - $dom->preserveWhiteSpace = false; - $dom->loadXML($xml); - $dom->formatOutput = true; + if ($formatOutput) { + $dom = new DOMDocument('1.0'); + $dom->preserveWhiteSpace = false; + $dom->loadXML($xml); + $dom->formatOutput = true; - return $dom->saveXML(); - } else { - return $xml; - } - } + return $dom->saveXML(); + } else { + return $xml; + } + } - public function prependBodyXhtml($xhtml): bool + public function prependBodyXhtml($xhtml): bool { - $firstSibling = $this->body()->firstChild; + $firstSibling = $this->body()->firstChild; - $importedNodeList = $this->importXhtml($xhtml); + $importedNodeList = $this->importXhtml($xhtml); - if ($importedNodeList->count()) + if ($importedNodeList->count()) foreach ($importedNodeList as $node) { $children[] = $node; } - else + else $children = []; - if (isset($children) and count($children)) - { + if (isset($children) and count($children)) { if (empty($firstSibling)) { foreach ($children as $node) { $this->body()->appendChild($node); @@ -415,26 +414,26 @@ public function prependBodyXhtml($xhtml): bool } } - return true; - } + return true; + } - public function getLinear() - { - return $this->linear; - } + public function getLinear() + { + return $this->linear; + } - public function setLinear($linear) - { - $this->linear = $linear; - } + public function setLinear($linear) + { + $this->linear = $linear; + } - public function getTitleId() - { - return $this->title_id; - } + public function getTitleId() + { + return $this->title_id; + } - public function setTitleId($title_id) - { - $this->title_id = $title_id; - } + public function setTitleId($title_id) + { + $this->title_id = $title_id; + } } diff --git a/src/UnifyImagesNames.php b/src/UnifyImagesNames.php index b2c7a13..a690154 100644 --- a/src/UnifyImagesNames.php +++ b/src/UnifyImagesNames.php @@ -18,55 +18,55 @@ class UnifyImagesNames private mixed $current_id; function __construct($epub) - { - $this->epub = &$epub; + { + $this->epub = &$epub; - $this->prefix = 'image_'; + $this->prefix = 'image_'; - $this->current_id = $this->getMaxId(); - } + $this->current_id = $this->getMaxId(); + } - public function getMaxId() - { - foreach ($this->epub->getImages() as $image) { - if (mb_substr(mb_strtolower($image->getFileName()), 0, strlen($this->prefix)) == $this->prefix) { + public function getMaxId() + { + foreach ($this->epub->getImages() as $image) { + if (mb_substr(mb_strtolower($image->getFileName()), 0, strlen($this->prefix)) == $this->prefix) { - $id = mb_substr(mb_strtolower($image->getFileName()), strlen($this->prefix)); + $id = mb_substr(mb_strtolower($image->getFileName()), strlen($this->prefix)); - if (is_numeric($id)) { - $ids[] = $id; - } - } - } + if (is_numeric($id)) { + $ids[] = $id; + } + } + } - return empty($ids) ? 0 : max($ids); - } + return empty($ids) ? 0 : max($ids); + } - public function createFileName(): string + public function createFileName(): string { - return $this->prefix . $this->current_id; - } + return $this->prefix . $this->current_id; + } - public function unify(): array + public function unify(): array { - $imagesNames = []; - $imagesForRename = []; + $imagesNames = []; + $imagesForRename = []; - foreach ($this->epub->getImages() as $image) { + foreach ($this->epub->getImages() as $image) { - if (in_array(mb_strtolower($image->getBaseName()), $imagesNames)) { - $imagesForRename[] = $image; - } else { - $imagesNames[] = mb_strtolower($image->getBaseName()); - } - } + if (in_array(mb_strtolower($image->getBaseName()), $imagesNames)) { + $imagesForRename[] = $image; + } else { + $imagesNames[] = mb_strtolower($image->getBaseName()); + } + } - foreach ($imagesForRename as $image) { - $this->current_id++; - $image->rename($this->prefix . $this->current_id . '.' . $image->getExtension()); - } + foreach ($imagesForRename as $image) { + $this->current_id++; + $image->rename($this->prefix . $this->current_id . '.' . $image->getExtension()); + } - return $imagesForRename; - } + return $imagesForRename; + } } diff --git a/src/UnifyTagIds.php b/src/UnifyTagIds.php index 698edfd..0773e2a 100644 --- a/src/UnifyTagIds.php +++ b/src/UnifyTagIds.php @@ -16,97 +16,97 @@ class UnifyTagIds private string $id_name; function __construct($epub, $prefix) - { - $this->epub = &$epub; - $this->id_name = $prefix . 'note-'; - } + { + $this->epub = &$epub; + $this->id_name = $prefix . 'note-'; + } - public function unify() - { - $idSectionArray = $this->getIdSectionsWithRepeatedIdsArray(); + public function unify() + { + $idSectionArray = $this->getIdSectionsWithRepeatedIdsArray(); - $current_id = $this->getMaxId(); + $current_id = $this->getMaxId(); - foreach ($idSectionArray as $id => $sections) { - $first_section = $sections[0]; + foreach ($idSectionArray as $id => $sections) { + $first_section = $sections[0]; - foreach ($sections as $c => $section) { - if ($c > 0) { + foreach ($sections as $c => $section) { + if ($c > 0) { - $current_id++; + $current_id++; - $id_name = $this->id_name . '' . $current_id; + $id_name = $this->id_name . '' . $current_id; - $nodes = $section->xpath()->query("//*[@id]"); + $nodes = $section->xpath()->query("//*[@id]"); - foreach ($nodes as $node) { + foreach ($nodes as $node) { - if ($node->getAttribute('id') == $id) { - $node->setAttribute('id', $id_name); - } - } + if ($node->getAttribute('id') == $id) { + $node->setAttribute('id', $id_name); + } + } - foreach ($this->epub->getSectionsList() as $section2) { + foreach ($this->epub->getSectionsList() as $section2) { - if ($first_section != $section2) { - $nodes = $section2->xpath()->query("//*[local-name()='a'][@href]"); + if ($first_section != $section2) { + $nodes = $section2->xpath()->query("//*[local-name()='a'][@href]"); - // находим все ссылки в главе + // находим все ссылки в главе - foreach ($nodes as $node) { + foreach ($nodes as $node) { - $href = $node->getAttribute('href'); + $href = $node->getAttribute('href'); - if (Url::fromString($href)->getFragment() == $id) { - $href = (string)Url::fromString($href) - ->withFragment($id_name); + if (Url::fromString($href)->getFragment() == $id) { + $href = (string)Url::fromString($href) + ->withFragment($id_name); - $node->setAttribute('href', $href); - } - } - } - } - } - } - } + $node->setAttribute('href', $href); + } + } + } + } + } + } + } - } + } - public function getIdSectionsWithRepeatedIdsArray(): array + public function getIdSectionsWithRepeatedIdsArray(): array { - $array = []; - - foreach ($this->epub->getSectionsList() as &$section) { - $nodes = $section->xpath()->query("//*[@id]"); - foreach ($nodes as $node) { - $array[$node->getAttribute('id')][] = &$section; - } - } - /* - $array2 = []; - foreach ($array as $id => $sections) { - if (count($sections) > 1) - $array2[$id] = $sections; - } - */ - return $array; - } - - public function getMaxId() - { - $array = array_keys($this->getIdSectionsWithRepeatedIdsArray()); - - foreach ($array as $id) { - if (mb_substr($id, 0, strlen($this->id_name)) == $this->id_name) { - - $id = mb_substr($id, strlen($this->id_name)); - - if (is_numeric($id)) { - $ids[] = $id; - } - } - } - - return empty($ids) ? 0 : max($ids); - } + $array = []; + + foreach ($this->epub->getSectionsList() as &$section) { + $nodes = $section->xpath()->query("//*[@id]"); + foreach ($nodes as $node) { + $array[$node->getAttribute('id')][] = &$section; + } + } + /* + $array2 = []; + foreach ($array as $id => $sections) { + if (count($sections) > 1) + $array2[$id] = $sections; + } + */ + return $array; + } + + public function getMaxId() + { + $array = array_keys($this->getIdSectionsWithRepeatedIdsArray()); + + foreach ($array as $id) { + if (mb_substr($id, 0, strlen($this->id_name)) == $this->id_name) { + + $id = mb_substr($id, strlen($this->id_name)); + + if (is_numeric($id)) { + $ids[] = $id; + } + } + } + + return empty($ids) ? 0 : max($ids); + } } diff --git a/tests/AddExtensionIfNotExistTest.php b/tests/AddExtensionIfNotExistTest.php index c327e42..e4805e7 100644 --- a/tests/AddExtensionIfNotExistTest.php +++ b/tests/AddExtensionIfNotExistTest.php @@ -4,20 +4,20 @@ class AddExtensionIfNotExistTest extends TestCase { - public function testInit() - { - $this->newEpub(); + public function testInit() + { + $this->newEpub(); - $this->addImage('image_2', 'Images/image_2', 'image/jpeg'); + $this->addImage('image_2', 'Images/image_2', 'image/jpeg'); - $this->addSection('section1.xhtml', ''); + $this->addSection('section1.xhtml', ''); - $this->getEpub()->addExtensionIfNotExist()->addExtension(); + $this->getEpub()->addExtensionIfNotExist()->addExtension(); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyContent()); + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyContent()); - $this->assertEquals('Images/image_2.jpeg', - $this->getEpub()->opf()->getManifestItemById('image_2.jpeg')->item(0)->getAttribute('href')); - } + $this->assertEquals('Images/image_2.jpeg', + $this->getEpub()->opf()->getManifestItemById('image_2.jpeg')->item(0)->getAttribute('href')); + } } diff --git a/tests/AddSectionsIdsTest.php b/tests/AddSectionsIdsTest.php index 8ae2913..1dcb361 100644 --- a/tests/AddSectionsIdsTest.php +++ b/tests/AddSectionsIdsTest.php @@ -4,69 +4,69 @@ class AddSectionsIdsTest extends TestCase { - public function testIfNoExists() - { - $this->newEpub(); + public function testIfNoExists() + { + $this->newEpub(); - $this->addSection('section1.xhtml', '123
'); - $this->addSection('section2.xhtml', '456
'); - $this->addSection('section3.xhtml', '789
'); + $this->addSection('section1.xhtml', '123
'); + $this->addSection('section2.xhtml', '456
'); + $this->addSection('section3.xhtml', '789
'); - $this->getEpub()->addSectionsIds()->init(); + $this->getEpub()->addSectionsIds()->init(); - $this->assertEquals('section-1', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyId()); - $this->assertEquals('section-2', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyId()); - $this->assertEquals('section-3', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section3.xhtml')->getBodyId()); - } + $this->assertEquals('section-1', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyId()); + $this->assertEquals('section-2', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyId()); + $this->assertEquals('section-3', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section3.xhtml')->getBodyId()); + } - public function testIfExists() - { - $this->newEpub(); + public function testIfExists() + { + $this->newEpub(); - $this->addSection('section1.xhtml', '123
'); - $this->addSection('section2.xhtml', '456
', 'some_id'); - $this->addSection('section3.xhtml', '789
'); + $this->addSection('section1.xhtml', '123
'); + $this->addSection('section2.xhtml', '456
', 'some_id'); + $this->addSection('section3.xhtml', '789
'); - $this->getEpub()->addSectionsIds()->init(); + $this->getEpub()->addSectionsIds()->init(); - $this->assertEquals('section-1', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyId()); - $this->assertEquals('section-2', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyId()); - $this->assertEquals('section-3', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section3.xhtml')->getBodyId()); - } + $this->assertEquals('section-1', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyId()); + $this->assertEquals('section-2', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyId()); + $this->assertEquals('section-3', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section3.xhtml')->getBodyId()); + } - public function testWithAnchor() - { - $this->newEpub(); + public function testWithAnchor() + { + $this->newEpub(); - $this->addSection('section1.xhtml', '123
'); - $this->addSection('section2.xhtml', ''); - $this->addSection('section3.xhtml', '789
', 'some_id'); + $this->addSection('section1.xhtml', '123
'); + $this->addSection('section2.xhtml', ''); + $this->addSection('section3.xhtml', '789
', 'some_id'); - $this->getEpub()->addSectionsIds()->init(); + $this->getEpub()->addSectionsIds()->init(); - $this->assertEquals('section-1', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyId()); - $this->assertEquals('section-2', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyId()); - $this->assertEquals('section-3', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section3.xhtml')->getBodyId()); + $this->assertEquals('section-1', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyId()); + $this->assertEquals('section-2', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyId()); + $this->assertEquals('section-3', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section3.xhtml')->getBodyId()); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyContent()); - } + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyContent()); + } - public function testWithEnexistedAnchor() - { - $this->newEpub(); + public function testWithEnexistedAnchor() + { + $this->newEpub(); - $this->addSection('section1.xhtml', '123
'); - $this->addSection('section2.xhtml', ''); - $this->addSection('section3.xhtml', '789
'); + $this->addSection('section1.xhtml', '123
'); + $this->addSection('section2.xhtml', ''); + $this->addSection('section3.xhtml', '789
'); - $this->getEpub()->addSectionsIds()->init(); + $this->getEpub()->addSectionsIds()->init(); - $this->assertEquals('section-1', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyId()); - $this->assertEquals('section-2', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyId()); - $this->assertEquals('section-3', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section3.xhtml')->getBodyId()); + $this->assertEquals('section-1', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyId()); + $this->assertEquals('section-2', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyId()); + $this->assertEquals('section-3', $this->getEpub()->getSectionByFilePath('OEBPS/Text/section3.xhtml')->getBodyId()); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyContent()); - } + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyContent()); + } } diff --git a/tests/ContainerXmlTest.php b/tests/ContainerXmlTest.php index 3e64409..f47ac34 100644 --- a/tests/ContainerXmlTest.php +++ b/tests/ContainerXmlTest.php @@ -12,28 +12,28 @@ class ContainerXmlTest extends TestCase * @throws \PhpZip\Exception\ZipException */ public function testOpen() - { - $epub = new Epub(); - $epub->setFile(__DIR__ . '/books/test.epub'); + { + $epub = new Epub(); + $epub->setFile(__DIR__ . '/books/test.epub'); - $container = $epub->container()->container(); + $container = $epub->container()->container(); - $this->assertEquals('urn:oasis:names:tc:opendocument:xmlns:container', $container->getAttribute('xmlns')); - $this->assertEquals('1.0', $container->getAttribute('version')); - } + $this->assertEquals('urn:oasis:names:tc:opendocument:xmlns:container', $container->getAttribute('xmlns')); + $this->assertEquals('1.0', $container->getAttribute('version')); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testAddRootFile() - { - $epub = new Epub(); + { + $epub = new Epub(); - $container = new Container($epub); - $container->appendRootFile('OEBPS/content.opf', 'application/oebps-package+xml'); + $container = new Container($epub); + $container->appendRootFile('OEBPS/content.opf', 'application/oebps-package+xml'); - $s = <<text
'); - $epub->opf()->appendToManifest('Section0001.xhtml', 'Text/Section0001.xhtml', 'application/xhtml+xml'); + $section = new Section($epub); + $section->setPath($epub->default_folder . '/Text/Section0001.xhtml'); + $section->setBodyHtml('text
'); + $epub->opf()->appendToManifest('Section0001.xhtml', 'Text/Section0001.xhtml', 'application/xhtml+xml'); - $epub->files[$epub->default_folder . '/Text/Section0001.xhtml'] = $section; + $epub->files[$epub->default_folder . '/Text/Section0001.xhtml'] = $section; - $section = new Section($epub); - $section->setPath($epub->default_folder . '/Text/Section0002.xhtml'); - $section->setBodyHtml('text
'); - $epub->opf()->appendToManifest('Section0002.xhtml', 'Text/Section0002.xhtml', 'application/xhtml+xml'); + $section = new Section($epub); + $section->setPath($epub->default_folder . '/Text/Section0002.xhtml'); + $section->setBodyHtml('text
'); + $epub->opf()->appendToManifest('Section0002.xhtml', 'Text/Section0002.xhtml', 'application/xhtml+xml'); - $epub->files[$epub->default_folder . '/Text/Section0002.xhtml'] = $section; + $epub->files[$epub->default_folder . '/Text/Section0002.xhtml'] = $section; - $epub->opf()->appendToSpine('Section0002.xhtml'); - $epub->opf()->appendToSpine('Section0001.xhtml'); + $epub->opf()->appendToSpine('Section0002.xhtml'); + $epub->opf()->appendToSpine('Section0001.xhtml'); - $s = <<123
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section0001.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml('123
'); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section0002.xhtml'); - $section->setBodyId('body_id'); - $section->setBodyHtml('123
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section0002.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml('123
'); - $parent = $ncx->appendNavMap('test', 'Text/Section0001.xhtml', '123', 3); + $parent = $ncx->appendNavMap('test', 'Text/Section0001.xhtml', '123', 3); - $this->assertEquals('test', $epub->ncx()->getTextById('123')); - $this->assertEquals('OEBPS/Text/Section0001.xhtml', $epub->ncx()->getFileById('123')->getPath()); + $this->assertEquals('test', $epub->ncx()->getTextById('123')); + $this->assertEquals('OEBPS/Text/Section0001.xhtml', $epub->ncx()->getFileById('123')->getPath()); - $ncx->appendNavMap('test2', 'Text/Section0002.xhtml', '124', 4, $parent); + $ncx->appendNavMap('test2', 'Text/Section0002.xhtml', '124', 4, $parent); - $this->assertEquals('test2', $epub->ncx()->getTextById('124')); - $this->assertEquals('OEBPS/Text/Section0002.xhtml', $epub->ncx()->getFileById('124')->getPath()); + $this->assertEquals('test2', $epub->ncx()->getTextById('124')); + $this->assertEquals('OEBPS/Text/Section0002.xhtml', $epub->ncx()->getFileById('124')->getPath()); - $navPoint = $ncx->getNavPoints($epub->ncx()->navmap())->item(0); + $navPoint = $ncx->getNavPoints($epub->ncx()->navmap())->item(0); - $this->assertEquals('123', $navPoint->getAttribute('id')); + $this->assertEquals('123', $navPoint->getAttribute('id')); - $navPoint2 = $ncx->getNavPoints($navPoint)->item(0); + $navPoint2 = $ncx->getNavPoints($navPoint)->item(0); - $this->assertEquals('124', $navPoint2->getAttribute('id')); - } + $this->assertEquals('124', $navPoint2->getAttribute('id')); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testFindTitleByFullPath() - { - $epub = new Epub(); + { + $epub = new Epub(); - $ncx = new Ncx($epub); - $ncx->setPath($epub->default_ncx_path); + $ncx = new Ncx($epub); + $ncx->setPath($epub->default_ncx_path); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section0001.xhtml'); - $section->setBodyId('body_id'); - $section->setBodyHtml('123
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section0001.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml('123
'); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section0002.xhtml'); - $section->setBodyId('body_id'); - $section->setBodyHtml('123
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section0002.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml('123
'); - $parent = $ncx->appendNavMap('test', 'Text/Section0001.xhtml', '123', 3); - $ncx->appendNavMap('test2', 'Text/Section0002.xhtml', '124', 4, $parent); + $parent = $ncx->appendNavMap('test', 'Text/Section0001.xhtml', '123', 3); + $ncx->appendNavMap('test2', 'Text/Section0002.xhtml', '124', 4, $parent); - $this->assertEquals('test', $ncx->findTitleByFullPath('OEBPS/Text/Section0001.xhtml')); - $this->assertEquals('test2', $ncx->findTitleByFullPath('OEBPS/Text/Section0002.xhtml')); - } + $this->assertEquals('test', $ncx->findTitleByFullPath('OEBPS/Text/Section0001.xhtml')); + $this->assertEquals('test2', $ncx->findTitleByFullPath('OEBPS/Text/Section0002.xhtml')); + } /** * @throws \PhpZip\Exception\ZipException @@ -208,43 +208,43 @@ public function testFindTitleByFullPath() * @throws \PhpZip\Exception\ZipEntryNotFoundException */ public function testSaveOpenNcxExist() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $epub->createNcx(); + $epub->createNcx(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section0001.xhtml'); - $section->setBodyId('body_id'); - $section->setBodyHtml('123
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section0001.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml('123
'); - $epub->ncx()->appendNavMap('test & — test', 'Text/Section0001.xhtml', '123', 3); + $epub->ncx()->appendNavMap('test & — test', 'Text/Section0001.xhtml', '123', 3); - $this->assertInstanceOf(Ncx::class, $epub->getFileByPath('OEBPS/toc.ncx')); + $this->assertInstanceOf(Ncx::class, $epub->getFileByPath('OEBPS/toc.ncx')); - $string = $epub->outputAsString(); + $string = $epub->outputAsString(); - $epub = new Epub(); - $epub->setFile($string); + $epub = new Epub(); + $epub->setFile($string); - $this->assertTrue($epub->zipFile->hasEntry('OEBPS/toc.ncx')); + $this->assertTrue($epub->zipFile->hasEntry('OEBPS/toc.ncx')); - $this->assertContains('OEBPS/toc.ncx', $epub->getAllFilesList()); - } + $this->assertContains('OEBPS/toc.ncx', $epub->getAllFilesList()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testCreateNew() - { - $epub = $this->newEpub(); - $epub->createContainer(); - $epub->createNcx(); + { + $epub = $this->newEpub(); + $epub->createContainer(); + $epub->createNcx(); - $this->assertInstanceOf(Container::class, $epub->container); - $this->assertInstanceOf(Ncx::class, $epub->ncx); - } + $this->assertInstanceOf(Container::class, $epub->container); + $this->assertInstanceOf(Ncx::class, $epub->ncx); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException @@ -267,23 +267,23 @@ public function testCreateNew() * @throws \PhpZip\Exception\ZipException */ public function testFileInAnotherFolder() - { - $epub = $this->newEpub(); - $epub->createContainer(); - $epub->createOpf(); - $epub->createNcx('OEBPS/ncx/toc.ncx'); + { + $epub = $this->newEpub(); + $epub->createContainer(); + $epub->createOpf(); + $epub->createNcx('OEBPS/ncx/toc.ncx'); - $this->assertEquals('123
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section0001.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml('123
'); - $epub->ncx()->appendNavMap('Текст & — ', 'Text/&Section0001.xhtml', '123', 3); + $epub->ncx()->appendNavMap('Текст & — ', 'Text/&Section0001.xhtml', '123', 3); - $xml = <<текст
'); + $section = new Section($epub); + $section->setPath('OEBPS/Тексты/Файл.xhtml'); + $section->setBodyHtml('текст
'); - $epub->opf()->appendToManifest('Файл.xhtml', 'Тексты/Файл.xhtml', 'application/xhtml+xml'); + $epub->opf()->appendToManifest('Файл.xhtml', 'Тексты/Файл.xhtml', 'application/xhtml+xml'); - $this->assertEquals('Porro hic libero note dolorem. Dolor note quia impedit et corrupti. Laborum quos sit facere ut at illum. Nobis accusantium libero sit eos. Sunt quia nulla quibusdam dolores. Mollitia dolorum quisquam voluptatum aperiam. Aut voluptatum accusantium alias voluptatem rerum quis illo et. Reiciendis ab minima aut suscipit. Mollitia velit eligendi quidem est. Facere rerum qui ut recusandae explicabo temporibus. Animi aut architecto eos rerum aut. Amet est explicabo minima nulla. Consequatur esse voluptatem vel voluptatem. Molestiae ad omnis magni amet. Aliquam voluptates odit dolorem praesentium nulla ullam. Totam consectetur cupiditate laborum sequi esse. Exercitationem velit dolores ut natus accusamus. Non nulla error voluptatum qui eum nam. Voluptate fuga facere odio autem maiores.
', $section->getBodyContent()); - } + $this->assertInstanceOf(DOMNode::class, $section->head()); + $this->assertEquals('', $section->title()->nodeValue); + $this->assertEquals('Porro hic libero note dolorem. Dolor note quia impedit et corrupti. Laborum quos sit facere ut at illum. Nobis accusantium libero sit eos. Sunt quia nulla quibusdam dolores. Mollitia dolorum quisquam voluptatum aperiam. Aut voluptatum accusantium alias voluptatem rerum quis illo et. Reiciendis ab minima aut suscipit. Mollitia velit eligendi quidem est. Facere rerum qui ut recusandae explicabo temporibus. Animi aut architecto eos rerum aut. Amet est explicabo minima nulla. Consequatur esse voluptatem vel voluptatem. Molestiae ad omnis magni amet. Aliquam voluptates odit dolorem praesentium nulla ullam. Totam consectetur cupiditate laborum sequi esse. Exercitationem velit dolores ut natus accusamus. Non nulla error voluptatum qui eum nam. Voluptate fuga facere odio autem maiores.
', $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testNewSection() - { - $epub = new Epub(); + { + $epub = new Epub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyHtml('123
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyHtml('123
'); - $s = <<123
', $section->getBodyContent()); - } + $this->assertEquals($s, $section->getContent()); + $this->assertEquals('123
', $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testSetBodyId() - { - $epub = new Epub(); + { + $epub = new Epub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyId('body_id'); - $section->setBodyHtml('123
'); - $this->assertEquals('body_id', $section->body()->getAttribute('id')); - } + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml('123
'); + $this->assertEquals('body_id', $section->body()->getAttribute('id')); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testGetBodyId() - { - $epub = new Epub(); + { + $epub = new Epub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyId('body_id'); - $section->setBodyHtml('123
'); - $this->assertEquals('body_id', $section->getBodyId()); - } + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml('123
'); + $this->assertEquals('body_id', $section->getBodyId()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testNbspEntities() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $html = 'text & text
'; + $html = 'text & text
'; - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyId('body_id'); - $section->setBodyHtml($html); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml($html); - $this->assertEquals('text & text
', - $section->getBodyContent()); - } + $this->assertEquals('text & text
', + $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testCariage() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyId('body_id'); - $section->setBodyHtml("test \r test \r test"); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml("test \r test \r test"); - $this->assertEquals('test test test
', - $section->getBodyContent()); - } + $this->assertEquals('test test test
', + $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testSvg() - { - $html = <<текст
', $section->getBodyContent()); - $this->assertInstanceOf(Section::class, $epub->getFileByPath('OEBPS/Text/Section1.xhtml')); + $this->assertEquals('текст
', $section->getBodyContent()); + $this->assertInstanceOf(Section::class, $epub->getFileByPath('OEBPS/Text/Section1.xhtml')); - $string = $epub->outputAsString(); + $string = $epub->outputAsString(); - $epub = new Epub(); - $epub->setFile($string); + $epub = new Epub(); + $epub->setFile($string); - $this->assertInstanceOf(Section::class, $epub->getFileByPath('OEBPS/Text/Section1.xhtml')); - $this->assertEquals('текст
', $epub->getFileByPath('OEBPS/Text/Section1.xhtml')->getBodyContent()); - } + $this->assertInstanceOf(Section::class, $epub->getFileByPath('OEBPS/Text/Section1.xhtml')); + $this->assertEquals('текст
', $epub->getFileByPath('OEBPS/Text/Section1.xhtml')->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipException @@ -290,186 +290,186 @@ public function testLoadXmlGetBodyContent() * @throws \PhpZip\Exception\ZipEntryNotFoundException */ public function testSetBodyXmlGetBodyContent() - { - $html = 'текст
'; + { + $html = 'текст
'; - $epub = $this->newEpub(); + $epub = $this->newEpub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyHtml($html); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyHtml($html); - $epub->opf()->appendToManifest('Section1.xhtml', 'Text/Section1.xhtml', 'application/xhtml+xml'); - $epub->opf()->appendToSpine('Section1.xhtml'); + $epub->opf()->appendToManifest('Section1.xhtml', 'Text/Section1.xhtml', 'application/xhtml+xml'); + $epub->opf()->appendToSpine('Section1.xhtml'); - $this->assertEquals($html, $section->getBodyContent()); - $this->assertInstanceOf(Section::class, $epub->getFileByPath('OEBPS/Text/Section1.xhtml')); + $this->assertEquals($html, $section->getBodyContent()); + $this->assertInstanceOf(Section::class, $epub->getFileByPath('OEBPS/Text/Section1.xhtml')); - $string = $epub->outputAsString(); + $string = $epub->outputAsString(); - $epub = new Epub(); - $epub->setFile($string); + $epub = new Epub(); + $epub->setFile($string); - $this->assertInstanceOf(Section::class, $epub->getFileByPath('OEBPS/Text/Section1.xhtml')); - $this->assertEquals($html, $epub->getFileByPath('OEBPS/Text/Section1.xhtml')->getBodyContent()); - } + $this->assertInstanceOf(Section::class, $epub->getFileByPath('OEBPS/Text/Section1.xhtml')); + $this->assertEquals($html, $epub->getFileByPath('OEBPS/Text/Section1.xhtml')->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testSetBodyHtmlTagsWithoutParent() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyId('body_id'); - $section->setBodyHtml('старый текст
'); - $section->setBodyHtml('текст текст
текст текст
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyId('body_id'); + $section->setBodyHtml('старый текст
'); + $section->setBodyHtml('текст текст
текст текст
'); - $this->assertEquals('текст текст
текст текст
', $section->getBodyContent()); - $this->assertEquals('body_id', $section->getBodyId()); - } + $this->assertEquals('текст текст
текст текст
', $section->getBodyContent()); + $this->assertEquals('body_id', $section->getBodyId()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testFixHtmlSelfClosedTags() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - libxml_use_internal_errors(false); + libxml_use_internal_errors(false); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyHtml('текст текст
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyHtml('текст текст
'); - $this->assertEquals('текст текст
', - $section->getBodyContent()); + $this->assertEquals('текст текст
', + $section->getBodyContent()); - libxml_use_internal_errors(true); + libxml_use_internal_errors(true); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyHtml('текст текст
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyHtml('текст текст
'); - $this->assertEquals('текст текст
', - $section->getBodyContent()); - } + $this->assertEquals('текст текст
', + $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testDoubleQuote() - { - $xhtml = '&<>"\'
'; + { + $xhtml = '&<>"\'
'; - $epub = $this->newEpub(); + $epub = $this->newEpub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyHtml($xhtml); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyHtml($xhtml); - $this->assertEquals('&<>"\'
', $section->getBodyContent()); - } + $this->assertEquals('&<>"\'
', $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testFormatOutput() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyHtml('текст текст
текст2 текст2
'); + $nodes = $section->importXhtml('текст текст
текст2 текст2
'); - $this->assertEquals('текст текст
', $section->dom()->saveXML($nodes->item(0))); - $this->assertEquals('текст2 текст2
', $section->dom()->saveXML($nodes->item(1))); - } + $this->assertEquals('текст текст
', $section->dom()->saveXML($nodes->item(0))); + $this->assertEquals('текст2 текст2
', $section->dom()->saveXML($nodes->item(1))); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testClearBody() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $html = <<123
456
", $section->getBodyContent()); + $this->assertEquals("123
456
", $section->getBodyContent()); - $section->clearBody(); + $section->clearBody(); - $this->assertEquals("", $section->getBodyContent()); - } + $this->assertEquals("", $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testPrependBodyXhtml() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->prependBodyXhtml('текст
'); + $section->prependBodyXhtml('текст
'); - $this->assertEquals('текст
', $section->getBodyContent()); + $this->assertEquals('текст
', $section->getBodyContent()); - $section->prependBodyXhtml('текст2
'); + $section->prependBodyXhtml('текст2
'); - $this->assertEquals('текст2
текст
', $section->getBodyContent()); + $this->assertEquals('текст2
текст
', $section->getBodyContent()); - $section->prependBodyXhtml('текст4
текст3
'); + $section->prependBodyXhtml('текст4
текст3
'); - $this->assertEquals('текст4
текст3
текст2
текст
', $section->getBodyContent()); - } + $this->assertEquals('текст4
текст3
текст2
текст
', $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testGetTitleV1() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyHtml('текст
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyHtml('текст
'); - $this->assertEquals('заголовок', $section->getTitle()); - $this->assertEquals('текст
', $section->getBodyContent()); - } + $this->assertEquals('заголовок', $section->getTitle()); + $this->assertEquals('текст
', $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testGetTitleV2() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyHtml('заголовок
текст
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyHtml('заголовок
текст
'); - $this->assertEquals('заголовок', $section->getTitle()); - $this->assertEquals('заголовок
текст
', $section->getBodyContent()); - } + $this->assertEquals('заголовок', $section->getTitle()); + $this->assertEquals('заголовок
текст
', $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testGetTitleV3() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyHtml('текст
'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyHtml('текст
'); - $this->assertEquals('заголовок', $section->getTitle()); - $this->assertEquals('текст
', $section->getBodyContent()); - } + $this->assertEquals('заголовок', $section->getTitle()); + $this->assertEquals('текст
', $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testGetTitleV4() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $xhtml = 'текст текст текст текст текст текст текст текст текст текст текст текст текст текст текст текст текст
'; + $xhtml = 'текст текст текст текст текст текст текст текст текст текст текст текст текст текст текст текст текст
'; - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); - $section->setBodyHtml($xhtml); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); + $section->setBodyHtml($xhtml); - $this->assertEquals('текст текст текст текст текст...', - $section->getTitle()); + $this->assertEquals('текст текст текст текст текст...', + $section->getTitle()); - $this->assertEquals($xhtml, - $section->getBodyContent()); - } + $this->assertEquals($xhtml, + $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testGetTitleV5() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $section = new Section($epub); - $section->setPath('OEBPS/Text/Section1.xhtml'); + $section = new Section($epub); + $section->setPath('OEBPS/Text/Section1.xhtml'); - $this->assertEquals('Section1.xhtml', - $section->getTitle()); + $this->assertEquals('Section1.xhtml', + $section->getTitle()); - $this->assertEquals('', - $section->getBodyContent()); - } + $this->assertEquals('', + $section->getBodyContent()); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testTitleHandler() - { - $epub = $this->newEpub(); + { + $epub = $this->newEpub(); - $section = new Section($epub); + $section = new Section($epub); - $this->assertEquals('Текст текст', $section->titleHandler('Текст текст')); - $this->assertEquals('Текст & текст', $section->titleHandler('Текст & текст')); - $this->assertEquals('Текст текст текст', $section->titleHandler("Текст \r\n\r\n\r\n текст \n\n текст")); - $this->assertEquals('Текст', $section->titleHandler(" Текст ")); - $this->assertEquals('текст', $section->titleHandler("текст")); + $this->assertEquals('Текст текст', $section->titleHandler('Текст текст')); + $this->assertEquals('Текст & текст', $section->titleHandler('Текст & текст')); + $this->assertEquals('Текст текст текст', $section->titleHandler("Текст \r\n\r\n\r\n текст \n\n текст")); + $this->assertEquals('Текст', $section->titleHandler(" Текст ")); + $this->assertEquals('текст', $section->titleHandler("текст")); - $title = 'текст текст'; // asc 194 - $this->assertStringContainsString(chr(194), $title); - $title = $section->titleHandler($title); - $this->assertStringNotContainsString(chr(194), $title); - } + $title = 'текст текст'; // asc 194 + $this->assertStringContainsString(chr(194), $title); + $title = $section->titleHandler($title); + $this->assertStringNotContainsString(chr(194), $title); + } /** * @throws \PhpZip\Exception\ZipException */ public function testIsLinear() - { - $epub = new Epub(); - $epub->setFile(__DIR__ . '/books/test_with_linear_no.epub'); + { + $epub = new Epub(); + $epub->setFile(__DIR__ . '/books/test_with_linear_no.epub'); - $epub->getSectionsList(); + $epub->getSectionsList(); - $this->assertEquals(null, $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->getLinear()); - $this->assertEquals('no', $epub->getFileByPath('OEBPS/Text/Section0002.xhtml')->getLinear()); - } + $this->assertEquals(null, $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->getLinear()); + $this->assertEquals('no', $epub->getFileByPath('OEBPS/Text/Section0002.xhtml')->getLinear()); + } /** * @throws \PhpZip\Exception\ZipException */ public function testGetTitleId() - { - $epub = new Epub(); - $epub->setFile(__DIR__ . '/books/test_header_anchor.epub'); + { + $epub = new Epub(); + $epub->setFile(__DIR__ . '/books/test_header_anchor.epub'); - $epub->getSectionsList(); + $epub->getSectionsList(); - $this->assertEquals('Глава 1', $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->getTitle()); - $this->assertEquals('header1', $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->getTitleId()); - $this->assertEquals('текст первой главы сноска
', - $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->getBodyContent()); + $this->assertEquals('Глава 1', $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->getTitle()); + $this->assertEquals('header1', $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->getTitleId()); + $this->assertEquals('текст первой главы сноска
', + $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->getBodyContent()); - $this->assertEquals('Глава 2', $epub->getFileByPath('OEBPS/Text/Section0002.xhtml')->getTitle()); - $this->assertEquals('header2', $epub->getFileByPath('OEBPS/Text/Section0002.xhtml')->getTitleId()); - $this->assertEquals('текст второй главы сноска
', - $epub->getFileByPath('OEBPS/Text/Section0002.xhtml')->getBodyContent()); + $this->assertEquals('Глава 2', $epub->getFileByPath('OEBPS/Text/Section0002.xhtml')->getTitle()); + $this->assertEquals('header2', $epub->getFileByPath('OEBPS/Text/Section0002.xhtml')->getTitleId()); + $this->assertEquals('текст второй главы сноска
', + $epub->getFileByPath('OEBPS/Text/Section0002.xhtml')->getBodyContent()); - $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->setTitleId('test'); + $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->setTitleId('test'); - $this->assertEquals('test', $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->getTitleId()); - } + $this->assertEquals('test', $epub->getFileByPath('OEBPS/Text/Section0001.xhtml')->getTitleId()); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index c977165..8c5d8c3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -12,7 +12,7 @@ abstract class TestCase extends PhpUnitTestCase { - private Epub $epub; + private Epub $epub; /** * @throws \PhpZip\Exception\ZipEntryNotFoundException @@ -20,64 +20,64 @@ abstract class TestCase extends PhpUnitTestCase */ public function newEpub(): Epub { - $this->epub = new Epub(); - $this->epub->createContainer(); - $this->epub->createOpf(); + $this->epub = new Epub(); + $this->epub->createContainer(); + $this->epub->createOpf(); - return $this->epub; - } + return $this->epub; + } - public function getEpub() - { - return $this->epub; - } + public function getEpub() + { + return $this->epub; + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function addSection($name, $text, $body_id = null) - { - $section = new Section($this->epub); - $section->setPath($this->epub->default_folder . '/Text/' . $name); + { + $section = new Section($this->epub); + $section->setPath($this->epub->default_folder . '/Text/' . $name); - if (!empty($text)) - $section->setBodyHtml($text); + if (!empty($text)) + $section->setBodyHtml($text); - if (!empty($body_id)) - $section->setBodyId($body_id); + if (!empty($body_id)) + $section->setBodyId($body_id); - $this->epub->opf()->appendToManifest($name, 'Text/' . $name, 'application/xhtml+xml'); - $this->epub->opf()->appendToSpine($name); - } + $this->epub->opf()->appendToManifest($name, 'Text/' . $name, 'application/xhtml+xml'); + $this->epub->opf()->appendToSpine($name); + } + + public function addImage($name, $path, $mimeType, $content = null) + { + if (empty($content)) + $content = $this->mockImage()->getImageBlob(); + + $image = new Image($this->epub); + $image->setPath($this->epub->default_folder . '/' . $path); + $image->setContent($content); + $this->epub->opf()->appendToManifest($name, $path, $mimeType); + } + + public function mockImage($width = 320, $height = 240): Imagick + { + $image = new Imagick(); + $image->newImage($width, $height, new ImagickPixel('green')); + $image->setImageFormat('jpeg'); - public function addImage($name, $path, $mimeType, $content = null) - { - if (empty($content)) - $content = $this->mockImage()->getImageBlob(); + $draw = new ImagickDraw(); + $draw->setFillColor(new ImagickPixel('gray')); + $draw->setFontSize(12); + $image->annotateImage($draw, 10, 45, 0, 'The quick brown fox jumps over the lazy dog'); - $image = new Image($this->epub); - $image->setPath($this->epub->default_folder . '/' . $path); - $image->setContent($content); - $this->epub->opf()->appendToManifest($name, $path, $mimeType); - } + return $image; + } - public function mockImage($width = 320, $height = 240): Imagick + public function nl($s) { - $image = new Imagick(); - $image->newImage($width, $height, new ImagickPixel('green')); - $image->setImageFormat('jpeg'); - - $draw = new ImagickDraw(); - $draw->setFillColor(new ImagickPixel('gray')); - $draw->setFontSize(12); - $image->annotateImage($draw, 10, 45, 0, 'The quick brown fox jumps over the lazy dog'); - - return $image; - } - - public function nl($s) - { - return str_replace("\n", "\r\n", $s); - } + return str_replace("\n", "\r\n", $s); + } } diff --git a/tests/UnifyImageNamesTest.php b/tests/UnifyImageNamesTest.php index 42c1b7d..9e8bebc 100644 --- a/tests/UnifyImageNamesTest.php +++ b/tests/UnifyImageNamesTest.php @@ -9,71 +9,71 @@ class UnifyImageNamesTest extends TestCase * @throws \PhpZip\Exception\ZipException */ public function testRename() - { - $this->newEpub(); + { + $this->newEpub(); - $this->addImage('image_2.jpg', 'Images/image_2.jpg', 'image/png'); + $this->addImage('image_2.jpg', 'Images/image_2.jpg', 'image/png'); - $this->addImage('image_2.jpg', 'Images2/image_2.jpg', 'image/png'); + $this->addImage('image_2.jpg', 'Images2/image_2.jpg', 'image/png'); - $this->addSection('section1.xhtml', ''); + $this->addSection('section1.xhtml', ''); - $this->addSection('section2.xhtml', ''); + $this->addSection('section2.xhtml', ''); - $this->assertEquals(2, $this->getEpub()->unifyImagesNames()->getMaxId()); + $this->assertEquals(2, $this->getEpub()->unifyImagesNames()->getMaxId()); - $this->getEpub()->unifyImagesNames()->unify(); + $this->getEpub()->unifyImagesNames()->unify(); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyContent()); + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyContent()); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyContent()); + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyContent()); - $images = $this->getEpub()->getImages(); + $images = $this->getEpub()->getImages(); - $this->assertEquals('image_3.jpg', - $images['OEBPS/Images2/image_3.jpg']->getBaseName()); + $this->assertEquals('image_3.jpg', + $images['OEBPS/Images2/image_3.jpg']->getBaseName()); - $this->assertEquals('image_2.jpg', - $images['OEBPS/Images/image_2.jpg']->getBaseName()); + $this->assertEquals('image_2.jpg', + $images['OEBPS/Images/image_2.jpg']->getBaseName()); - $this->assertEquals('Images/image_2.jpg', $this->getEpub()->opf()->getManifestItemById('image_2.jpg')->item(0)->getAttribute('href')); - $this->assertEquals('Images2/image_3.jpg', $this->getEpub()->opf()->getManifestItemById('image_3.jpg')->item(0)->getAttribute('href')); - } + $this->assertEquals('Images/image_2.jpg', $this->getEpub()->opf()->getManifestItemById('image_2.jpg')->item(0)->getAttribute('href')); + $this->assertEquals('Images2/image_3.jpg', $this->getEpub()->opf()->getManifestItemById('image_3.jpg')->item(0)->getAttribute('href')); + } /** * @throws \PhpZip\Exception\ZipEntryNotFoundException * @throws \PhpZip\Exception\ZipException */ public function testRenameWithCaseInsensitive() - { - $this->newEpub(); + { + $this->newEpub(); - $this->addImage('image_2.jpg', 'Images/image_2.jpg', 'image/png'); - $this->addImage('image_2.JPEG', 'Images2/image_2.JPG', 'image/png'); + $this->addImage('image_2.jpg', 'Images/image_2.jpg', 'image/png'); + $this->addImage('image_2.JPEG', 'Images2/image_2.JPG', 'image/png'); - $this->addSection('section1.xhtml', ''); - $this->addSection('section2.xhtml', ''); + $this->addSection('section1.xhtml', ''); + $this->addSection('section2.xhtml', ''); - $this->assertEquals(2, $this->getEpub()->unifyImagesNames()->getMaxId()); - $this->getEpub()->unifyImagesNames()->unify(); + $this->assertEquals(2, $this->getEpub()->unifyImagesNames()->getMaxId()); + $this->getEpub()->unifyImagesNames()->unify(); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyContent()); + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/section1.xhtml')->getBodyContent()); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyContent()); + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/section2.xhtml')->getBodyContent()); - $images = $this->getEpub()->getImages(); + $images = $this->getEpub()->getImages(); - $this->assertEquals('image_3.JPG', - $images['OEBPS/Images2/image_3.JPG']->getBaseName()); + $this->assertEquals('image_3.JPG', + $images['OEBPS/Images2/image_3.JPG']->getBaseName()); - $this->assertEquals('image_2.jpg', - $images['OEBPS/Images/image_2.jpg']->getBaseName()); + $this->assertEquals('image_2.jpg', + $images['OEBPS/Images/image_2.jpg']->getBaseName()); - $this->assertEquals('Images/image_2.jpg', $this->getEpub()->opf()->getManifestItemById('image_2.jpg')->item(0)->getAttribute('href')); - $this->assertEquals('Images2/image_3.JPG', $this->getEpub()->opf()->getManifestItemById('image_3.JPG')->item(0)->getAttribute('href')); - } + $this->assertEquals('Images/image_2.jpg', $this->getEpub()->opf()->getManifestItemById('image_2.jpg')->item(0)->getAttribute('href')); + $this->assertEquals('Images2/image_3.JPG', $this->getEpub()->opf()->getManifestItemById('image_3.JPG')->item(0)->getAttribute('href')); + } } diff --git a/tests/UnifyTagIdsTest.php b/tests/UnifyTagIdsTest.php index 99393f0..cf73fe3 100644 --- a/tests/UnifyTagIdsTest.php +++ b/tests/UnifyTagIdsTest.php @@ -6,121 +6,121 @@ class UnifyTagIdsTest extends TestCase { - private string $prefix = ''; + private string $prefix = ''; - public function testMaxId() - { - $this->newEpub(); + public function testMaxId() + { + $this->newEpub(); - $this->addSection('Section0001.xhtml', 'text
'); - $this->addSection('Section0002.xhtml', 'text
'); + $this->addSection('Section0001.xhtml', 'text
'); + $this->addSection('Section0002.xhtml', 'text
'); - $unify = new UnifyTagIds($this->getEpub(), $this->prefix); - $this->assertEquals(1, $unify->getMaxId()); + $unify = new UnifyTagIds($this->getEpub(), $this->prefix); + $this->assertEquals(1, $unify->getMaxId()); - $this->newEpub(); + $this->newEpub(); - $this->addSection('Section0001.xhtml', 'text
'); - $this->addSection('Section0002.xhtml', 'text
'); + $this->addSection('Section0001.xhtml', 'text
'); + $this->addSection('Section0002.xhtml', 'text
'); - $unify = new UnifyTagIds($this->getEpub(), $this->prefix); - $this->assertEquals(4, $unify->getMaxId()); - } + $unify = new UnifyTagIds($this->getEpub(), $this->prefix); + $this->assertEquals(4, $unify->getMaxId()); + } - public function testUnifyWithoutLinks() - { - $this->newEpub(); + public function testUnifyWithoutLinks() + { + $this->newEpub(); - $this->addSection('Section0001.xhtml', 'text
'); - $this->addSection('Section0002.xhtml', 'text
'); + $this->addSection('Section0001.xhtml', 'text
'); + $this->addSection('Section0002.xhtml', 'text
'); - $unify = new UnifyTagIds($this->getEpub(), $this->prefix); - $unify->unify(); + $unify = new UnifyTagIds($this->getEpub(), $this->prefix); + $unify->unify(); - $this->assertEquals('text
', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0001.xhtml')->getBodyContent()); + $this->assertEquals('text
', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0001.xhtml')->getBodyContent()); - $this->assertEquals('text
', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0002.xhtml')->getBodyContent()); - } + $this->assertEquals('text
', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0002.xhtml')->getBodyContent()); + } - public function testUnifyWithoutLinksOnlySectionIds() - { - $this->newEpub(); + public function testUnifyWithoutLinksOnlySectionIds() + { + $this->newEpub(); - $this->addSection('Section0001.xhtml', 'text
', 'note-1'); - $this->addSection('Section0002.xhtml', 'text
', 'note-1'); + $this->addSection('Section0001.xhtml', 'text
', 'note-1'); + $this->addSection('Section0002.xhtml', 'text
', 'note-1'); - $unify = new UnifyTagIds($this->getEpub(), $this->prefix); - $unify->unify(); + $unify = new UnifyTagIds($this->getEpub(), $this->prefix); + $unify->unify(); - $this->assertEquals('note-1', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0001.xhtml')->getBodyId()); + $this->assertEquals('note-1', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0001.xhtml')->getBodyId()); - $this->assertEquals('note-2', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0002.xhtml')->getBodyId()); - } + $this->assertEquals('note-2', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0002.xhtml')->getBodyId()); + } - public function testReplaceFragmentInAnchors() - { - $this->newEpub(); + public function testReplaceFragmentInAnchors() + { + $this->newEpub(); - $this->addSection('Section0001.xhtml', 'text link
'); - $this->addSection('Section0002.xhtml', 'text link
'); - $this->addSection('Section0003.xhtml', 'link text text
'); + $this->addSection('Section0001.xhtml', 'text link
'); + $this->addSection('Section0002.xhtml', 'text link
'); + $this->addSection('Section0003.xhtml', 'link text text
'); - $unify = new UnifyTagIds($this->getEpub(), $this->prefix); - $unify->unify(); + $unify = new UnifyTagIds($this->getEpub(), $this->prefix); + $unify->unify(); - $this->assertEquals('text link
', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0001.xhtml')->getBodyContent()); + $this->assertEquals('text link
', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0001.xhtml')->getBodyContent()); - $this->assertEquals('text link
', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0002.xhtml')->getBodyContent()); + $this->assertEquals('text link
', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0002.xhtml')->getBodyContent()); - $this->assertEquals('link text text
', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0003.xhtml')->getBodyContent()); - } + $this->assertEquals('link text text
', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0003.xhtml')->getBodyContent()); + } - public function testReplaceFragmentInAnchors2() - { - $this->newEpub(); + public function testReplaceFragmentInAnchors2() + { + $this->newEpub(); - $this->addSection('Section0001.xhtml', ''); - $this->addSection('Section0002.xhtml', ''); - $this->addSection('Section0003.xhtml', ''); + $this->addSection('Section0001.xhtml', ''); + $this->addSection('Section0002.xhtml', ''); + $this->addSection('Section0003.xhtml', ''); - $unify = new UnifyTagIds($this->getEpub(), $this->prefix); - $unify->unify(); + $unify = new UnifyTagIds($this->getEpub(), $this->prefix); + $unify->unify(); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0001.xhtml')->getBodyContent()); + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0001.xhtml')->getBodyContent()); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0002.xhtml')->getBodyContent()); + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0002.xhtml')->getBodyContent()); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0003.xhtml')->getBodyContent()); - } + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0003.xhtml')->getBodyContent()); + } - public function testReplaceFragmentInAnchors4() - { - $this->newEpub(); + public function testReplaceFragmentInAnchors4() + { + $this->newEpub(); - $this->addSection('Section0001.xhtml', ''); - $this->addSection('Section0002.xhtml', ''); - $this->addSection('Section0003.xhtml', ''); + $this->addSection('Section0001.xhtml', ''); + $this->addSection('Section0002.xhtml', ''); + $this->addSection('Section0003.xhtml', ''); - $unify = new UnifyTagIds($this->getEpub(), $this->prefix); - $unify->unify(); + $unify = new UnifyTagIds($this->getEpub(), $this->prefix); + $unify->unify(); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0001.xhtml')->getBodyContent()); + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0001.xhtml')->getBodyContent()); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0002.xhtml')->getBodyContent()); + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0002.xhtml')->getBodyContent()); - $this->assertEquals('', - $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0003.xhtml')->getBodyContent()); - } + $this->assertEquals('', + $this->getEpub()->getSectionByFilePath('OEBPS/Text/Section0003.xhtml')->getBodyContent()); + } }