Add DateTimeFormat enum

This commit is contained in:
Alex Cabal 2024-11-08 16:41:15 -06:00
parent be5574eaec
commit c35c47b793
18 changed files with 38 additions and 26 deletions

View file

@ -54,11 +54,11 @@ class AtomFeed extends Feed{
foreach($this->Entries as $entry){
$obj = new StdClass();
if($entry instanceof Ebook){
$obj->Updated = $entry->EbookUpdated->format('Y-m-d\TH:i:s\Z');
$obj->Updated = $entry->EbookUpdated->format(Enums\DateTimeFormat::Iso->value);
$obj->Id = SITE_URL . $entry->Url;
}
else{
$obj->Updated = $entry->Updated !== null ? $entry->Updated->format('Y-m-d\TH:i:s\Z') : '';
$obj->Updated = $entry->Updated !== null ? $entry->Updated->format(Enums\DateTimeFormat::Iso->value) : '';
$obj->Id = $entry->Id;
}
$currentEntries[] = $obj;

View file

@ -0,0 +1,12 @@
<?
namespace Enums;
enum DateTimeFormat: string{
case Sql = 'Y-m-d H:i:s'; // `2022-01-05 23:42:12`
case Iso = 'Y-m-d\TH:i:s\Z'; // `2022-01-05T23:42:12Z`
case FullDateTime = 'F j, Y g:i a'; // `January 5, 2022 2:04 pm`
case Ical = 'Ymd\THis\Z'; //20220105T234212Z
case Html = 'Y-m-d\TH:i:s'; // `2022-01-05T23:42:12`
case Rss = 'r'; // `D, d M Y H:i:s`
case UnixTimestamp = 'U';
}

View file

@ -98,7 +98,7 @@ class Session{
public static function SetSessionCookie(string $sessionId): void{
/** @throws void */
setcookie('sessionid', $sessionId, ['expires' => intval((new DateTimeImmutable('+1 week'))->format('U')), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']); // Expires in two weeks
setcookie('sessionid', $sessionId, ['expires' => intval((new DateTimeImmutable('+1 week'))->format(Enums\DateTimeFormat::UnixTimestamp->value)), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']); // Expires in two weeks
}
/**

View file

@ -21,7 +21,7 @@ print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
<title><?= Formatter::EscapeXml($title) ?></title>
<? if($subtitle !== null){ ?><subtitle><?= Formatter::EscapeXml($subtitle) ?></subtitle><? } ?>
<icon><?= SITE_URL ?>/images/logo.png</icon>
<updated><?= $updated->format('Y-m-d\TH:i:s\Z') ?></updated>
<updated><?= $updated->format(Enums\DateTimeFormat::Iso->value) ?></updated>
<author>
<name>Standard Ebooks</name>
<uri><?= SITE_URL ?></uri>

View file

@ -15,8 +15,8 @@ use function Safe\filesize;
<uri><?= SITE_URL . Formatter::EscapeXml($entry->AuthorsUrl) ?></uri>
</author>
<? } ?>
<published><?= $entry->Created->format('Y-m-d\TH:i:s\Z') ?></published>
<updated><?= $entry->Updated->format('Y-m-d\TH:i:s\Z') ?></updated>
<published><?= $entry->Created->format(Enums\DateTimeFormat::Iso->value) ?></published>
<updated><?= $entry->Updated->format(Enums\DateTimeFormat::Iso->value) ?></updated>
<rights>Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.</rights>
<summary type="text"><?= Formatter::EscapeXml($entry->Description) ?></summary>
<content type="html"><?= Formatter::EscapeXml($entry->LongDescription) ?></content>

View file

@ -24,9 +24,9 @@ use function Safe\filesize;
<? } ?>
</author>
<? } ?>
<published><?= $entry->EbookCreated->format('Y-m-d\TH:i:s\Z') ?></published>
<dc:issued><?= $entry->EbookCreated->format('Y-m-d\TH:i:s\Z') ?></dc:issued>
<updated><?= $entry->EbookUpdated->format('Y-m-d\TH:i:s\Z') ?></updated>
<published><?= $entry->EbookCreated->format(Enums\DateTimeFormat::Iso->value) ?></published>
<dc:issued><?= $entry->EbookCreated->format(Enums\DateTimeFormat::Iso->value) ?></dc:issued>
<updated><?= $entry->EbookUpdated->format(Enums\DateTimeFormat::Iso->value) ?></updated>
<dc:language><?= Formatter::EscapeXml($entry->Language) ?></dc:language>
<dc:publisher>Standard Ebooks</dc:publisher>
<rights>Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.</rights>

View file

@ -36,7 +36,7 @@ print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
<subtitle><?= Formatter::EscapeXml($subtitle) ?></subtitle>
<? } ?>
<icon><?= SITE_URL ?>/images/logo.png</icon>
<updated><?= $updated->format('Y-m-d\TH:i:s\Z') ?></updated>
<updated><?= $updated->format(Enums\DateTimeFormat::Iso->value) ?></updated>
<? if($isCrawlable){ ?>
<fh:complete/>
<? } ?>

View file

@ -30,7 +30,7 @@ print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
<subtitle><?= Formatter::EscapeXml($subtitle) ?></subtitle>
<? } ?>
<icon><?= SITE_URL ?>/images/logo.png</icon>
<updated><?= $updated->format('Y-m-d\TH:i:s\Z') ?></updated>
<updated><?= $updated->format(Enums\DateTimeFormat::Iso->value) ?></updated>
<author>
<name>Standard Ebooks</name>
<uri><?= SITE_URL ?></uri>
@ -39,7 +39,7 @@ print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
<entry>
<title><?= Formatter::EscapeXml($entry->Title) ?></title>
<link href="<?= SITE_URL . Formatter::EscapeXml($entry->Url) ?>" rel="<?= Formatter::EscapeXml($entry->Rel) ?>" type="application/atom+xml;profile=opds-catalog;kind=<?= $entry->Type ?>; charset=utf-8"/>
<updated><? if($entry->Updated !== null){ ?><?= $entry->Updated->format('Y-m-d\TH:i:s\Z') ?><? } ?></updated>
<updated><? if($entry->Updated !== null){ ?><?= $entry->Updated->format(Enums\DateTimeFormat::Iso->value) ?><? } ?></updated>
<id><?= Formatter::EscapeXml($entry->Id) ?></id>
<content type="text"><?= Formatter::EscapeXml($entry->Description) ?></content>
</entry>

View file

@ -10,7 +10,7 @@ use function Safe\preg_replace;
<title><?= Formatter::EscapeXml($entry->Title) ?>, by <?= Formatter::EscapeXml(strip_tags($entry->AuthorsHtml)) ?></title>
<link><?= SITE_URL . Formatter::EscapeXml($entry->Url) ?></link>
<description><?= Formatter::EscapeXml($entry->Description) ?></description>
<pubDate><?= $entry->EbookCreated->format('r') ?></pubDate>
<pubDate><?= $entry->EbookCreated->format(Enums\DateTimeFormat::Rss->value) ?></pubDate>
<guid><?= Formatter::EscapeXml(preg_replace('/^url:/ius', '', $entry->Identifier)) ?></guid>
<? foreach($entry->Tags as $tag){ ?>
<category domain="https://standardebooks.org/vocab/subjects"><?= Formatter::EscapeXml($tag->Name) ?></category>

View file

@ -18,7 +18,7 @@ print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
<description><?= Formatter::EscapeXml($description) ?></description>
<language>en-US</language>
<copyright>https://creativecommons.org/publicdomain/zero/1.0/</copyright>
<lastBuildDate><?= $updated->format('r'); ?></lastBuildDate>
<lastBuildDate><?= $updated->format(Enums\DateTimeFormat::Rss->value); ?></lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<atom:link href="<?= SITE_URL . Formatter::EscapeXml($url) ?>" rel="self" type="application/rss+xml"/>
<atom:link href="<?= SITE_URL ?>/ebooks/opensearch" rel="search" type="application/opensearchdescription+xml" />

View file

@ -52,7 +52,7 @@ try{
// Increment local download count, expires in 2 weeks
$downloadCount++;
setcookie('download-count', (string)$downloadCount, ['expires' => intval((new DateTimeImmutable('+2 week'))->format('U')), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']);
setcookie('download-count', (string)$downloadCount, ['expires' => intval((new DateTimeImmutable('+2 week'))->format(Enums\DateTimeFormat::UnixTimestamp->value)), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']);
}
catch(Exceptions\InvalidFileException | Exceptions\EbookNotFoundException){
Template::Emit404();

View file

@ -27,7 +27,7 @@ print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<?xml-stylesheet href=\"" . S
<title>Search Results</title>
<subtitle>Results for <?= Formatter::EscapeXml($query) ?>”.</subtitle>
<icon><?= SITE_URL ?>/images/logo.png</icon>
<updated><?= NOW->format('Y-m-d\TH:i:s\Z') ?></updated>
<updated><?= NOW->format(Enums\DateTimeFormat::Iso->value) ?></updated>
<author>
<name>Standard Ebooks</name>
<uri><?= SITE_URL ?></uri>

View file

@ -28,7 +28,7 @@ print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<?xml-stylesheet href=\"". SI
<title>Search Results</title>
<subtitle>Results for <?= Formatter::EscapeXml($query) ?>”.</subtitle>
<icon><?= SITE_URL ?>/images/logo.png</icon>
<updated><?= NOW->format('Y-m-d\TH:i:s\Z') ?></updated>
<updated><?= NOW->format(Enums\DateTimeFormat::Iso->value) ?></updated>
<author>
<name>Standard Ebooks</name>
<uri><?= SITE_URL ?></uri>

View file

@ -26,7 +26,7 @@ print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<?xml-stylesheet href=\"" . S
<description>Results for <?= Formatter::EscapeXml($query) ?>”.</description>
<language>en-US</language>
<copyright>https://creativecommons.org/publicdomain/zero/1.0/</copyright>
<lastBuildDate><?= NOW->format('r') ?></lastBuildDate>
<lastBuildDate><?= NOW->format(Enums\DateTimeFormat::Rss->value) ?></lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<atom:link href="<?= SITE_URL ?>/feeds/rss/all?query=<?= urlencode($query) ?>" rel="self" type="application/rss+xml"/>
<atom:link href="<?= SITE_URL ?>/ebooks/opensearch" rel="search" type="application/opensearchdescription+xml" />

View file

@ -35,7 +35,7 @@ catch(Exceptions\AppException){
<p><?= $poll->Description ?></p>
<? if($poll->IsActive()){ ?>
<? if($poll->End !== null){ ?>
<p class="center-notice">This poll closes on <?= $poll->End->format('F j, Y g:i a') ?>.</p>
<p class="center-notice">This poll closes on <?= $poll->End->format(Enums\DateTimeFormat::FullDateTime->value) ?>.</p>
<? } ?>
<? if(!$canVote){ ?>
<p class="center-notice">Youve already voted in this poll.</p>
@ -48,9 +48,9 @@ catch(Exceptions\AppException){
</p>
<? }else{ ?>
<? if($poll->Start !== null && $poll->Start > NOW){ ?>
<p class="center-notice">This poll opens on <?= $poll->Start->format('F j, Y g:i a') ?>.</p>
<p class="center-notice">This poll opens on <?= $poll->Start->format(Enums\DateTimeFormat::FullDateTime->value) ?>.</p>
<? }else{ ?>
<p class="center-notice">This poll closed on <?= $poll->End->format('F j, Y g:i a') ?>.</p>
<p class="center-notice">This poll closed on <?= $poll->End->format(Enums\DateTimeFormat::FullDateTime->value) ?>.</p>
<p class="button-row narrow"><a href="<?= $poll->Url ?>/votes" class="button">View results</a></p>
<? } ?>
<? } ?>

View file

@ -26,7 +26,7 @@ catch(Exceptions\AppException){
<? if($created){ ?>
<p class="center-notice">Thank you for voting in the <a href="<?= $vote->PollItem->Poll->Url ?>"><?= Formatter::EscapeHtml($vote->PollItem->Poll->Name) ?> poll</a>.</p>
<? }else{ ?>
<p class="center-notice">Your vote in the <a href="<?= $vote->PollItem->Poll->Url ?>"><?= Formatter::EscapeHtml($vote->PollItem->Poll->Name) ?> poll</a> was submitted on <?= $vote->Created->format('F j, Y g:i a') ?>.</p>
<p class="center-notice">Your vote in the <a href="<?= $vote->PollItem->Poll->Url ?>"><?= Formatter::EscapeHtml($vote->PollItem->Poll->Name) ?> poll</a> was submitted on <?= $vote->Created->format(Enums\DateTimeFormat::FullDateTime->value) ?>.</p>
<? } ?>
<p class="button-row narrow"><a class="button" href="<?= $vote->PollItem->Poll->Url ?>/votes"> view results</a></p>
</section>

View file

@ -15,10 +15,10 @@ catch(Exceptions\AppException){
<p class="center-notice">Total votes: <?= number_format($poll->VoteCount) ?></p>
<? if($poll->IsActive()){ ?>
<? if($poll->End !== null){ ?>
<p class="center-notice">This poll closes on <?= $poll->End->format('F j, Y g:i a') ?>.</p>
<p class="center-notice">This poll closes on <?= $poll->End->format(Enums\DateTimeFormat::FullDateTime->value) ?>.</p>
<? } ?>
<? }elseif($poll->End !== null){ ?>
<p class="center-notice">This poll closed on <?= $poll->End->format('F j, Y g:i a') ?>.</p>
<p class="center-notice">This poll closed on <?= $poll->End->format(Enums\DateTimeFormat::FullDateTime->value) ?>.</p>
<? } ?>
<table class="votes">
<tbody>

View file

@ -5,7 +5,7 @@ $hideDonationAlert = HttpInput::Bool(POST, 'hide-donation-alert');
$colorScheme = HttpInput::Str(POST, 'color-scheme');
if($hideDonationAlert !== null){
setcookie('hide-donation-alert', $hideDonationAlert ? 'true' : 'false', ['expires' => intval((new DateTimeImmutable('+1 month'))->format('U')), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
setcookie('hide-donation-alert', $hideDonationAlert ? 'true' : 'false', ['expires' => intval((new DateTimeImmutable('+1 month'))->format(Enums\DateTimeFormat::UnixTimestamp->value)), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
}
if($colorScheme !== null){
@ -18,7 +18,7 @@ if($colorScheme !== null){
setcookie('color-scheme', '', ['expires' => 0, 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
}
else{
setcookie('color-scheme', $colorScheme, ['expires' => intval((new DateTimeImmutable('+1 year'))->format('U')), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
setcookie('color-scheme', $colorScheme, ['expires' => intval((new DateTimeImmutable('+1 year'))->format(Enums\DateTimeFormat::UnixTimestamp->value)), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
}
}