Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Litlife committed Feb 20, 2022
1 parent 019b3fa commit 6d8b93c
Show file tree
Hide file tree
Showing 29 changed files with 2,810 additions and 2,806 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* text=auto
*.css linguist-vendored
*.scss linguist-vendored
*.js linguist-vendored
CHANGELOG.md export-ignore
24 changes: 12 additions & 12 deletions src/AddExtensionIfNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
}
}
118 changes: 59 additions & 59 deletions src/AddSectionsIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
86 changes: 43 additions & 43 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Loading

0 comments on commit 6d8b93c

Please sign in to comment.