From df3612ed7e6691530503f79483d2fdbc032d01b8 Mon Sep 17 00:00:00 2001 From: polo-pc-greta Date: Thu, 27 Mar 2025 10:13:03 +0100 Subject: mise en ligne github --- src/model/Path.php | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/model/Path.php (limited to 'src/model/Path.php') diff --git a/src/model/Path.php b/src/model/Path.php new file mode 100644 index 0000000..6faadfd --- /dev/null +++ b/src/model/Path.php @@ -0,0 +1,84 @@ +findPage(Director::$menu_data, $path_array); // remplit $this->current_page + } + catch(Exception $e){} + /*echo "nb d'autres pages: " . count(Director::$menu_data->getOtherPages()) . '
'; + echo 'longueur du chemin: ' . count($this->current_page) . '
'; + foreach($this->current_page as $current){ + echo $current->getEndOfPath() . ' '; + } + die;*/ + } + + // produit un tableau de Page en comparant le chemin demandé avec les données dans Menu + // succès => une exception est lancée pour sortir des fonctions imbriquées + // echec => redirection vers la page erreur 404 + private function findPage(Page|Menu $menu, array $path_array) + { + // recherche dans les autres pages + if($menu instanceof Menu){ + foreach($menu->getOtherPages() as $page) + { + if($path_array[0] === $page->getEndOfPath()) + { + $this->current_page[] = $page; + throw new Exception(); + } + } + } + // recherche dans le menu + foreach($menu->getChildren() as $page) + { + if($path_array[0] === $page->getEndOfPath()) + { + $this->current_page[] = $page; + if(count($path_array) > 1) + { + array_shift($path_array); // $this->path_array n'est pas modifié, un tableau PHP est passé à une fonction par copie + $this->findPage($page, $path_array); + } + else{ + throw new Exception(); // sortir de tous les findPage() en même temps + } + } + } + // rien trouvé + URL::setPath('erreur404.html'); + header('Location: '. new URL); + die; + } + + public function getString(): string + { + $path_string = ""; + foreach($this->current_page as $one_page){ + $path_string .= $one_page->getEndOfPath() . '/'; + } + return rtrim($path_string, '/'); + } + public function getArray(): array + { + return $this->current_page; + } + + // c'est là qu'on est quoi + public function getLast(): Page + { + return $this->current_page[count($this->current_page) - 1]; + } +} \ No newline at end of file -- cgit v1.2.3