Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mstenta committed Mar 14, 2024
1 parent 98b545f commit dec12a4
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/CropPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ public function getCropPlantingStages(PlanRecordInterface $crop_planting, int|nu
];
}

// If start/end bounds are specified, filter and trim stages to fit.
if (!is_null($start) || !is_null($end)) {
$stages = $this->boundTimelineStages($stages, $start, $end);
}

return $stages;
}

Expand All @@ -174,6 +179,7 @@ public function getAssetLocationStages(AssetInterface $asset, int|null $start =
}

// Sort stages chronologically.
// @todo end is always null here
usort($stages, function ($a, $b) {
if ($a['start'] == $b['end']) {
return 0;
Expand All @@ -189,6 +195,11 @@ public function getAssetLocationStages(AssetInterface $asset, int|null $start =
}
}

// If start/end bounds are specified, filter and trim stages to fit.
if (!is_null($start) || !is_null($end)) {
$stages = $this->boundTimelineStages($stages, $start, $end);
}

return $stages;
}

Expand Down Expand Up @@ -216,4 +227,46 @@ protected function getAssetMovementLogs(AssetInterface $asset) {
return $logs;
}

/**
* Filter and trim timeline stages to fit within start/end bounds.
*
* @param array $stages
* Array of timeline stages.
* @param int|null $start
* Optionally specify a start timestamp.
* @param int|null $end
* Optionally specify an end timestamp.
*
* @return array
* Returns an array of filtered and trimmed stages.
*/
protected function boundTimelineStages(array $stages, int|null $start = NULL, int|null $end = NULL) {

// Filter out stages that end before the start bound, or start after the
// end bound.
$stages = array_filter($stages, function ($stage) use ($start, $end) {
if (!is_null($start) && !is_null($stage['end']) && $stage['end'] <= $start) {
return FALSE;
}
if (!is_null($end) && !is_null($stage['start']) && $stage['start'] >= $end) {
return FALSE;
}
return TRUE;
});


// Trim stages to fit within the start and end bounds.
array_map(function ($stage) use ($start, $end) {
if (!is_null($start) && !is_null($stage['start']) && $stage['start'] < $start) {
$stage['start'] = $start;
}
if (!is_null($end) && !is_null($stage['end']) && $stage['end'] > $end) {
$stage['end'] = $end;
}
return $stage;
}, $stages);

return $stages;
}

}

0 comments on commit dec12a4

Please sign in to comment.