@@ -39,17 +39,25 @@ public function fillBetween(
39
39
Carbon $ endDate ,
40
40
string |null $ projectionName = null ,
41
41
string |null $ period = null ,
42
+ callable |null $ fillCallable = null ,
42
43
): self {
43
44
[$ projectionName , $ period ] = $ this ->resolveTypeParameters ($ projectionName , $ period );
44
45
[$ startDate , $ endDate ] = $ this ->resolveDatesParameters ($ period , $ startDate , $ endDate );
45
46
46
47
$ allPeriods = $ this ->getAllPeriods ($ startDate , $ endDate , $ period );
47
48
$ allProjections = new self ([]);
49
+ $ lastProjection = null ;
48
50
49
- $ allPeriods ->each (function (string $ currentPeriod ) use (&$ projectionName , &$ period , &$ allProjections ) {
51
+ $ allPeriods ->each (function (string $ currentPeriod ) use (&$ projectionName , &$ period , &$ allProjections, & $ fillCallable , & $ lastProjection ) {
50
52
$ projection = $ this ->firstWhere ('start_date ' , $ currentPeriod );
51
53
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 ();
53
61
});
54
62
55
63
return $ allProjections ;
@@ -199,16 +207,20 @@ private function getAllPeriods(Carbon $startDate, Carbon $endDate, string $perio
199
207
}
200
208
201
209
/**
202
- * Makes an empty projection from the given projector name.
210
+ * Makes a projection from the given projector name.
203
211
*/
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 {
206
218
return Projection::make ([
207
219
'projection_name ' => $ projectionName ,
208
220
'key ' => null ,
209
221
'period ' => $ period ,
210
222
'start_date ' => $ startDate ,
211
- 'content ' => (new $ projectionName ())->defaultContent (),
223
+ 'content ' => $ content ?? (new $ projectionName ())->defaultContent (),
212
224
]);
213
225
}
214
226
}
0 commit comments