<?php
+const OPEN = true;
+const CLOSE = false;
+
class CodeGenerator
{
-
/**
* Iterate through all folders and subfolders and make copy of files from
* $fromFolder to $toFolder.
return $str;
}
+
+ /**
+ * Forms the html for a given array of data
+ *
+ * @param array $data
+ * - array of arrays with page data.
+ * - Keys expected: section, text, url, title, class
+ * @param string $lang - TODO: LANGUAGE NOT IMPLEMENTED
+ * - language of the page
+ * - will try to get localized strings (section-lang, text-lang, title-lang....)
+ *
+ * @return string the html formed from the given data with the given language or the default language if given blank or missing
+ */
+ function form_html(array $data, string $lang = null): string
+ {
+ $html = "";
+ $is_p_closed = true;
+ $is_list_closed = true;
+ $is_div_closed = true;
+
+
+ foreach ($data as $i => $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')));
+
+ // if new-col close p and ul and open div with col class
+ if (in_array("new-col", $class)) {
+ [$closing_tag, $is_p_closed] = $this->open_or_close_p($is_p_closed, $class, CLOSE);
+ $html .= $closing_tag;
+ [$closing_tag, $is_list_closed] = $this->open_or_close_ul($is_list_closed, $class, CLOSE, $text);
+ $html .= $closing_tag;
+
+ if (!$is_div_closed) {
+ $html .= '</div>';
+ $is_div_closed = true;
+ }
+ $html .= '<div class="col">';
+ $is_div_closed = false;
+ }
+
+
+ switch ($class) {
+ case in_array("list", $class):
+ // if previous iteration was diferent section close li
+ if ($i != 0 && $data[$i - 1]['section'] != $section) {
+ [$closing_tag, $is_list_closed] = $this->open_or_close_ul($is_list_closed, $class, CLOSE, $text);
+ $html .= $closing_tag;
+ }
+
+ $html .= $this->form_h2_title($data, $i, $section, $url, $title, $class);
+ [$closing_tag, $is_list_closed] = $this->open_or_close_ul($is_list_closed, $class, OPEN, $text);
+ $html .= $closing_tag;
+ $html .= $this->form_list($text, $class, $url, $title);
+
+ break;
+
+ default:
+ // if previous iteration was diferent section close p
+ if ($i != 0 && $data[$i - 1]['section'] != $section) {
+ [$closing_tag, $is_p_closed] = $this->open_or_close_p($is_p_closed, $class, CLOSE);
+ $html .= $closing_tag;
+ }
+
+ $html .= $this->form_h2_title($data, $i, $section, $url, $title, $class);
+ [$closing_tag, $is_p_closed] = $this->open_or_close_p($is_p_closed, $class, OPEN);
+ $html .= $closing_tag;
+ $html .= $this->form_content($text, $class, $url, $title);
+
+ break;
+ }
+ }
+
+ // close last p
+ $html .= $this->open_or_close_p($is_p_closed, null, CLOSE)[0];
+ 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);
+ if (!empty($filtered_classes))
+ $html .= ' class="' . implode(' ', $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) && !empty($filtered_classes))
+ $html .= ' class="' . implode(' ', $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) && !empty($filtered_classes))
+ $html .= ' class="' . implode(' ', $filtered_classes) . '"';
+ $html .= '>';
+
+ if (empty($text) && !empty($url))
+ $html .= $this->a($url, $section, $title);
+ elseif (empty($text) && !empty($title))
+ $html .= $this->spanTitle($title, $section);
+ 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", "new-col", "list"];
+ return array_diff($class, $meta_classes);
+ }
+
+
+ private function form_content(string $text, array $class, string $url, string $title): string
+ {
+ $html = "";
+
+ // if has title and url = a with title
+ if (!empty($title) && !empty($url))
+ $html .= $this->a($url, $text, $title);
+ // if has only title = span with title
+ elseif (!empty($title)) {
+ $html .= $this->spanTitle($title, $text);
+ }
+ // if has only url = a
+ elseif (!empty($url))
+ $html .= $this->a($url, $text);
+ // else just text = p
+ else
+ $html .= $text;
+
+ // if in class array is not "same-line" add br
+ if (!in_array("same-line", $class))
+ $html .= '<br>';
+ else
+ $html .= ' ';
+
+ return $html;
+ }
+
+ private function form_list(string $text, array $class, string $url, string $title): string
+ {
+ $html = "<li";
+
+ // if class not empty add class
+ $filtered_classes = $this->filter_meta_classes($class);
+ if (!empty($filtered_classes))
+ $html .= ' class="' . implode(' ', $filtered_classes) . '"';
+
+ // if has title and url = a with title
+ if (!empty($title) && !empty($url)) {
+ $html .= '>';
+ $html .= $this->a($url, $text, $title);
+ }
+ // if has only title = span with title
+ elseif (!empty($title)) {
+ $html .= " title='$title'>$text";
+ } else
+ $html .= ">$text";
+
+ $html .= "</li>";
+
+ return $html;
+ }
+
+ private function str_val_or_empty(array $arr, string $key): string
+ {
+ return isset($arr[$key]) ? $arr[$key] : "";
+ }
}