Add AsOf trait for sortable items
This commit is contained in:
parent
0ec4fd017f
commit
9491359b52
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
34
src/app/domain/AsOf.php
Normal file
34
src/app/domain/AsOf.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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,16 +68,13 @@ 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));
|
||||
usort($req->history, AsOf::newestToOldest(...));
|
||||
$this->asOf = $req->history[0]->asOf;
|
||||
$this->lastPrayed =
|
||||
array_values(array_filter($req->history, fn (History $it) => $it->action == RequestAction::Prayed))[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;
|
||||
}
|
||||
|
|
|
@ -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 = '';
|
||||
|
|
Loading…
Reference in New Issue
Block a user