Skip to content

Commit

Permalink
Merge pull request #20 from timothepearce/add-callable-to-projection
Browse files Browse the repository at this point in the history
Add callable to projection
  • Loading branch information
timothepearce authored Jan 21, 2022
2 parents 5d31e1f + 31518fc commit 35063f8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
24 changes: 18 additions & 6 deletions src/Collections/ProjectionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,25 @@ public function fillBetween(
Carbon $endDate,
string|null $projectionName = null,
string|null $period = null,
callable|null $fillCallable = null,
): self {
[$projectionName, $period] = $this->resolveTypeParameters($projectionName, $period);
[$startDate, $endDate] = $this->resolveDatesParameters($period, $startDate, $endDate);

$allPeriods = $this->getAllPeriods($startDate, $endDate, $period);
$allProjections = new self([]);
$lastProjection = null;

$allPeriods->each(function (string $currentPeriod) use (&$projectionName, &$period, &$allProjections) {
$allPeriods->each(function (string $currentPeriod) use (&$projectionName, &$period, &$allProjections, &$fillCallable, &$lastProjection) {
$projection = $this->firstWhere('start_date', $currentPeriod);

$allProjections->push($projection ?? $this->makeEmptyProjection($projectionName, $period, $currentPeriod));
$allProjections->push(
is_null($projection) ?
$this->makeProjection($projectionName, $period, $currentPeriod, is_null($fillCallable) ? null : $fillCallable($lastProjection)) :
$projection
);

$lastProjection = $allProjections->last();
});

return $allProjections;
Expand Down Expand Up @@ -199,16 +207,20 @@ private function getAllPeriods(Carbon $startDate, Carbon $endDate, string $perio
}

/**
* Makes an empty projection from the given projector name.
* Makes a projection from the given projector name.
*/
private function makeEmptyProjection(string $projectionName, string $period, string $startDate): Projection
{
private function makeProjection(
string $projectionName,
string $period,
string $startDate,
array|null $content = null
): Projection {
return Projection::make([
'projection_name' => $projectionName,
'key' => null,
'period' => $period,
'start_date' => $startDate,
'content' => (new $projectionName())->defaultContent(),
'content' => $content ?? (new $projectionName())->defaultContent(),
]);
}
}
21 changes: 21 additions & 0 deletions tests/Collections/ProjectionCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ public function it_guess_the_projection_name_if_no_one_is_given_when_filled()
$this->assertEquals($filledCollection->last()->projection_name, SinglePeriodProjection::class);
}

/** @test */
public function it_fills_the_missing_period_with_the_given_callable()
{
Log::factory()->create();

/** @var ProjectionCollection $collection */
$collection = Projection::all();

$filledCollection = $collection->fillBetween(
now(),
now()->addMinutes(10),
SinglePeriodProjection::class,
'5 minutes',
function (Projection $lastProjection) {
return $lastProjection->content;
}
);

$this->assertEquals($filledCollection->last()->content, $filledCollection->first()->content);
}

/** @test */
public function it_is_formatted_to_a_time_series()
{
Expand Down
15 changes: 0 additions & 15 deletions tests/Models/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,4 @@ class Log extends Model
* The projections list.
*/
protected array $projections = [SinglePeriodProjection::class];

// /**
// * API wip.
// */
// public function relationWithCargoProjection()
// {
// $projections = $this->projections()->get(); // Get all the projections
//
// $this->projectionsFromInterval('5 minutes'); // Get all the projections for the given interval
// $this->lastProjectionFromInterval('1 hour'); // Get the latest projection for the given interval
// $this->firstProjectionFromInterval('1 day'); // Get the first projection for the given interval
// $this->projectionsByIntervals(); // Get all the projections ordered by intervals
//
// $this->projections; // Give a super set of the default collection instance with useful methods
// }
}

0 comments on commit 35063f8

Please sign in to comment.