Add AsOf trait for sortable items

This commit is contained in:
Daniel J. Summers 2023-08-23 19:58:31 -04:00
parent 0ec4fd017f
commit 9491359b52
5 changed files with 48 additions and 24 deletions

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace MyPrayerJournal; namespace MyPrayerJournal;
use BitBadger\PgSQL\Documents\{ Configuration, Definition, Document, DocumentIndex, Query }; use BitBadger\PgSQL\Documents\{ Configuration, Definition, Document, DocumentIndex, Query };
use MyPrayerJournal\Domain\{ History, JournalRequest, Note, Request, RequestAction }; use MyPrayerJournal\Domain\{ AsOf, History, JournalRequest, Note, Request, RequestAction };
class Data class Data
{ {
@ -114,8 +114,7 @@ class Data
public static function getAnsweredRequests(string $userId): array public static function getAnsweredRequests(string $userId): array
{ {
$answered = Data::getJournalByAnswered($userId, '=='); $answered = Data::getJournalByAnswered($userId, '==');
usort($answered, usort($answered, AsOf::newestToOldest(...));
fn (JournalRequest $a, JournalRequest $b) => $a->asOf == $b->asOf ? 0 : ($a->asOf > $b->asOf ? -1 : 1));
return $answered; return $answered;
} }
@ -128,8 +127,7 @@ class Data
public static function getJournal(string $userId): array public static function getJournal(string $userId): array
{ {
$reqs = data::getJournalByAnswered($userId, '<>'); $reqs = data::getJournalByAnswered($userId, '<>');
usort($reqs, usort($reqs, AsOf::oldestToNewest(...));
fn (JournalRequest $a, JournalRequest $b) => $a->asOf == $b->asOf ? 0 : ($a->asOf < $b->asOf ? -1 : 1));
return $reqs; return $reqs;
} }
} }

34
src/app/domain/AsOf.php Normal file
View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace MyPrayerJournal\Domain;
trait AsOf
{
/** The "as of" date/time */
public \DateTimeImmutable $asOf;
/**
* Sort an as-of item from oldest to newest
*
* @param AsOf $a The first item to compare
* @param AsOf $b The second item to compare
* @return int 0 if they are equal, -1 if A is earlier than B, or 1 if B is earlier than A
*/
public static function oldestToNewest(AsOf $a, AsOf $b): int
{
return $a->asOf == $b->asOf ? 0 : ($a->asOf < $b->asOf ? -1 : 1);
}
/**
* Sort an as-of item from newest to oldest
*
* @param AsOf $a The first item to compare
* @param AsOf $b The second item to compare
* @return int 0 if they are equal, -1 if B is earlier than A, or 1 if A is earlier than B
*/
public static function newestToOldest(AsOf $a, AsOf $b): int
{
return $a->asOf == $b->asOf ? 0 : ($a->asOf > $b->asOf ? -1 : 1);
}
}

View File

@ -3,15 +3,12 @@ declare(strict_types=1);
namespace MyPrayerJournal\Domain; namespace MyPrayerJournal\Domain;
use DateTimeImmutable;
/** /**
* A record of action taken on a prayer request, including updates to its text * A record of action taken on a prayer request, including updates to its text
*/ */
class History class History
{ {
/** The date/time this action was taken */ use AsOf;
public DateTimeImmutable $asOf;
/** The action taken that generated this history entry */ /** The action taken that generated this history entry */
public RequestAction $action = RequestAction::Created; public RequestAction $action = RequestAction::Created;
@ -21,7 +18,7 @@ class History
public function __construct() public function __construct()
{ {
$this->asOf = new DateTimeImmutable('1/1/1970', new \DateTimeZone('Etc/UTC')); $this->asOf = new \DateTimeImmutable('1/1/1970', new \DateTimeZone('Etc/UTC'));
} }
public function isCreated(): bool public function isCreated(): bool

View File

@ -8,6 +8,8 @@ namespace MyPrayerJournal\Domain;
*/ */
class JournalRequest class JournalRequest
{ {
use AsOf;
/** The ID of the prayer request */ /** The ID of the prayer request */
public string $id = ''; public string $id = '';
@ -17,9 +19,6 @@ class JournalRequest
/** The current text of the request */ /** The current text of the request */
public string $text = ''; public string $text = '';
/** The date/time this request was last updated */
public \DateTimeImmutable $asOf;
/** The date/time this request was last marked as prayed */ /** The date/time this request was last marked as prayed */
public \DateTimeImmutable $lastPrayed; public \DateTimeImmutable $lastPrayed;
@ -69,16 +68,13 @@ class JournalRequest
$this->recurrenceType = $req->recurrenceType; $this->recurrenceType = $req->recurrenceType;
$this->recurrence = $req->recurrence; $this->recurrence = $req->recurrence;
usort($req->history, usort($req->history, AsOf::newestToOldest(...));
fn (History $a, History $b) => $a->asOf == $b->asOf ? 0 : ($a->asOf > $b->asOf ? -1 : 1));
$this->asOf = $req->history[0]->asOf; $this->asOf = $req->history[0]->asOf;
$this->lastPrayed = $this->lastPrayed = array_values(
array_values(array_filter($req->history, fn (History $it) => $it->action == RequestAction::Prayed))[0] array_filter($req->history, fn (History $it) => $it->isPrayed()))[0]?->asOf;
?->asOf;
if ($full) { if ($full) {
usort($req->notes, usort($req->notes, AsOf::newestToOldest(...));
fn (Note $a, Note $b) => $a->asOf == $b->asOf ? 0 : ($a->asOf > $b->asOf ? -1 : 1));
$this->history = $req->history; $this->history = $req->history;
$this->notes = $req->notes; $this->notes = $req->notes;
} }

View File

@ -8,8 +8,7 @@ namespace MyPrayerJournal\Domain;
*/ */
class Note class Note
{ {
/** The date/time this note was entered */ use AsOf;
public \DateTimeImmutable $asOf;
/** The note */ /** The note */
public string $notes = ''; public string $notes = '';