Skip to content

Commit

Permalink
Fix collection to array conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseekn committed Oct 25, 2023
1 parent 3612609 commit b79cb98
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/LaravelMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,28 +523,29 @@ protected function generateDateRange($startDate, $endDate)
return $dateRange;
}

protected function fillEmptyDatesFromCollection(Collection $data): Collection
protected function fillEmptyDatesFromCollection(array $data): array
{
$dateRange = $this->generateDateRange($this->period[0], $this->period[1]);
$mergedData = [];
$data = collect($data);
$result = [];

foreach ($dateRange as $date) {
$dataForDate = $data->where('label', $date)->first();

if ($dataForDate) {
$mergedData[] = [
'label' => $dataForDate->label,
'data' => $dataForDate->data
$result[] = [
'label' => $dataForDate['label'],
'data' => $dataForDate['data']
];
} else {
$mergedData[] = [
$result[] = [
'label' => $date,
'data' => $this->emptyDatesData
];
}
}

return collect($mergedData);
return $result;
}

/**
Expand All @@ -561,7 +562,7 @@ public function metrics(): mixed
*/
public function trends(): array
{
$trendsData = $this->trendsData();
$trendsData = $this->trendsData()->toArray();

if ($this->fillEmptyDates) {
$trendsData = $this->fillEmptyDatesFromCollection($trendsData);
Expand Down Expand Up @@ -639,10 +640,10 @@ protected function formatPeriod(string $period): string
};
}

protected function formatDate(Collection $data): Collection
protected function formatDate(array $data): array
{
return $data->map(function ($datum) {
if (!is_numeric($datum['label']) && !DateTime::createFromFormat('Y-m-d',$datum['label'])) {
return array_map(function ($datum) {
if (!is_numeric($datum['label']) && !DateTime::createFromFormat('Y-m-d', $datum['label'])) {
return $datum;
}

Expand All @@ -659,7 +660,7 @@ protected function formatDate(Collection $data): Collection
}

return $datum;
});
}, $data);
}

protected function checkDateFormat(array $dates): void
Expand Down

0 comments on commit b79cb98

Please sign in to comment.