/** * Search for the next section with free lanes. * * @param Registration $registration * @param \Closure $compareFunction for comparing the number of the inspected lane with the best match * @return RaceSection | null */ private function getNextPossibleSection(Registration $registration, $compareFunction) { $mySectionNumber = $registration->getSection()->getNumber(); $maxStarters = $registration->getSection()->getRace()->getMaxStarterPerSection(); $sections = array(); /** @var RaceSection $section */ foreach ($registration->getSection()->getRace()->getSections() as $section) { if (!$section->isStarted() && !$section->isFinished()) { // check for free spaces if ($section->getValidRegistrations()->count() < $maxStarters) { $sections[] = $section; } } } /** @var RaceSection $next */ $next = null; foreach ($sections as $section) { if ($compareFunction($section->getNumber(), $mySectionNumber)) { if (is_null($next)) { $next = $section; } else { // search the lowest section of those that are higher if (!$compareFunction($next->getNumber(), $section->getNumber())) { $next = $section; } } } } return $next; }