HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /home/timetracker.panomity.com/src/Model/DailyStatistic.php
<?php

/*
 * This file is part of the Kimai time-tracking app.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Model;

use App\Entity\User;
use App\Model\Statistic\StatisticDate;
use DateTimeInterface;

final class DailyStatistic implements DateStatisticInterface
{
    /**
     * @var array<string, StatisticDate>
     */
    private array $days = [];
    private DateTimeInterface $begin;
    private DateTimeInterface $end;

    public function __construct(DateTimeInterface $begin, DateTimeInterface $end, private User $user)
    {
        $this->begin = clone $begin;
        $this->end = clone $end;
    }

    public function getUser(): User
    {
        return $this->user;
    }

    private function setupDays(): void
    {
        if (!empty($this->days)) {
            return;
        }

        $tmp = \DateTime::createFromInterface($this->begin);
        $tmp->setTime(0, 0, 0);
        while ($tmp < $this->end) {
            $id = $tmp->format('Y-m-d');
            $this->days[$id] = new StatisticDate(clone $tmp);
            $tmp->modify('+1 day');
        }
    }

    /**
     * @return StatisticDate[]
     */
    public function getDays(): array
    {
        $this->setupDays();

        return array_values($this->days);
    }

    /**
     * For unified frontend access
     *
     * @return StatisticDate[]
     */
    public function getData(): array
    {
        return $this->getDays();
    }

    public function getDayByDateTime(\DateTimeInterface $date): ?StatisticDate
    {
        return $this->getDay($date->format('Y'), $date->format('m'), $date->format('d'));
    }

    public function getByDateTime(\DateTimeInterface $date): ?StatisticDate
    {
        return $this->getDayByDateTime($date);
    }

    public function getDayByReportDate(string $date): ?StatisticDate
    {
        $this->setupDays();

        if (!isset($this->days[$date])) {
            return null;
        }

        return $this->days[$date];
    }

    public function getDay(string $year, string $month, string $day): ?StatisticDate
    {
        if ((int) $month < 10) {
            $month = '0' . (int) $month;
        }

        if ((int) $day < 10) {
            $day = '0' . (int) $day;
        }

        $date = $year . '-' . $month . '-' . $day;

        return $this->getDayByReportDate($date);
    }

    /**
     * @return DateTimeInterface[]
     */
    public function getDateTimes(): array
    {
        $this->setupDays();
        $all = [];

        foreach ($this->days as $id => $day) {
            $all[] = $day->getDate();
        }

        return $all;
    }
}