protected function completeSection(Response $response, $mindepth) { krsort($this->stack, SORT_NUMERIC); // merge all sections in depths >= mindepth $merge = null; foreach ($this->stack as $depth => $section) { if ($merge) { $section->addSubsection($merge); $merge = null; } if ($depth < $mindepth) { continue; } $merge = $section; unset($this->stack[$depth]); } if ($merge) { if ($mindepth != 0) { throw new \Exception('found child (' . $merge->getHeading() . ') without parent at depth ' . $mindepth); } $response->addSection($merge); } }