diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index f5e71e2..ca9d427 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ vendor composer.lock index.php +.php-cs-fixer.cache diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index e9b69c2..72967c9 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ }, "require-dev": { "blumilksoftware/codestyle": "^3.1", - "phpunit/phpunit": "^11.1.3" + "phpunit/phpunit": "^11.2" }, "authors": [ { diff --git a/src/HeatmapBuilder.php b/src/HeatmapBuilder.php old mode 100644 new mode 100755 index 2fea280..958bc9f --- a/src/HeatmapBuilder.php +++ b/src/HeatmapBuilder.php @@ -9,6 +9,7 @@ use Blumilk\HeatmapBuilder\Contracts\TimeGroupable; use Carbon\Carbon; use Carbon\CarbonPeriod; +use Carbon\CarbonTimeZone; class HeatmapBuilder { @@ -24,7 +25,10 @@ public function __construct( protected ?Decorator $decorator = null, protected bool $alignedToStartOfPeriod = false, protected bool $alignedToEndOfPeriod = false, - ) {} + protected ?CarbonTimeZone $timezone = null, + ) { + $this->timezone ??= new CarbonTimeZone("UTC"); + } /** * @param iterable $data @@ -42,18 +46,21 @@ public function build(iterable $data): array PeriodInterval::Monthly => $date->format("Y-m"), PeriodInterval::Weekly => $date->format("Y:W"), PeriodInterval::Daily => $date->format("Y-m-d"), + PeriodInterval::Hourly => $date->format("H:i"), }, count: count( array_filter($data, fn(Carbon $item): bool => match ($this->periodInterval) { PeriodInterval::Monthly => $item->isSameMonth($date), PeriodInterval::Weekly => $item->isSameWeek($date), PeriodInterval::Daily => $item->isSameDay($date), + PeriodInterval::Hourly => $item->isSameHour($date), }), ), isToday: match ($this->periodInterval) { PeriodInterval::Monthly => $date->isCurrentMonth(), PeriodInterval::Weekly => $date->isCurrentWeek(), PeriodInterval::Daily => $date->isCurrentDay(), + PeriodInterval::Hourly => $date->isCurrentHour(), }, inFuture: $date->isFuture(), ); @@ -125,10 +132,13 @@ protected function mapToCarbon(array|ArrayAccess|TimeGroupable $item): Carbon ? Carbon::parse($item->getTimeGroupableIndicator()) : Carbon::parse($item[$this->arrayAccessIndex]); + $date->setTimezone($this->timezone); + return match ($this->periodInterval) { PeriodInterval::Monthly => $date->startOfMonth(), PeriodInterval::Weekly => $date->startOfWeek(), PeriodInterval::Daily => $date->startOfDay(), + PeriodInterval::Hourly => $date->startOfHour(), }; } @@ -140,6 +150,7 @@ protected function getPeriod(): CarbonPeriod PeriodInterval::Monthly => $this->now->copy()->subYear(), PeriodInterval::Weekly => $this->now->copy()->subMonth(), PeriodInterval::Daily => $this->now->copy()->subWeek(), + PeriodInterval::Hourly => $this->now->copy()->subDay(), }, $this->periodInterval->value, $this->now->copy(), @@ -152,6 +163,7 @@ protected function getPeriod(): CarbonPeriod PeriodInterval::Monthly => $from->startOfYear(), PeriodInterval::Weekly => $from->startOfMonth(), PeriodInterval::Daily => $from->startOfWeek(), + PeriodInterval::Hourly => $from->startOfDay(), }; $period->setStartDate($from); @@ -163,6 +175,7 @@ protected function getPeriod(): CarbonPeriod PeriodInterval::Monthly => $to->endOfYear(), PeriodInterval::Weekly => $to->endOfMonth(), PeriodInterval::Daily => $to->endOfWeek(), + PeriodInterval::Hourly => $to->endOfDay(), }; $period->setEndDate($to); diff --git a/src/PeriodActions.php b/src/PeriodActions.php index 7617917..c3c7f4d 100644 --- a/src/PeriodActions.php +++ b/src/PeriodActions.php @@ -43,6 +43,7 @@ public function forNumberOfTiles(int $number): static $start = $this->now->copy()->sub( $number, match ($this->periodInterval) { + PeriodInterval::Hourly => "hours", PeriodInterval::Daily => "days", PeriodInterval::Weekly => "weeks", PeriodInterval::Monthly => "months", diff --git a/src/PeriodInterval.php b/src/PeriodInterval.php index 4143685..e588bd1 100644 --- a/src/PeriodInterval.php +++ b/src/PeriodInterval.php @@ -6,6 +6,7 @@ enum PeriodInterval: string { + case Hourly = "1 hour"; case Daily = "1 day"; case Weekly = "1 week"; case Monthly = "1 month";