|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace EbookReader\Driver; |
| 6 | + |
| 7 | +use EbookReader\Data\TxtData; |
| 8 | +use EbookReader\Exception\ParserException; |
| 9 | +use EbookReader\Meta\TxtMeta; |
| 10 | + |
| 11 | +final class TxtDriver extends AbstractDriver |
| 12 | +{ |
| 13 | + private ?string $internalFile = null; |
| 14 | + |
| 15 | + protected function getInternalFile(): string |
| 16 | + { |
| 17 | + if (!$this->internalFile) { |
| 18 | + $zip = new \ZipArchive(); |
| 19 | + $res = $zip->open($this->getFile(), \ZipArchive::RDONLY); |
| 20 | + if (true === $res) { |
| 21 | + $txtFile = null; |
| 22 | + // get first .txt file |
| 23 | + for ($i = 0; $i < $zip->numFiles; ++$i) { |
| 24 | + $fileName = $zip->getNameIndex($i); |
| 25 | + if (false === $fileName) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + if ('txt' === \pathinfo($fileName, \PATHINFO_EXTENSION)) { |
| 29 | + $txtFile = $fileName; |
| 30 | + break; |
| 31 | + } |
| 32 | + } |
| 33 | + $zip->close(); |
| 34 | + if (null === $txtFile) { |
| 35 | + throw new ParserException(); |
| 36 | + } |
| 37 | + |
| 38 | + $this->internalFile = 'zip://'.$this->getFile().'#'.$txtFile; |
| 39 | + } else { |
| 40 | + $this->internalFile = 'file://'.$this->getFile(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return $this->internalFile; |
| 45 | + } |
| 46 | + |
| 47 | + public function isValid(): bool |
| 48 | + { |
| 49 | + try { |
| 50 | + $f = new \SplFileObject($this->getInternalFile(), 'r'); |
| 51 | + } catch (\Exception $e) { |
| 52 | + return false; |
| 53 | + } finally { |
| 54 | + unset($f); // close file |
| 55 | + } |
| 56 | + |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return TxtData[] |
| 62 | + */ |
| 63 | + public function getData(): array |
| 64 | + { |
| 65 | + try { |
| 66 | + $f = new \SplFileObject($this->getInternalFile(), 'r'); |
| 67 | + } catch (\Exception $e) { |
| 68 | + throw new ParserException(previous: $e); |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + $text = ''; |
| 73 | + while (!$f->eof()) { |
| 74 | + $text .= $f->fread(4096); |
| 75 | + } |
| 76 | + } finally { |
| 77 | + unset($f); // close file |
| 78 | + } |
| 79 | + |
| 80 | + $text = \mb_trim($text); |
| 81 | + $pos = \strpos($text, "\n"); |
| 82 | + if (false !== $pos) { |
| 83 | + $title = \substr($text, 0, $pos); |
| 84 | + $title = \mb_trim($title); |
| 85 | + } else { |
| 86 | + $title = null; |
| 87 | + } |
| 88 | + |
| 89 | + return [ |
| 90 | + new TxtData($text, $title, []), |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + public function getCover(): ?string |
| 95 | + { |
| 96 | + // todo |
| 97 | + throw new \RuntimeException('Not implemented'); |
| 98 | + } |
| 99 | + |
| 100 | + public function getMeta(): TxtMeta |
| 101 | + { |
| 102 | + try { |
| 103 | + $f = new \SplFileObject($this->getInternalFile(), 'r'); |
| 104 | + } catch (\Exception $e) { |
| 105 | + throw new ParserException(previous: $e); |
| 106 | + } |
| 107 | + |
| 108 | + try { |
| 109 | + $text = ''; |
| 110 | + while (!$f->eof()) { |
| 111 | + $text = \mb_trim($f->fgets()); |
| 112 | + if ('' !== $text) { |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + } finally { |
| 117 | + unset($f); // close file |
| 118 | + } |
| 119 | + |
| 120 | + return new TxtMeta($text); |
| 121 | + } |
| 122 | +} |
0 commit comments