Skip to content

Commit

Permalink
#11 - Add build from query method to builder class (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
BarTracz authored Jul 9, 2024
1 parent 020b3e2 commit db8b2ae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ $data = [
$heatmapBuilder = new LaravelHeatmapBuilder();
$heatmap = $heatmapBuilder->buildFromArray($data, 'created_at');
```
There is also option to build from query:
```php
use Blumilk\LaravelHeatmap\LaravelHeatmapBuilder;

$query = YourModel::where('created_at', '>', '2022-11-01 00:00:00');

$heatmapBuilder = new LaravelHeatmapBuilder();
$heatmap = $heatmapBuilder->buildFromQuery($query, 'created_at');
```
16 changes: 16 additions & 0 deletions src/LaravelHeatmapBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Blumilk\Heatmap\HeatmapBuilder;
use Blumilk\Heatmap\PeriodInterval;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use InvalidArgumentException;
Expand All @@ -30,6 +31,21 @@ public function buildFromArray(array $data, string $arrayAccessIndex = self::DEF
return parent::build($collection);
}

public function buildFromQuery(Builder $query, string $arrayAccessIndex = self::DEFAULT_ARRAY_ACCESS_INDEX): array
{
if (!is_a($query, Builder::class)) {
throw new InvalidArgumentException("The provided query is not a valid Eloquent Builder instance.");
}

$this->arrayAccessIndex = $arrayAccessIndex;

$results = $query->get();

$collection = $results instanceof Collection ? $results : collect($results);

return $this->buildFromCollection($collection, $arrayAccessIndex);
}

/**
* @param iterable<array|ArrayAccess|TimeGroupable|Model> $data
*/
Expand Down

0 comments on commit db8b2ae

Please sign in to comment.