@@ -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}
0 commit comments