Convert SOURCE_ constants to an enum

This commit is contained in:
Mike Colagrosso 2024-01-28 00:01:12 -07:00 committed by Alex Cabal
parent 3606a5c425
commit 09b4385100
4 changed files with 41 additions and 39 deletions

View file

@ -82,16 +82,6 @@ const HTTP_HEAD = 5;
const VIEW_GRID = 'grid';
const VIEW_LIST = 'list';
const SOURCE_PROJECT_GUTENBERG = 0;
const SOURCE_HATHI_TRUST = 1;
const SOURCE_WIKISOURCE = 2;
const SOURCE_INTERNET_ARCHIVE = 3;
const SOURCE_GOOGLE_BOOKS = 4;
const SOURCE_OTHER = 6;
const SOURCE_PROJECT_GUTENBERG_CANADA = 7;
const SOURCE_PROJECT_GUTENBERG_AUSTRALIA = 8;
const SOURCE_FADED_PAGE = 9;
const AVERAGE_READING_WORDS_PER_MINUTE = 275;
const PAYMENT_CHANNEL_FA = 0;

View file

@ -429,32 +429,32 @@ class Ebook{
foreach($xml->xpath('/package/metadata/dc:source') ?: [] as $element){
$e = (string)$element;
if(mb_stripos($e, 'gutenberg.org/') !== false){
$this->Sources[] = new EbookSource(SOURCE_PROJECT_GUTENBERG, $e);
$this->Sources[] = new EbookSource(EbookSourceType::ProjectGutenberg, $e);
}
elseif(mb_stripos($e, 'gutenberg.net.au/') !== false){
$this->Sources[] = new EbookSource(SOURCE_PROJECT_GUTENBERG_AUSTRALIA, $e);
$this->Sources[] = new EbookSource(EbookSourceType::ProjectGutenbergAustralia, $e);
}
elseif(mb_stripos($e, 'gutenberg.ca/') !== false){
$this->Sources[] = new EbookSource(SOURCE_PROJECT_GUTENBERG_CANADA, $e);
$this->Sources[] = new EbookSource(EbookSourceType::ProjectGutenbergCanada, $e);
}
elseif(mb_stripos($e, 'archive.org/details') !== false){
// `/details` excludes Wayback Machine URLs which may sometimes occur, for example in Lyrical Ballads
$this->Sources[] = new EbookSource(SOURCE_INTERNET_ARCHIVE, $e);
$this->Sources[] = new EbookSource(EbookSourceType::InternetArchive, $e);
}
elseif(mb_stripos($e, 'hathitrust.org/') !== false){
$this->Sources[] = new EbookSource(SOURCE_HATHI_TRUST, $e);
$this->Sources[] = new EbookSource(EbookSourceType::HathiTrust, $e);
}
elseif(mb_stripos($e, 'wikisource.org/') !== false){
$this->Sources[] = new EbookSource(SOURCE_WIKISOURCE, $e);
$this->Sources[] = new EbookSource(EbookSourceType::Wikisource, $e);
}
elseif(mb_stripos($e, 'books.google.com/') !== false || mb_stripos($e, 'google.com/books/') !== false){
$this->Sources[] = new EbookSource(SOURCE_GOOGLE_BOOKS, $e);
$this->Sources[] = new EbookSource(EbookSourceType::GoogleBooks, $e);
}
elseif(mb_stripos($e, 'www.fadedpage.com') !== false){
$this->Sources[] = new EbookSource(SOURCE_FADED_PAGE, $e);
$this->Sources[] = new EbookSource(EbookSourceType::FadedPage, $e);
}
else{
$this->Sources[] = new EbookSource(SOURCE_OTHER, $e);
$this->Sources[] = new EbookSource(EbookSourceType::Other, $e);
}
}

View file

@ -1,9 +1,21 @@
<?
enum EbookSourceType{
case ProjectGutenberg;
case ProjectGutenbergAustralia;
case ProjectGutenbergCanada;
case InternetArchive;
case HathiTrust;
case Wikisource;
case GoogleBooks;
case FadedPage;
case Other;
}
class EbookSource{
public int $Type;
public EbookSourceType $Type;
public string $Url;
public function __construct(int $type, string $url){
public function __construct(EbookSourceType $type, string $url){
$this->Type = $type;
$this->Url = $url;
}

View file

@ -48,21 +48,21 @@ try{
// Divide our sources into transcriptions and scans
foreach($ebook->Sources as $source){
switch($source->Type){
case SOURCE_PROJECT_GUTENBERG:
case SOURCE_PROJECT_GUTENBERG_AUSTRALIA:
case SOURCE_PROJECT_GUTENBERG_CANADA:
case SOURCE_WIKISOURCE:
case SOURCE_FADED_PAGE:
case EbookSourceType::ProjectGutenberg:
case EbookSourceType::ProjectGutenbergAustralia:
case EbookSourceType::ProjectGutenbergCanada:
case EbookSourceType::Wikisource:
case EbookSourceType::FadedPage:
$transcriptionSources[] = $source;
break;
case SOURCE_INTERNET_ARCHIVE:
case SOURCE_HATHI_TRUST:
case SOURCE_GOOGLE_BOOKS:
case EbookSourceType::InternetArchive:
case EbookSourceType::HathiTrust:
case EbookSourceType::GoogleBooks:
$scanSources[] = $source;
break;
case SOURCE_OTHER:
case EbookSourceType::Other:
$otherSources[] = $source;
break;
}
@ -337,11 +337,11 @@ catch(Exceptions\EbookNotFoundException){
<? foreach($transcriptionSources as $source){ ?>
<li>
<p>
<? if($source->Type == SOURCE_PROJECT_GUTENBERG){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="project-gutenberg">Transcription at Project Gutenberg</a>
<? }elseif($source->Type == SOURCE_PROJECT_GUTENBERG_AUSTRALIA){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="project-gutenberg">Transcription at Project Gutenberg Australia</a>
<? }elseif($source->Type == SOURCE_PROJECT_GUTENBERG_CANADA){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="project-gutenberg">Transcription at Project Gutenberg Canada</a>
<? }elseif($source->Type == SOURCE_WIKISOURCE){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="wikisource">Transcription at Wikisource</a>
<? }elseif($source->Type == SOURCE_FADED_PAGE){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="globe">Transcription at Faded Page</a>
<? if($source->Type == EbookSourceType::ProjectGutenberg){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="project-gutenberg">Transcription at Project Gutenberg</a>
<? }elseif($source->Type == EbookSourceType::ProjectGutenbergAustralia){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="project-gutenberg">Transcription at Project Gutenberg Australia</a>
<? }elseif($source->Type == EbookSourceType::ProjectGutenbergCanada){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="project-gutenberg">Transcription at Project Gutenberg Canada</a>
<? }elseif($source->Type == EbookSourceType::Wikisource){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="wikisource">Transcription at Wikisource</a>
<? }elseif($source->Type == EbookSourceType::FadedPage){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="globe">Transcription at Faded Page</a>
<? }else{?>
<a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="globe">Transcription</a>
<? } ?>
@ -358,9 +358,9 @@ catch(Exceptions\EbookNotFoundException){
<? foreach($scanSources as $source){ ?>
<li>
<p>
<? if($source->Type == SOURCE_INTERNET_ARCHIVE){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="internet-archive">Page scans at the Internet Archive</a>
<? }elseif($source->Type == SOURCE_HATHI_TRUST){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="hathitrust">Page scans at HathiTrust</a>
<? }elseif($source->Type == SOURCE_GOOGLE_BOOKS){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="google">Page scans at Google Books</a>
<? if($source->Type == EbookSourceType::InternetArchive){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="internet-archive">Page scans at the Internet Archive</a>
<? }elseif($source->Type == EbookSourceType::HathiTrust){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="hathitrust">Page scans at HathiTrust</a>
<? }elseif($source->Type == EbookSourceType::GoogleBooks){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="google">Page scans at Google Books</a>
<? }else{ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="globe">Page scans</a><? } ?>
</p>
</li>
@ -375,7 +375,7 @@ catch(Exceptions\EbookNotFoundException){
<? foreach($otherSources as $source){ ?>
<li>
<p>
<? if($source->Type == SOURCE_OTHER){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="globe"><?= Formatter::EscapeHtml(preg_replace(['|https?://(en\.)?|', '|/.+$|'], '', (string)$source->Url)) /* force type to (string) to satisfy PHPStan */ ?></a><? } ?>
<? if($source->Type == EbookSourceType::Other){ ?><a href="<?= Formatter::EscapeHtml($source->Url) ?>" class="globe"><?= Formatter::EscapeHtml(preg_replace(['|https?://(en\.)?|', '|/.+$|'], '', (string)$source->Url)) /* force type to (string) to satisfy PHPStan */ ?></a><? } ?>
</p>
</li>
<? } ?>