Skip to content

Commit 35063f8

Browse files
Merge pull request #20 from timothepearce/add-callable-to-projection
Add callable to projection
2 parents 5d31e1f + 31518fc commit 35063f8

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

src/Collections/ProjectionCollection.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,25 @@ public function fillBetween(
3939
Carbon $endDate,
4040
string|null $projectionName = null,
4141
string|null $period = null,
42+
callable|null $fillCallable = null,
4243
): self {
4344
[$projectionName, $period] = $this->resolveTypeParameters($projectionName, $period);
4445
[$startDate, $endDate] = $this->resolveDatesParameters($period, $startDate, $endDate);
4546

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

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

52-
$allProjections->push($projection ?? $this->makeEmptyProjection($projectionName, $period, $currentPeriod));
54+
$allProjections->push(
55+
is_null($projection) ?
56+
$this->makeProjection($projectionName, $period, $currentPeriod, is_null($fillCallable) ? null : $fillCallable($lastProjection)) :
57+
$projection
58+
);
59+
60+
$lastProjection = $allProjections->last();
5361
});
5462

5563
return $allProjections;
@@ -199,16 +207,20 @@ private function getAllPeriods(Carbon $startDate, Carbon $endDate, string $perio
199207
}
200208

201209
/**
202-
* Makes an empty projection from the given projector name.
210+
* Makes a projection from the given projector name.
203211
*/
204-
private function makeEmptyProjection(string $projectionName, string $period, string $startDate): Projection
205-
{
212+
private function makeProjection(
213+
string $projectionName,
214+
string $period,
215+
string $startDate,
216+
array|null $content = null
217+
): Projection {
206218
return Projection::make([
207219
'projection_name' => $projectionName,
208220
'key' => null,
209221
'period' => $period,
210222
'start_date' => $startDate,
211-
'content' => (new $projectionName())->defaultContent(),
223+
'content' => $content ?? (new $projectionName())->defaultContent(),
212224
]);
213225
}
214226
}

tests/Collections/ProjectionCollectionTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@ public function it_guess_the_projection_name_if_no_one_is_given_when_filled()
199199
$this->assertEquals($filledCollection->last()->projection_name, SinglePeriodProjection::class);
200200
}
201201

202+
/** @test */
203+
public function it_fills_the_missing_period_with_the_given_callable()
204+
{
205+
Log::factory()->create();
206+
207+
/** @var ProjectionCollection $collection */
208+
$collection = Projection::all();
209+
210+
$filledCollection = $collection->fillBetween(
211+
now(),
212+
now()->addMinutes(10),
213+
SinglePeriodProjection::class,
214+
'5 minutes',
215+
function (Projection $lastProjection) {
216+
return $lastProjection->content;
217+
}
218+
);
219+
220+
$this->assertEquals($filledCollection->last()->content, $filledCollection->first()->content);
221+
}
222+
202223
/** @test */
203224
public function it_is_formatted_to_a_time_series()
204225
{

tests/Models/Log.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,4 @@ class Log extends Model
1818
* The projections list.
1919
*/
2020
protected array $projections = [SinglePeriodProjection::class];
21-
22-
// /**
23-
// * API wip.
24-
// */
25-
// public function relationWithCargoProjection()
26-
// {
27-
// $projections = $this->projections()->get(); // Get all the projections
28-
//
29-
// $this->projectionsFromInterval('5 minutes'); // Get all the projections for the given interval
30-
// $this->lastProjectionFromInterval('1 hour'); // Get the latest projection for the given interval
31-
// $this->firstProjectionFromInterval('1 day'); // Get the first projection for the given interval
32-
// $this->projectionsByIntervals(); // Get all the projections ordered by intervals
33-
//
34-
// $this->projections; // Give a super set of the default collection instance with useful methods
35-
// }
3621
}

0 commit comments

Comments
 (0)