diff --git a/src/bootstrap/main.php b/src/bootstrap/main.php index a033bd1..fde2511 100644 --- a/src/bootstrap/main.php +++ b/src/bootstrap/main.php @@ -10,7 +10,7 @@ declare(strict_types=1); namespace CodeHydrater\bootstrap; -$mem_baseline = memory_get_usage(); +define("Memory_Baseline", memory_get_usage()); site_helper::load_file(CodeHydrater_FRAMEWORK . 'bootstrap/errors.php'); diff --git a/src/bootstrap/safer_io.php b/src/bootstrap/safer_io.php index f3a243b..384d326 100644 --- a/src/bootstrap/safer_io.php +++ b/src/bootstrap/safer_io.php @@ -72,6 +72,8 @@ final class use_iol { } } + $root_folder = str_replace('/', "\\", $root_folder); + $class_name = "\\Project\\inputs\\{$root_folder}\\{$file}_in"; $input = $class_name::$method($app); diff --git a/src/classes/attributes/validators/my_validator.php b/src/classes/attributes/validators/my_validator.php index 1083b3e..d3f5e48 100644 --- a/src/classes/attributes/validators/my_validator.php +++ b/src/classes/attributes/validators/my_validator.php @@ -214,16 +214,12 @@ $handlers = [ continue; } - $message = $handlers[$class]( + $handlers[$class]( $name, $value, $attribute->newInstance(), $messages ); - - if ($message) { - $this->errors[$name][] = $message; - } } } } diff --git a/src/classes/common.php b/src/classes/common.php index a4fd694..bba42f2 100644 --- a/src/classes/common.php +++ b/src/classes/common.php @@ -81,6 +81,14 @@ final class common { public static function get_count($i): int { return (is_array($i) || is_object($i)) ? count($i) : 0; } + + public static function str_replace_first(string $search, string $replace, string $subject): string { + $pos = strpos($subject, $search); + if ($pos !== false) { + return substr_replace($subject, $replace, $pos, strlen($search)); + } + return $subject; + } // Begin Strings Functions here:: public static function string_position(string $string, string $needle, int $offset = 0, $encoding = null) { @@ -222,10 +230,10 @@ final class common { if (\CodeHydrater\bootstrap\configure::get('security', 'show_dumps') !== true) { return; } - if (!is_object($var)) { +// if (!is_object($var)) { var_dump($var); echo '
'; - } +// } if ($var === false) { echo 'It is FALSE!'; diff --git a/src/classes/html.php b/src/classes/html.php index 82efb69..b880b38 100644 --- a/src/classes/html.php +++ b/src/classes/html.php @@ -11,6 +11,14 @@ declare(strict_types=1); namespace CodeHydrater; final class html { + const ECHO = true; + public static function do_echo(?string $input, bool $echo = self::ECHO): ?string { + if ($echo) { + echo $input; + return null; + } + return $input; + } /** * Purpose: To output all HTML select (drop down) option values. @@ -53,6 +61,25 @@ final class html { return $values; } + private static function make_headers_on_table(array $header_fields, array $options = []) { + $echo = $options['echo'] ?? self::ECHO; + $nl = PHP_EOL; + $default_table_setup = "border='1' cellpadding='1' cellspacing='1'"; + $table = $options['table_setup'] ?? $default_table_setup; + $escape = $options['escape'] ?? true; + $th = $options['th'] ?? ""; + $th_class = (! empty($th)) ? " class=\"$th\" " : ""; + + $ret = self::do_echo("{$nl}{$nl}", $echo); + $ret .= self::do_echo("\t{$nl}", $echo); + foreach($header_fields as $header_field) { + $field = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($header_field) : $header_field; + $ret .= self::do_echo("\t\t{$field}{$nl}", $echo); + } + $ret .= self::do_echo("\t{$nl}", $echo); + return $ret; + } + /** * Used by Memory_Usage script. * Displays a table from input arrays -- fields @@ -64,70 +91,118 @@ final class html { public static function show_table( array $header_fields, array $fields, - bool $escape = true - ): void { - $nl = PHP_EOL; - echo "{$nl}
{$nl}"; - echo "\t{$nl}"; - foreach($header_fields as $header_field) { - $field = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($header_field) : $header_field; - echo "\t\t{$nl}"; - } - echo "\t{$nl}"; - + array $options = [] + ): ?string { + $echo = $options['echo'] ?? self::ECHO; + $nl = PHP_EOL; + $ret = self::make_headers_on_table($header_fields, $options); + $escape = $options['escape'] ?? true; foreach($fields as $field) { - echo "\t{$nl}"; + $ret .= self::do_echo("\t{$nl}", $echo); foreach($field as $td) { $cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td; - echo "\t\t{$nl}"; + $ret .= self::do_echo("\t\t{$nl}", $echo); } - echo "\t{$nl}"; + $ret .= self::do_echo("\t{$nl}", $echo); } - echo "
{$field}
{$cell}{$cell}
{$nl}"; + $ret .= self::do_echo("{$nl}", $echo); + return $ret; } - /** - * Generators use Memory in an Effient way!!!! So use this. - * @param array $header_fields - * @param array $db_field_names - * @param \Iterator $records - * @param bool $escape - * @return void - */ - public static function show_table_from_generator( - array $header_fields, - \Iterator $records, - bool $escape = true - ): void { - $nl = PHP_EOL; - echo "{$nl}{$nl}"; - echo "\t{$nl}"; - foreach($header_fields as $header_field) { - $field = ($escape) ? \bs_tts\safer_io::h($header_field) : $header_field; - echo "\t\t{$nl}"; - } - echo "\t{$nl}"; - - foreach($records as $record) { - echo "\t{$nl}"; + private static function make_cell_table_helper_for_IO(\Iterator $record, array $options = []): array { + $echo = $options['echo'] ?? self::ECHO; + $escape = $options['escape'] ?? true; + $cell_class = $options['cell_class'] ?? ""; + $row_class = $options['row_class'] ?? ""; + $cell_class_tag = (! empty($cell_class)) ? " class=\"$cell_class\" " : ""; + $row_class_tag = (! empty($row_class)) ? " class=\"$row_class\" " : ""; + $nl = PHP_EOL; + $ret = self::do_echo("\t{$nl}", $echo); + + $errors = []; + foreach($record as $html) { + $key = $html['name'] ?? ""; + $value = $html['html']; + + if (\CodeHydrater\common::get_count($html['errors'])) { + $errors[$key] = $html['errors'][$key]; + } + + $td = $value ?? ""; + if (is_string($td)) { + $cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td; + } else { + $cell = (string) $td; + } + $ret .= self::do_echo("\t\t{$cell}{$nl}", $echo); + } + $ret .= self::do_echo("\t{$nl}", $echo); + return ['errors' => $errors, 'data' => $ret]; + } + + private static function make_cell_table_helper(array $record, array $options = []): ?string { + $echo = $options['echo'] ?? self::ECHO; + $escape = $options['escape'] ?? true; + $cell_class = $options['cell_class'] ?? ""; + $row_class = $options['row_class'] ?? ""; + $cell_class_tag = (! empty($cell_class)) ? " class=\"$cell_class\" " : ""; + $row_class_tag = (! empty($row_class)) ? " class=\"$row_class\" " : ""; + $nl = PHP_EOL; + $ret = self::do_echo("\t{$nl}", $echo); foreach($record as $key => $value) { if (! is_string($key)) { continue; // Remove Duplicate records } $td = $value ?? ""; if (is_string($td)) { - $cell = ($escape) ? \bs_tts\safer_io::h($td) : $td; + $cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td; } else { $cell = (string) $td; } - echo "\t\t{$nl}"; + $ret .= self::do_echo("\t\t{$cell}{$nl}", $echo); } - echo "\t{$nl}"; + $ret .= self::do_echo("\t{$nl}", $echo); + return $ret; + } + + /** + * Generators use Memory in an Effient way!!!! So use this. + * @param array $header_fields + * @param array $db_field_names + * @param $records, note: \Iterator not always can be used here + * @param bool $escape + * @return void + */ + public static function show_table_from( + array $header_fields, + $records, + array $options = [], + ) { + $echo = $options['echo'] ?? self::ECHO; + $ret = self::make_headers_on_table($header_fields, $options); + switch($options['helper']) { + case "single-row": + $ret .= self::make_cell_table_helper($records, $options); + break; + case "io": + $ret = self::make_cell_table_helper_for_IO($records, $options); + $ret['data'] .= self::do_echo($ret['data'] . "
{$field}
{$cell}
" . PHP_EOL); + return $ret; + default: + foreach($records as $record) { + self::make_cell_table_helper($record, $options); + } } - echo "{$nl}"; + $ret .= self::do_echo("" . PHP_EOL, $echo); + return $ret; } - public static function show_errors(?array $errors = []): void { + public static function show_errors( + ?array $errors = [], + array $options = [] + ): ?string { + $echo = $options['echo'] ?? self::ECHO; + $ret = ""; if ($errors) { $message = "Please correct the following errors: "; $i = 0; @@ -135,7 +210,8 @@ final class html { $i++; $message .= "
{$i}) " . $error; } - echo $message; + $ret .= self::do_echo($message, $echo); } + return $ret; } } \ No newline at end of file diff --git a/src/classes/memory_usage.php b/src/classes/memory_usage.php index b7978ef..7fa7bda 100644 --- a/src/classes/memory_usage.php +++ b/src/classes/memory_usage.php @@ -18,6 +18,9 @@ final class memory_usage { * @retval string of unit size for bytes */ public static function convert_bytes($size) { + if ($size === null) { + return 0; + } $unit=array('b','kb','mb','gb','tb','pb'); return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; } @@ -27,7 +30,11 @@ final class memory_usage { * @param bool $echo (default of true) - true: displays, false: returns data */ public static function get_memory_stats($echo = true) { - global $mem_baseline; + if (defined("Memory_Baseline")) { + $mem_baseline = Memory_Baseline; + } else { + $mem_baseline = 0; + } $check = common::get_bool(misc::request_var('debug')); @@ -44,27 +51,17 @@ final class memory_usage { $diff_peak = $peak - $mem_baseline; $s_diff_peak = self::convert_bytes($diff_peak); - if (! $echo) { - $a_fields['start-up'] = $s_startup_mem; - $a_fields['currently'] = $s_current; - $a_fields['total-diff'] = $s_diff; - $a_fields['peak'] = $s_peak; - $a_fields['total-diff-peak'] = $s_diff_peak; - } else { $a_headers = array('Memory Item', 'Size'); $a_fields[] = array('On-StartUp', $s_startup_mem); $a_fields[] = array('Currently', $s_current); $a_fields[] = array('Total Diff', "{$s_diff}"); $a_fields[] = array(' ', ' '); $a_fields[] = array('PEAK', $s_peak); - $a_fields[] = array('Total Diff PEAK', "{$s_diff_peak}"); - } - - if ($echo === true) { - html::show_table($a_headers, $a_fields, false); - } else { - return $a_fields; - } + $a_fields[] = array('Total Diff PEAK', "{$s_diff_peak}"); + + $options['escape'] = false; + $options['echo'] = $echo; + return html::show_table($a_headers, $a_fields, $options); } } diff --git a/src/classes/traits/form_validator.php b/src/classes/traits/form_validator.php index 3cb7a96..33d229a 100644 --- a/src/classes/traits/form_validator.php +++ b/src/classes/traits/form_validator.php @@ -53,9 +53,12 @@ trait form_validator { } private static function is_required(array $data, string $field): bool { - $r = self::check_if_null_or_false($data, $field); - if (is_bool($r)) return $r; - + // Don't use check_if_null_or_false here, it should always fail on false or null! + $r = $data[$field] ?? null; + if ($r === null || $r === false) { + return false; // It's a required field, so fail + } + if (common::get_count($r)) { return false; // Should not be an array here } diff --git a/src/classes/view.php b/src/classes/view.php index 5659e8d..89ff29b 100644 --- a/src/classes/view.php +++ b/src/classes/view.php @@ -122,7 +122,7 @@ final class view { * @param string $render_page * @return bool was found */ - public function set_template(string $render_page): bool { + public function set_php_template(string $render_page): bool { if (! empty($render_page)) { $render_page = str_replace('.php', '', $render_page); $templ = "/templates/{$render_page}";