Files
Toiminnanohjaus/app/Modules/Reports/Domain/ValueObjects/ReportPeriod.php
T
petriandClaude Sonnet 5.5 64f1e7c54a Add Reports module with the hours by person report
Management's reports (spec 13) get their own module that reads the other
modules through ports. The first report sums hours by person in a period,
narrowed by service unit or customer, with breakdowns, a chart and CSV.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 08:39:19 +02:00

81 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Reports\Domain\ValueObjects;
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
use DateTimeImmutable;
/**
* The days a report covers, first and last included (Y-m-d).
*/
final readonly class ReportPeriod
{
public string $from;
public string $to;
public function __construct(string $from, string $to)
{
$first = self::parse($from);
$last = self::parse($to);
if ($first === null || $last === null || $first > $last) {
throw new InvalidReportPeriod;
}
$this->from = $from;
$this->to = $to;
}
public static function month(DateTimeImmutable $day): self
{
return new self($day->format('Y-m-01'), $day->format('Y-m-t'));
}
public static function previousMonth(DateTimeImmutable $day): self
{
return self::month($day->modify('first day of previous month'));
}
public static function year(DateTimeImmutable $day): self
{
return new self($day->format('Y-01-01'), $day->format('Y-12-31'));
}
public static function previousYear(DateTimeImmutable $day): self
{
return self::year($day->modify('first day of january last year'));
}
/**
* The first month (Y-m), for data kept by month such as billing.
*/
public function fromMonth(): string
{
return substr($this->from, 0, 7);
}
public function toMonth(): string
{
return substr($this->to, 0, 7);
}
public function dayCount(): int
{
return (int) (new DateTimeImmutable($this->from))->diff(new DateTimeImmutable($this->to))->days + 1;
}
private static function parse(string $value): ?DateTimeImmutable
{
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) !== 1) {
return null;
}
$date = DateTimeImmutable::createFromFormat('!Y-m-d', $value);
return $date !== false && $date->format('Y-m-d') === $value ? $date : null;
}
}