aboutsummaryrefslogtreecommitdiff
path: root/src/View/AbstractBuilder.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/View/AbstractBuilder.php')
-rw-r--r--src/View/AbstractBuilder.php81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/View/AbstractBuilder.php b/src/View/AbstractBuilder.php
new file mode 100644
index 0000000..170e871
--- /dev/null
+++ b/src/View/AbstractBuilder.php
@@ -0,0 +1,81 @@
1<?php
2// src/View/AbstractBuilder.php
3
4declare(strict_types=1);
5
6namespace App\View;
7
8use App\Entity\Node;
9use App\Model\Model;
10
11abstract class AbstractBuilder
12{
13 const VIEWS_PATH = '../src/View/templates/';
14 protected string $html = '';
15 protected int $id_node;
16
17 protected function __construct(Node $node)
18 {
19 $this->id_node = $node->getId();
20 }
21
22 protected function useChildrenBuilder(Node $node): void
23 {
24 foreach($node->getChildren() as $child_node)
25 {
26 // le double \\ permet de ne pas échapper la quote
27 $builder_name = 'App\View\\' . $this->snakeToPascalCase($child_node->getName()) . 'Builder';
28 $builder = new $builder_name($child_node);
29 $this->html .= $builder->render();
30
31 // pages spéciales où on n'assemble pas tout
32 if($builder_name === 'HeadBuilder' && in_array(Model::$page_path->getString(), ['connection', 'user_edit']))
33 {
34 foreach($node->getChildren() as $target_node){
35 if($target_node->getName() === 'main'){
36 $main_node = $target_node;
37 break;
38 }
39 }
40 // on construit <main> et on s'arrête! les autres noeuds sont ignorés
41 $builder_name = $this->snakeToPascalCase($main_node->getName()) . 'Builder';
42 $builder = new $builder_name($main_node);
43 $this->html .= "<body>\n";
44 $this->html .= $builder->render() . "\n";
45 $this->html .= "</body>\n</html>";
46 break;
47 }
48 }
49 }
50
51 private function snakeToPascalCase(string $input): string
52 {
53 return str_replace('_', '', ucwords($input, '_'));
54 }
55
56 public function render(): string // = getHTML(), nécéssite d'être public
57 {
58 return $this->html;
59 }
60 protected function addHTML(string $html): void
61 {
62 $this->html .= $html;
63 }
64
65 protected function insertSVG(string $path, array $attributes = []): string
66 {
67 $svg = file_get_contents($path);
68
69 // modification des attributs
70 if(!empty($attributes)){
71 $dom = new \DOMDocument();
72 $dom->loadXML($svg);
73 $svg_elem = $dom->documentElement;
74 foreach($attributes as $key => $value){
75 $svg_elem->setAttribute($key, $value);
76 }
77 $svg = $dom->saveXML($svg_elem);
78 }
79 return $svg;
80 }
81} \ No newline at end of file