myPrayerJournal/src/lib/Domain/Recurrence.php
Daniel J. Summers 20ad50928a Move files, complete PHP migration
The application works the same way as the F# version
2024-06-23 16:35:55 -04:00

42 lines
1.2 KiB
PHP

<?php declare(strict_types=1);
namespace MyPrayerJournal\Domain;
use DateInterval;
use JsonSerializable;
/**
* The recurrence for a prayer request
*/
class Recurrence implements JsonSerializable
{
/**
* @param RecurrencePeriod $period The recurrence period
* @param int|null $interval How many of the periods will pass before the request is visible again
*/
public function __construct(public RecurrencePeriod $period, public ?int $interval = null) { }
/**
* Get the date/time interval for this recurrence
*
* @return DateInterval The interval matching the recurrence
*/
public function interval(): DateInterval
{
$period = match ($this->period) {
RecurrencePeriod::Immediate => 'T0S',
RecurrencePeriod::Hours => "T{$this->interval}H",
RecurrencePeriod::Days => "{$this->interval}D",
RecurrencePeriod::Weeks => ($this->interval * 7) . 'D'
};
return new DateInterval("P$period");
}
public function jsonSerialize(): mixed
{
$values = ['period' => $this->period->value];
if (isset($this->interval)) $values['interval'] = $this->interval;
return $values;
}
}