diff --git a/src/Trend.php b/src/Trend.php index 0a2fe96..ecfe5b0 100755 --- a/src/Trend.php +++ b/src/Trend.php @@ -124,10 +124,22 @@ public function count(string $column = '*'): Collection public function mapValuesToDates(Collection $values): Collection { - return $this->getDatePeriod() - ->flatMap(fn (Carbon $date) => [$date->format($this->getCarbonDateFormat()) => 0]) - ->merge($values->flatMap(fn ($value) => [$value->date => $value->aggregate])) - ->sortKeys(); + $values = $values->map(fn ($value) => new TrendValue( + date: $value->date, + aggregate: $value->aggregate, + )); + + $placeholders = $this->getDatePeriod()->map( + fn (Carbon $date) => new TrendValue( + date: $date->format($this->getCarbonDateFormat()), + aggregate: 0, + ) + ); + + return $values + ->merge($placeholders) + ->unique('date') + ->flatten(); } protected function getDatePeriod(): Collection diff --git a/src/TrendValue.php b/src/TrendValue.php new file mode 100644 index 0000000..8f24c09 --- /dev/null +++ b/src/TrendValue.php @@ -0,0 +1,12 @@ +