Skip to content

Commit

Permalink
Support collections for series
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBernskiold committed Aug 30, 2024
1 parent a97211c commit 786d2cd
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/Data/Series.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use BernskioldMedia\LaravelHighcharts\Concerns\Makeable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Tappable;

Expand All @@ -29,15 +30,12 @@ class Series implements Arrayable, Jsonable
Makeable,
Tappable;

/**
* @var array<DataPoint|string|array>
*/
public array $data = [];
public ?Collection $data = null;

/**
* @param array<DataPoint|string|array> $data
* @param array<DataPoint|string|array> $data
*/
public function __construct(array $data = [])
public function __construct(Collection|array $data = null)
{
$this->data($data);
}
Expand All @@ -63,9 +61,9 @@ public function id(string $id): self
}

/**
* @param array<DataPoint|string|array> $data
* @param Collection<DataPoint|string|array>|array<DataPoint|string|array>|null $data
*/
public function data(array $data): self
public function data(Collection|array|null $data = null): self
{
$this->data = $data;

Expand All @@ -76,12 +74,10 @@ public function toArray(): array
{
$series = $this->options;

if ($this->data) {
$series['data'] = collect($this->data)
->map(fn ($dataPoint) => $dataPoint instanceof DataPoint
? $dataPoint->toArray()
: $dataPoint)
->toArray();
if ($this->data !== null) {
$data = $this->data instanceof Collection ? $this->data : collect($this->data);

$series['data'] = $data->toArray();
}

if ($this->type) {
Expand Down

0 comments on commit 786d2cd

Please sign in to comment.