diff --git a/src/app/Data.php b/src/app/Data.php index ab7e206..96613ae 100644 --- a/src/app/Data.php +++ b/src/app/Data.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace MyPrayerJournal; 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 { @@ -114,8 +114,7 @@ class Data public static function getAnsweredRequests(string $userId): array { $answered = Data::getJournalByAnswered($userId, '=='); - usort($answered, - fn (JournalRequest $a, JournalRequest $b) => $a->asOf == $b->asOf ? 0 : ($a->asOf > $b->asOf ? -1 : 1)); + usort($answered, AsOf::newestToOldest(...)); return $answered; } @@ -128,8 +127,7 @@ class Data public static function getJournal(string $userId): array { $reqs = data::getJournalByAnswered($userId, '<>'); - usort($reqs, - fn (JournalRequest $a, JournalRequest $b) => $a->asOf == $b->asOf ? 0 : ($a->asOf < $b->asOf ? -1 : 1)); + usort($reqs, AsOf::oldestToNewest(...)); return $reqs; } } diff --git a/src/app/domain/AsOf.php b/src/app/domain/AsOf.php new file mode 100644 index 0000000..99ffea2 --- /dev/null +++ b/src/app/domain/AsOf.php @@ -0,0 +1,34 @@ +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); + } +} diff --git a/src/app/domain/History.php b/src/app/domain/History.php index e9f0923..73f9a14 100644 --- a/src/app/domain/History.php +++ b/src/app/domain/History.php @@ -3,15 +3,12 @@ declare(strict_types=1); namespace MyPrayerJournal\Domain; -use DateTimeImmutable; - /** * A record of action taken on a prayer request, including updates to its text */ class History { - /** The date/time this action was taken */ - public DateTimeImmutable $asOf; + use AsOf; /** The action taken that generated this history entry */ public RequestAction $action = RequestAction::Created; @@ -21,7 +18,7 @@ class History 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 diff --git a/src/app/domain/JournalRequest.php b/src/app/domain/JournalRequest.php index 3dbcb59..4e3f901 100644 --- a/src/app/domain/JournalRequest.php +++ b/src/app/domain/JournalRequest.php @@ -8,6 +8,8 @@ namespace MyPrayerJournal\Domain; */ class JournalRequest { + use AsOf; + /** The ID of the prayer request */ public string $id = ''; @@ -17,9 +19,6 @@ class JournalRequest /** The current text of the request */ public string $text = ''; - /** The date/time this request was last updated */ - public \DateTimeImmutable $asOf; - /** The date/time this request was last marked as prayed */ public \DateTimeImmutable $lastPrayed; @@ -69,18 +68,15 @@ class JournalRequest $this->recurrenceType = $req->recurrenceType; $this->recurrence = $req->recurrence; - usort($req->history, - fn (History $a, History $b) => $a->asOf == $b->asOf ? 0 : ($a->asOf > $b->asOf ? -1 : 1)); - $this->asOf = $req->history[0]->asOf; - $this->lastPrayed = - array_values(array_filter($req->history, fn (History $it) => $it->action == RequestAction::Prayed))[0] - ?->asOf; + usort($req->history, AsOf::newestToOldest(...)); + $this->asOf = $req->history[0]->asOf; + $this->lastPrayed = array_values( + array_filter($req->history, fn (History $it) => $it->isPrayed()))[0]?->asOf; if ($full) { - usort($req->notes, - fn (Note $a, Note $b) => $a->asOf == $b->asOf ? 0 : ($a->asOf > $b->asOf ? -1 : 1)); + usort($req->notes, AsOf::newestToOldest(...)); $this->history = $req->history; - $this->notes = $req->notes; + $this->notes = $req->notes; } } } diff --git a/src/app/domain/Note.php b/src/app/domain/Note.php index 17e3861..811534a 100644 --- a/src/app/domain/Note.php +++ b/src/app/domain/Note.php @@ -8,8 +8,7 @@ namespace MyPrayerJournal\Domain; */ class Note { - /** The date/time this note was entered */ - public \DateTimeImmutable $asOf; + use AsOf; /** The note */ public string $notes = '';