added regions and comments to improve code readability
authorEduardo <[email protected]>
Thu, 30 Nov 2023 13:19:55 +0000 (14:19 +0100)
committerEduardo <[email protected]>
Thu, 30 Nov 2023 13:19:55 +0000 (14:19 +0100)
page-generator/generator.php

index 07247413a50eaa7cd1ced00c1bb20f342af6f84c..29a99320a3c888dc8a1259c0c2bcbb30d00dbb63 100644 (file)
@@ -5,6 +5,8 @@ const CLOSE = false;
 
 class CodeGenerator
 {
+    #region filesystem related
+
     /**
      * Iterate through all folders and subfolders and make copy of files from
      * $fromFolder to $toFolder.
@@ -74,6 +76,9 @@ class CodeGenerator
         file_put_contents($filePath, $content, FILE_APPEND | LOCK_EX);
     }
 
+    #endregion
+    #region templating
+
     /**
      * returns a string with the var name encoded to be set later with setTemplateVar()
      */
@@ -96,6 +101,9 @@ class CodeGenerator
         }
     }
 
+    #endregion
+    #region html elements generation
+
     /**
      * Returns a string with a span tag with a title set
      */
@@ -182,6 +190,88 @@ class CodeGenerator
         return $html;
     }
 
+    /**
+     * Will return an empty string if the title is not required
+     */
+    private function form_h2_title(array $data, int $currentIndex, string $section, string $url, string $title, array $class): string
+    {
+        $html = "";
+
+        // if previous iteration was diferent section or is first iteration and new section is not empty add h2
+        if (($currentIndex != 0 && $data[$currentIndex - 1]['section'] != $section) || ($currentIndex == 0 && !empty($section))) {
+            $html .= '<h2';
+            // if text is empty add class, title and url
+            $filtered_classes = $this->filter_meta_classes($class);
+            if (empty($text)) {
+                $html .= $this->class_html_or_empty($filtered_classes);
+            }
+
+            $html .= '>';
+
+            if (empty($text) && !empty($url)) {
+                $html .= $this->a($url, $section, $title, $filtered_classes);
+            } elseif (empty($text) && !empty($title)) {
+                $html .= $this->spanTitle($title, $section, $filtered_classes);
+            } else {
+                $html .= $section;
+            }
+
+            $html .= '</h2>';
+        }
+
+        return $html;
+    }
+
+    #endregion
+    #region html containers management
+
+    private function open_or_close_p(bool $is_p_closed, array | null $class, bool $open): array
+    {
+        $html = "";
+
+        if ($is_p_closed && $open) {
+            $html .= '<p';
+            $is_p_closed = false;
+
+            // if class not empty add class
+            $filtered_classes = $this->filter_meta_classes($class);
+            $html .= $this->class_html_or_empty($filtered_classes);
+
+            $html .= '>';
+        } elseif (!$is_p_closed && !$open) {
+            $html .= '</p>';
+            $is_p_closed = true;
+        }
+
+        return [$html, $is_p_closed];
+    }
+
+    private function open_or_close_ul(bool $is_list_closed, array | null $class, bool $open, string $text): array
+    {
+        $html = "";
+
+        if ($is_list_closed && $open) {
+            $html .= '<ul';
+            $is_list_closed = false;
+
+            // if class not empty and text empty add class
+            $filtered_classes = $this->filter_meta_classes($class);
+            if (empty($text)) {
+                $html .= $this->class_html_or_empty($filtered_classes);
+            }
+
+            $html .= '>';
+        } elseif (!$is_list_closed && !$open) {
+            $html .= '</ul>';
+            $is_list_closed = true;
+        }
+
+        return [$html, $is_list_closed];
+    }
+
+    #endregion
+    #region form html contents
+
     /**
      * Forms the html for a given array of data
      *
@@ -277,91 +367,6 @@ class CodeGenerator
         return $html;
     }
 
-    private function open_or_close_p(bool $is_p_closed, array | null $class, bool $open): array
-    {
-        $html = "";
-
-        if ($is_p_closed && $open) {
-            $html .= '<p';
-            $is_p_closed = false;
-
-            // if class not empty add class
-            $filtered_classes = $this->filter_meta_classes($class);
-            $html .= $this->class_html_or_empty($filtered_classes);
-
-            $html .= '>';
-        } elseif (!$is_p_closed && !$open) {
-            $html .= '</p>';
-            $is_p_closed = true;
-        }
-
-        return [$html, $is_p_closed];
-    }
-
-    private function open_or_close_ul(bool $is_list_closed, array | null $class, bool $open, string $text): array
-    {
-        $html = "";
-
-        if ($is_list_closed && $open) {
-            $html .= '<ul';
-            $is_list_closed = false;
-
-            // if class not empty and text empty add class
-            $filtered_classes = $this->filter_meta_classes($class);
-            if (empty($text)) {
-                $html .= $this->class_html_or_empty($filtered_classes);
-            }
-
-            $html .= '>';
-        } elseif (!$is_list_closed && !$open) {
-            $html .= '</ul>';
-            $is_list_closed = true;
-        }
-
-        return [$html, $is_list_closed];
-    }
-
-    /**
-     * Will return an empty string if the title is not required
-     */
-    private function form_h2_title(array $data, int $currentIndex, string $section, string $url, string $title, array $class): string
-    {
-        $html = "";
-
-        // if previous iteration was diferent section or is first iteration and new section is not empty add h2
-        if (($currentIndex != 0 && $data[$currentIndex - 1]['section'] != $section) || ($currentIndex == 0 && !empty($section))) {
-            $html .= '<h2';
-            // if text is empty add class, title and url
-            $filtered_classes = $this->filter_meta_classes($class);
-            if (empty($text)) {
-                $html .= $this->class_html_or_empty($filtered_classes);
-            }
-
-            $html .= '>';
-
-            if (empty($text) && !empty($url)) {
-                $html .= $this->a($url, $section, $title, $filtered_classes);
-            } elseif (empty($text) && !empty($title)) {
-                $html .= $this->spanTitle($title, $section, $filtered_classes);
-            } else {
-                $html .= $section;
-            }
-
-            $html .= '</h2>';
-        }
-
-        return $html;
-    }
-
-    /**
-     * Returns an array with the meta classes removed
-     */
-    private function filter_meta_classes(array $class): array
-    {
-        $meta_classes = ["same-line", "no-space", "new-col", "list"];
-        return array_diff($class, $meta_classes);
-    }
-
     private function form_content(string $text, array $class, string $url, string $title): string
     {
         $html             = "";
@@ -450,13 +455,32 @@ class CodeGenerator
         return $html;
     }
 
+    #endregion
+    #region array manipulation utils
+
+    /**
+     * Returns an array with the meta classes removed
+     */
+    private function filter_meta_classes(array $class): array
+    {
+        return array_diff($class, $meta_classes);
+    }
+
+    /**
+     * Returns the class attribute with the classes in the array or "" (an empty string) if array is empty
+     */
     private function class_html_or_empty(array $filtered_classes): string
     {
         return !empty($filtered_classes) ? ' class="' . implode(' ', $filtered_classes) . '"' : "";
     }
 
+    /**
+     * Returns the value of the key in the array or "" (an empty string) if key is not set
+     */
     private function str_val_or_empty(array $arr, string $key): string
     {
         return isset($arr[$key]) ? $arr[$key] : "";
     }
+
+    #endregion
 }