['body', 'head', 'nav', 'foot'],'js_array' => ['main']]; public function __construct(string $name = '', array $attributes = [], int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null) { $this->name_node = $name; $this->attributes = $attributes; $this->position = $position; $this->parent = $parent; $this->page = $page; $this->article = $article; } // pfff... public function getId(): ?int { return $this->id_node; } public function getName(): string { return $this->name_node; } /*public function setName(string $name): void { $this->name_node = $name; }*/ public function getArticleTimestamp(): string { return $this->article_timestamp; } public function getAttributes(): array { return $this->attributes; } public function setDefaultAttributes(array $attributes): void { $this->attributes = $attributes; } public function useDefaultAttributes(): void { $this->attributes = self::$default_attributes; } public function addAttribute(string $key, string $value): void { if(!isset($this->attributes[$key])) { // sécurité $key inexistante $this->attributes[$key] = []; } if(!in_array($value, $this->attributes[$key])){ $this->attributes[$key][] = $value; } } /*public function removeAttribute(string $key, string $value): void { if(isset($this->attributes[$key])) // sécurité $key inexistante { // supprime et réindex avec un nouveau tableau $tmp_array = $this->attributes[$key]; $this->attributes[$key] = []; foreach($tmp_array as $entry){ if($entry !== $value){ $this->attributes[$key][] = $entry; } } } }*/ public function getParent(): ?self { return $this->parent; } /*public function setParent(?self $parent): void { $this->parent = $parent; }*/ public function getPosition(): int { return $this->position; } public function setPosition(int $position): void { $this->position = $position; } public function getPage(): Page { return $this->page; } /*public function setPage(Page $page): void { $this->page = $page; }*/ public function getArticle(): Article { return $this->article; } /*public function setArticle(Article $article): void { $this->article = $article; }*/ public function getNodeData(): ?NodeData { return $this->node_data; } public function getChildren(): array { return $this->children; } public function getNodeByName(string $name): ?Node { foreach($this->children as $child){ if($child->getName() === $name){ return $child; } } return null; } public function addChild(self $child): void { $this->children[] = $child; // cas particulier des news: utilise les dates au lieu des positions (les positions existent mais sont ignorées) if($this->getName() === 'news_block'){ $this->sortNews($this->getNodeData()->getData()['chrono'] ?? false); // faux = ordre chronologique } else{ $this->sortChildren(false); } } private function sortNews(bool $chrono = false) // affichage du plus récent au plus ancien par défaut { // tri par insertion similaire à Position::sortChildren for($i = 1; $i < count($this->children); $i++){ $tmp = $this->children[$i]; $j = $i - 1; $compare = $chrono ? fn($a, $b) => $a > $b : fn($a, $b) => $a < $b; while($j >= 0 && $compare($this->children[$j]->getArticle()->getDateTime(), $tmp->getArticle()->getDateTime())){ $this->children[$j + 1] = $this->children[$j]; $j--; } $this->children[$j + 1] = $tmp; } } public function removeChild(self $child): void { foreach($this->children as $key => $object){ if($object->getId() === $child->getId()){ unset($this->children[$key]); } break; } $this->children = array_values($this->children); // réindexer pour supprimer la case vide } public function getAdoptedChild(): ?self // peut renvoyer null { return $this->adopted; } public function setAdoptedChild(self $child): void { $this->adopted = $child; } }