aboutsummaryrefslogtreecommitdiff
path: root/src/Model/Path.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Model/Path.php')
-rw-r--r--src/Model/Path.php80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/Model/Path.php b/src/Model/Path.php
new file mode 100644
index 0000000..91eb2ff
--- /dev/null
+++ b/src/Model/Path.php
@@ -0,0 +1,80 @@
1<?php
2// src/Model/Path.php
3//
4// attention, cette classe dépend de src/model/Menu.php
5
6declare(strict_types=1);
7
8namespace App\Model;
9
10use App\Entity\Page;
11use App\Model\Model;
12use App\Model\Menu;
13use App\Service\URL;
14
15class Path extends Page
16{
17 private array $current_page = []; // tableau d'objets Page
18
19 public function __construct()
20 {
21 $path_array = explode('/', CURRENT_PAGE);
22 try{
23 // parcourir le menu de haut en bas pour obtenir un ou plusieurs objets Page
24 $this->findPage(Model::$menu, $path_array); // remplit $this->current_page
25 }
26 catch(\Exception $e){}
27 }
28
29 // produit un tableau de Page en comparant le chemin demandé avec les données dans Menu
30 // succès => une exception est lancée pour sortir des fonctions imbriquées
31 // echec => redirection vers la page erreur 404
32 private function findPage(Page|Menu $menu, array $path_array): void
33 {
34 // recherche dans les autres pages
35 if($menu instanceof Menu){
36 foreach($menu->getOtherPages() as $page){
37 if($path_array[0] === $page->getEndOfPath()){
38 $this->current_page[] = $page;
39 throw new \Exception();
40 }
41 }
42 }
43 // recherche dans le menu
44 foreach($menu->getChildren() as $page){
45 if($path_array[0] === $page->getEndOfPath()){
46 $this->current_page[] = $page;
47 if(count($path_array) > 1){
48 array_shift($path_array); // $this->path_array n'est pas modifié, un tableau PHP est passé à une fonction par copie
49 $this->findPage($page, $path_array);
50 }
51 else{
52 throw new \Exception(); // sortir de tous les findPage() en même temps
53 }
54 }
55 }
56
57 // rien trouvé
58 header('Location: '. new URL(['page' => Model::$menu->getChildren()[0]->getEndOfPath()]));
59 die;
60 }
61
62 public function getString(): string
63 {
64 $path_string = "";
65 foreach($this->current_page as $one_page){
66 $path_string .= $one_page->getEndOfPath() . '/';
67 }
68 return rtrim($path_string, '/');
69 }
70 public function getArray(): array
71 {
72 return $this->current_page;
73 }
74
75 // c'est là qu'on est quoi
76 public function getLast(): Page
77 {
78 return $this->current_page[count($this->current_page) - 1];
79 }
80} \ No newline at end of file