From dc934d1818c2e7e44b2022ad5906adde48019b55 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 30 Nov 2023 14:19:55 +0100 Subject: [PATCH] added regions and comments to improve code readability --- page-generator/generator.php | 194 ++++++++++++++++++++--------------- 1 file changed, 109 insertions(+), 85 deletions(-) diff --git a/page-generator/generator.php b/page-generator/generator.php index 0724741..29a9932 100644 --- a/page-generator/generator.php +++ b/page-generator/generator.php @@ -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 .= '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 .= ''; + } + + 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 .= 'filter_meta_classes($class); + $html .= $this->class_html_or_empty($filtered_classes); + + $html .= '>'; + } elseif (!$is_p_closed && !$open) { + $html .= '

'; + $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 .= 'filter_meta_classes($class); + if (empty($text)) { + $html .= $this->class_html_or_empty($filtered_classes); + } + + $html .= '>'; + } elseif (!$is_list_closed && !$open) { + $html .= ''; + $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 .= 'filter_meta_classes($class); - $html .= $this->class_html_or_empty($filtered_classes); - - $html .= '>'; - } elseif (!$is_p_closed && !$open) { - $html .= '

'; - $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 .= 'filter_meta_classes($class); - if (empty($text)) { - $html .= $this->class_html_or_empty($filtered_classes); - } - - $html .= '>'; - } elseif (!$is_list_closed && !$open) { - $html .= ''; - $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 .= '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 .= ''; - } - - 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 } -- 2.30.2