class CodeGenerator
{
+ #region filesystem related
+
/**
* Iterate through all folders and subfolders and make copy of files from
* $fromFolder to $toFolder.
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()
*/
}
}
+ #endregion
+ #region html elements generation
+
/**
* Returns a string with a span tag with a title set
*/
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
*
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 = "";
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
}