From 49d2cf45d9ed42a2ef2793e11e89aba9b84588b6 Mon Sep 17 00:00:00 2001 From: Laurent Beauvisage Date: Sun, 13 Apr 2025 18:44:17 +0200 Subject: [PATCH 1/2] reduce value to merge first week of preview year (if week number is 53) with first week data --- src/Trend.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Trend.php b/src/Trend.php index 28ff762..da88b06 100755 --- a/src/Trend.php +++ b/src/Trend.php @@ -155,6 +155,30 @@ public function mapValuesToDates(Collection $values): Collection ->merge($placeholders) ->unique('date') ->sort() + ->reduce(function (Collection $carry, TrendValue $trendValue) { + if ($this->interval !== 'week') { + $carry->push($trendValue); + return $carry; + } + + [ + $year, + $month, + ] = explode('-', $trendValue->date); + // Handle the special case for week 53 of the year + // when the week starts in December and ends in January of the next year merge with the first week of the next year. + if ($month == '01') { + if ($lastIndex = $carry->search(fn (TrendValue $value) => $value->date === ($year - 1) . '-53')) { + $last = $carry->get($lastIndex); + $trendValue->aggregate += $last->aggregate; + $carry->splice($lastIndex, 1); + } + } + $carry->push($trendValue); + + return $carry; + }, collect()) + ->dd() ->flatten(); } From 8437b263dabcbf5abd2ae997d1210eed2cf4b367 Mon Sep 17 00:00:00 2001 From: Laurent Beauvisage Date: Sun, 13 Apr 2025 18:46:08 +0200 Subject: [PATCH 2/2] remove dd() --- src/Trend.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Trend.php b/src/Trend.php index da88b06..553f144 100755 --- a/src/Trend.php +++ b/src/Trend.php @@ -178,7 +178,6 @@ public function mapValuesToDates(Collection $values): Collection return $carry; }, collect()) - ->dd() ->flatten(); }