protected function getAdjacentPage()

in src/site/controllers/GuidePageController.php [180:216]


  protected function getAdjacentPage(
    dict<string, NavDataNode> $guides,
    string $guide,
    string $page,
    bool $next = false,
  ): ?PaginationDataNode {
    $sibling_pages = $guides[$guide]['children'];
    $sibling_pages = $next ? array_reverse($sibling_pages) : $sibling_pages;
    $adjacent_page = null;

    // Gets last page if next = true, first page if previous (next = false)
    $head_page = C\first_key($sibling_pages);

    if ($page === $head_page) {
      $adj = $this->getAdjacentGuide($guides, $guide, $next);
      if ($adj !== null) {
        list($adjacent_guide, $adjacent_guide_data) = $adj;
        $guide_pages = $adjacent_guide_data['children'];
        $guide_pages = $next ? $guide_pages : array_reverse($guide_pages);
        $adjacent_page = shape(
          'page' => tuple(C\first_keyx($guide_pages), C\firstx($guide_pages)),
          'guide' => tuple($adjacent_guide, $adjacent_guide_data),
        );
      }
    } else {
      foreach ($sibling_pages as $sibling => $sibling_data) {
        if ($sibling === $page) {
          break;
        }
        $adjacent_page = shape(
          'page' => tuple($sibling, $sibling_data),
          'guide' => tuple($guide, $guides[$guide]),
        );
      }
    }
    return $adjacent_page;
  }