From 33a751323bbfedc6b461bef19e4b179926cc74cb Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 26 Nov 2023 16:02:16 +0100 Subject: [PATCH] Coding Style: tryng to follow PSR-12 --- page-generator/generator.php | 157 +++++++++++++++++++++-------------- page-generator/index.php | 53 +++++++----- 2 files changed, 124 insertions(+), 86 deletions(-) diff --git a/page-generator/generator.php b/page-generator/generator.php index 1b919a7..713e7ed 100644 --- a/page-generator/generator.php +++ b/page-generator/generator.php @@ -1,6 +1,6 @@ isDir()) + if ($file->isDir()) { rmdir($file); - else + } else { unlink($file); + } } rmdir($dir); @@ -49,23 +51,25 @@ class CodeGenerator /** * Create a folder structure for a given list of pages */ - function generateFoldersForPages(array $folderList, $basePath) + public function generateFoldersForPages(array $folderList, $basePath) { foreach ($folderList as $folder) { - if ($folder == '/') + if ($folder == '/') { mkdir($basePath . "/index"); - else + } else { mkdir($basePath . '/' . $folder); + } } } /** * Create or append to a file ($filePath) the text in $content */ - function writeToFile(string $filePath, string | null $content) + public function writeToFile(string $filePath, string | null $content) { - if (is_null($content)) + if (is_null($content)) { return; + } file_put_contents($filePath, $content, FILE_APPEND | LOCK_EX); } @@ -73,7 +77,7 @@ class CodeGenerator /** * returns a string with the var name encoded to be set later with setTemplateVar() */ - function templateVar(string $name): string + public function templateVar(string $name): string { return "$$" . $name . "$$"; } @@ -81,25 +85,27 @@ class CodeGenerator /** * Replace a template variable with a given value and return the new content string */ - function setTemplateVar(string $filePath, string $var, string $value) + public function setTemplateVar(string $filePath, string $var, string $value) { $replaceVar = "$$" . $var . "$$"; - if (file_exists($filePath)) + if (file_exists($filePath)) { return str_replace($replaceVar, $value, file_get_contents($filePath)); - else + } else { return str_replace($replaceVar, $value, $filePath); + } } /** * Returns a string with a span tag with a title set */ - function spanTitle(string $title, string $text, array | null $class): string + public function spanTitle(string $title, string $text, array | null $class): string { $html = " $line) { $section = $this->str_val_or_empty($line, 'section'); - $text = $this->str_val_or_empty($line, 'text'); - $url = $this->str_val_or_empty($line, 'url'); - $title = $this->str_val_or_empty($line, 'title'); - $class = array_map('trim', explode(",", $this->str_val_or_empty($line, 'class'))); + $text = $this->str_val_or_empty($line, 'text'); + $url = $this->str_val_or_empty($line, 'url'); + $title = $this->str_val_or_empty($line, 'title'); + $class = array_map('trim', explode(",", $this->str_val_or_empty($line, 'class'))); // if new-col close p and ul and open div with col class if (in_array("new-col", $class)) { @@ -209,7 +219,6 @@ class CodeGenerator $is_div_closed = false; } - switch ($class) { case in_array("list", $class): // if previous iteration was diferent section close li @@ -263,8 +272,9 @@ class CodeGenerator // if class not empty add class $filtered_classes = $this->filter_meta_classes($class); - if (!empty($filtered_classes)) + if (!empty($filtered_classes)) { $html .= ' class="' . implode(' ', $filtered_classes) . '"'; + } $html .= '>'; } elseif (!$is_p_closed && !$open) { @@ -285,8 +295,9 @@ class CodeGenerator // if class not empty and text empty add class $filtered_classes = $this->filter_meta_classes($class); - if (empty($text) && !empty($filtered_classes)) + if (empty($text) && !empty($filtered_classes)) { $html .= ' class="' . implode(' ', $filtered_classes) . '"'; + } $html .= '>'; } elseif (!$is_list_closed && !$open) { @@ -309,16 +320,19 @@ class CodeGenerator $html .= 'filter_meta_classes($class); - if (empty($text) && !empty($filtered_classes)) + if (empty($text) && !empty($filtered_classes)) { $html .= ' class="' . implode(' ', $filtered_classes) . '"'; + } + $html .= '>'; - if (empty($text) && !empty($url)) + if (empty($text) && !empty($url)) { $html .= $this->a($url, $section, $title, $filtered_classes); - elseif (empty($text) && !empty($title)) + } elseif (empty($text) && !empty($title)) { $html .= $this->spanTitle($title, $section, $filtered_classes); - else + } else { $html .= $section; + } $html .= ''; } @@ -335,34 +349,40 @@ class CodeGenerator return array_diff($class, $meta_classes); } - private function form_content(string $text, array $class, string $url, string $title): string { - $html = ""; + $html = ""; $filtered_classes = $this->filter_meta_classes($class); // if has title and url = a with title - if (!empty($title) && !empty($url)) + if (!empty($title) && !empty($url)) { $html .= $this->a($url, $text, $title, $filtered_classes); + } + // if has only title = span with title elseif (!empty($title)) { $html .= $this->spanTitle($title, $text, $filtered_classes); } // if has only url = a - elseif (!empty($url)) + elseif (!empty($url)) { $html .= $this->a($url, $text, null, $filtered_classes); + } + // else just text = p - else + else { $html .= $text; + } // if in class array is not "same-line" add br - if (!in_array("same-line", $class)) + if (!in_array("same-line", $class)) { $html .= '
'; - else + } else { $html .= ' '; + } - if (in_array("no-space", $class)) + if (in_array("no-space", $class)) { $html = trim($html); + } return $html; } @@ -370,41 +390,50 @@ class CodeGenerator private function form_list(string $text, array $class, string $url, string $title): string { // if in class array is not "same-line" set flag - if (!in_array("same-line", $class)) + if (!in_array("same-line", $class)) { $this->is_li_same_line = false; - else + } else { $this->is_li_same_line = true; + } - if (!isset($this->was_li_same_line) || !$this->was_li_same_line) + if (!isset($this->was_li_same_line) || !$this->was_li_same_line) { $html = "filter_meta_classes($class); - if (!empty($filtered_classes)) + if (!empty($filtered_classes)) { $html .= ' class="' . implode(' ', $filtered_classes) . '"'; + } // if has title and url = a with title if (!empty($url)) { - if (!isset($this->was_li_same_line) || !$this->was_li_same_line) + if (!isset($this->was_li_same_line) || !$this->was_li_same_line) { $html .= '>'; + } + $html .= $this->a($url, $text, $title, $filtered_classes); - if (in_array("no-space", $class)) + if (in_array("no-space", $class)) { $html = trim($html); + } } // if has only title = span with title elseif (!empty($title)) { $html .= " title='$title'>$text"; } else { - if (!isset($this->was_li_same_line) || !$this->was_li_same_line) + if (!isset($this->was_li_same_line) || !$this->was_li_same_line) { $html .= '>'; + } + $html .= "$text"; } - if (!$this->is_li_same_line) + if (!$this->is_li_same_line) { $html .= ''; + } $this->was_li_same_line = $this->is_li_same_line; return $html; diff --git a/page-generator/index.php b/page-generator/index.php index d1ea3e1..8c884c2 100644 --- a/page-generator/index.php +++ b/page-generator/index.php @@ -1,12 +1,12 @@ $image) { } // format image classes foreach ($data['images'] as $key => $image) { - if (empty($image['class'])) + if (empty($image['class'])) { $data['images'][$key]['class'] = null; - else + } else { $data['images'][$key]['class'] = array_map('trim', explode(",", $image['class'])); + } } // create partials: @@ -101,8 +103,9 @@ foreach ($data['header'] as $item) { } } -foreach ($data['langs'] as $lang) +foreach ($data['langs'] as $lang) { $generator->writeToFile($head_path, ''); +} $generator->writeToFile($head_path, ' ' . $generator->templateVar("page-title") . ' @@ -144,7 +147,7 @@ foreach ($data['menu'] as $item) { if (boolval($item['active'])) { $generator->writeToFile($header_path, '
  • '); $generator->writeToFile($header_path, $generator->a('../' . $item['path'], $item['item'], null, null)); - $generator->writeToFile($header_path, '
  • '); + $generator->writeToFile($header_path, ''); } } $generator->writeToFile($header_path, ''); @@ -157,10 +160,13 @@ $generator->writeToFile($header_path, ''); // populate page array with paths $page_array = []; -foreach ($data['menu'] as $item) - foreach ($item as $key => $value) - if ($key == 'path') +foreach ($data['menu'] as $item) { + foreach ($item as $key => $value) { + if ($key == 'path') { $page_array[] = $value; + } + } +} // generate page folders mkdir(BASE_PATH_TEMP . "/pages"); @@ -171,18 +177,20 @@ $tmp_head = $generator->setTemplateVar($head_path, "lang-code", "en"); // populate an array with path => title $path_title = []; -foreach ($menu_list as $item) +foreach ($menu_list as $item) { $path_title[$item['path']] = $item['title']; +} foreach ($data['pages'] as $page) { - $page_path = BASE_PATH_TEMP . "/pages/" . $page['path'] . "/index.html"; + $page_path = BASE_PATH_TEMP . "/pages/" . $page['path'] . "/index.html"; $page['class'] = array_map('trim', explode(",", $page['class'])); // put page title in header - if (!empty($path_title[$page['path']])) + if (!empty($path_title[$page['path']])) { $formed_head = $generator->setTemplateVar($tmp_head, "page-title", $path_title[$page['path']] . " | " . PAGE_TITLE); - else + } else { $formed_head = $generator->setTemplateVar($tmp_head, "page-title", PAGE_TITLE); + } // put headers $generator->writeToFile($page_path, $formed_head); @@ -195,11 +203,12 @@ foreach ($data['pages'] as $page) { } // get page data - if (empty($data['pages'][$page['path']])) + if (empty($data['pages'][$page['path']])) { $data['pages'][$page['path']] = load_data(URL . '?sheet=' . $page['path']); + } $page_data = $data['pages'][$page['path']]; - + // generate page $generator->writeToFile($page_path, $generator->form_html($page_data, "en")); $generator->writeToFile($page_path, ''); -- 2.30.2