Skip to content

Commit

Permalink
Add splitData() to data interval sets
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoiq committed Feb 14, 2020
1 parent 8971f61 commit 2e2e76b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Time/IntervalData/DateIntervalDataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,26 @@ public function modifyData(self $other, callable $reducer): self
return new static($results);
}

/**
* Split interval set to more interval sets with different subsets of original data.
* @param callable $splitter Maps original data set to a group of data sets. Should return array with keys indicating the data set group.
* @return self[] $this
*/
public function splitData(callable $splitter): array
{
$intervalGroups = [];
foreach ($this->intervals as $interval) {
foreach ($splitter($interval->getData()) as $key => $values) {
$intervalGroups[$key][] = new DateIntervalData($interval->getStart(), $interval->getEnd(), $values);
}
}

$intervalSets = [];
foreach ($intervalGroups as $intervals) {
$intervalSets[] = (new DateIntervalDataSet($intervals))->normalize();
}

return $intervalSets;
}

}
22 changes: 22 additions & 0 deletions src/Time/IntervalData/NightIntervalDataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,26 @@ public function modifyData(self $other, callable $reducer): self
return new static($results);
}

/**
* Split interval set to more interval sets with different subsets of original data.
* @param callable $splitter Maps original data set to a group of data sets. Should return array with keys indicating the data set group.
* @return self[] $this
*/
public function splitData(callable $splitter): array
{
$intervalGroups = [];
foreach ($this->intervals as $interval) {
foreach ($splitter($interval->getData()) as $key => $values) {
$intervalGroups[$key][] = new NightIntervalData($interval->getStart(), $interval->getEnd(), $values);
}
}

$intervalSets = [];
foreach ($intervalGroups as $intervals) {
$intervalSets[] = (new NightIntervalDataSet($intervals))->normalize();
}

return $intervalSets;
}

}

0 comments on commit 2e2e76b

Please sign in to comment.