main
Robert 2 months ago
parent 4a70f8b183
commit c396101ccc
  1. 2
      src/bootstrap/main.php
  2. 2
      src/bootstrap/safer_io.php
  3. 6
      src/classes/attributes/validators/my_validator.php
  4. 12
      src/classes/common.php
  5. 158
      src/classes/html.php
  6. 25
      src/classes/memory_usage.php
  7. 7
      src/classes/traits/form_validator.php
  8. 2
      src/classes/view.php

@ -10,7 +10,7 @@ declare(strict_types=1);
namespace CodeHydrater\bootstrap; namespace CodeHydrater\bootstrap;
$mem_baseline = memory_get_usage(); define("Memory_Baseline", memory_get_usage());
site_helper::load_file(CodeHydrater_FRAMEWORK . 'bootstrap/errors.php'); site_helper::load_file(CodeHydrater_FRAMEWORK . 'bootstrap/errors.php');

@ -72,6 +72,8 @@ final class use_iol {
} }
} }
$root_folder = str_replace('/', "\\", $root_folder);
$class_name = "\\Project\\inputs\\{$root_folder}\\{$file}_in"; $class_name = "\\Project\\inputs\\{$root_folder}\\{$file}_in";
$input = $class_name::$method($app); $input = $class_name::$method($app);

@ -214,16 +214,12 @@ $handlers = [
continue; continue;
} }
$message = $handlers[$class]( $handlers[$class](
$name, $name,
$value, $value,
$attribute->newInstance(), $attribute->newInstance(),
$messages $messages
); );
if ($message) {
$this->errors[$name][] = $message;
}
} }
} }
} }

@ -82,6 +82,14 @@ final class common {
return (is_array($i) || is_object($i)) ? count($i) : 0; 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:: // Begin Strings Functions here::
public static function string_position(string $string, string $needle, int $offset = 0, $encoding = null) { public static function string_position(string $string, string $needle, int $offset = 0, $encoding = null) {
return F::strpos($string, $needle, $offset); return F::strpos($string, $needle, $offset);
@ -222,10 +230,10 @@ final class common {
if (\CodeHydrater\bootstrap\configure::get('security', 'show_dumps') !== true) { if (\CodeHydrater\bootstrap\configure::get('security', 'show_dumps') !== true) {
return; return;
} }
if (!is_object($var)) { // if (!is_object($var)) {
var_dump($var); var_dump($var);
echo '<br>'; echo '<br>';
} // }
if ($var === false) { if ($var === false) {
echo 'It is FALSE!'; echo 'It is FALSE!';

@ -11,6 +11,14 @@ declare(strict_types=1);
namespace CodeHydrater; namespace CodeHydrater;
final class html { 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. * Purpose: To output all HTML select (drop down) option values.
@ -53,6 +61,25 @@ final class html {
return $values; 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}<table {$table}>{$nl}", $echo);
$ret .= self::do_echo("\t<tr>{$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<th{$th_class}>{$field}</th>{$nl}", $echo);
}
$ret .= self::do_echo("\t</tr>{$nl}", $echo);
return $ret;
}
/** /**
* Used by Memory_Usage script. * Used by Memory_Usage script.
* Displays a table from input arrays -- fields * Displays a table from input arrays -- fields
@ -64,70 +91,118 @@ final class html {
public static function show_table( public static function show_table(
array $header_fields, array $header_fields,
array $fields, array $fields,
bool $escape = true array $options = []
): void { ): ?string {
$echo = $options['echo'] ?? self::ECHO;
$nl = PHP_EOL; $nl = PHP_EOL;
echo "{$nl}<table border='1' cellpadding='1' cellspacing='1'>{$nl}"; $ret = self::make_headers_on_table($header_fields, $options);
echo "\t<tr>{$nl}"; $escape = $options['escape'] ?? true;
foreach($header_fields as $header_field) {
$field = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($header_field) : $header_field;
echo "\t\t<th>{$field}</th>{$nl}";
}
echo "\t</tr>{$nl}";
foreach($fields as $field) { foreach($fields as $field) {
echo "\t<tr>{$nl}"; $ret .= self::do_echo("\t<tr>{$nl}", $echo);
foreach($field as $td) { foreach($field as $td) {
$cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td; $cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td;
echo "\t\t<td>{$cell}</td>{$nl}"; $ret .= self::do_echo("\t\t<td>{$cell}</td>{$nl}", $echo);
} }
echo "\t</tr>{$nl}"; $ret .= self::do_echo("\t</tr>{$nl}", $echo);
} }
echo "</table>{$nl}"; $ret .= self::do_echo("</table>{$nl}", $echo);
return $ret;
} }
/** private static function make_cell_table_helper_for_IO(\Iterator $record, array $options = []): array {
* Generators use Memory in an Effient way!!!! So use this. $echo = $options['echo'] ?? self::ECHO;
* @param array $header_fields $escape = $options['escape'] ?? true;
* @param array $db_field_names $cell_class = $options['cell_class'] ?? "";
* @param \Iterator $records $row_class = $options['row_class'] ?? "";
* @param bool $escape $cell_class_tag = (! empty($cell_class)) ? " class=\"$cell_class\" " : "";
* @return void $row_class_tag = (! empty($row_class)) ? " class=\"$row_class\" " : "";
*/
public static function show_table_from_generator(
array $header_fields,
\Iterator $records,
bool $escape = true
): void {
$nl = PHP_EOL; $nl = PHP_EOL;
echo "{$nl}<table border='1' cellpadding='1' cellspacing='1'>{$nl}"; $ret = self::do_echo("\t<tr{$row_class_tag}>{$nl}", $echo);
echo "\t<tr>{$nl}";
foreach($header_fields as $header_field) { $errors = [];
$field = ($escape) ? \bs_tts\safer_io::h($header_field) : $header_field; foreach($record as $html) {
echo "\t\t<th>{$field}</th>{$nl}"; $key = $html['name'] ?? "";
$value = $html['html'];
if (\CodeHydrater\common::get_count($html['errors'])) {
$errors[$key] = $html['errors'][$key];
} }
echo "\t</tr>{$nl}";
foreach($records as $record) { $td = $value ?? "";
echo "\t<tr>{$nl}"; if (is_string($td)) {
$cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td;
} else {
$cell = (string) $td;
}
$ret .= self::do_echo("\t\t<td{$cell_class_tag}>{$cell}</td>{$nl}", $echo);
}
$ret .= self::do_echo("\t</tr>{$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<tr{$row_class_tag}>{$nl}", $echo);
foreach($record as $key => $value) { foreach($record as $key => $value) {
if (! is_string($key)) { if (! is_string($key)) {
continue; // Remove Duplicate records continue; // Remove Duplicate records
} }
$td = $value ?? ""; $td = $value ?? "";
if (is_string($td)) { if (is_string($td)) {
$cell = ($escape) ? \bs_tts\safer_io::h($td) : $td; $cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td;
} else { } else {
$cell = (string) $td; $cell = (string) $td;
} }
echo "\t\t<td>{$cell}</td>{$nl}"; $ret .= self::do_echo("\t\t<td{$cell_class_tag}>{$cell}</td>{$nl}", $echo);
}
$ret .= self::do_echo("\t</tr>{$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'] . "</table>" . PHP_EOL);
return $ret;
default:
foreach($records as $record) {
self::make_cell_table_helper($record, $options);
} }
echo "\t</tr>{$nl}";
} }
echo "</table>{$nl}"; $ret .= self::do_echo("</table>" . 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) { if ($errors) {
$message = "Please correct the following errors: "; $message = "Please correct the following errors: ";
$i = 0; $i = 0;
@ -135,7 +210,8 @@ final class html {
$i++; $i++;
$message .= "<br/>{$i}) " . $error; $message .= "<br/>{$i}) " . $error;
} }
echo $message; $ret .= self::do_echo($message, $echo);
} }
return $ret;
} }
} }

@ -18,6 +18,9 @@ final class memory_usage {
* @retval string of unit size for bytes * @retval string of unit size for bytes
*/ */
public static function convert_bytes($size) { public static function convert_bytes($size) {
if ($size === null) {
return 0;
}
$unit=array('b','kb','mb','gb','tb','pb'); $unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; 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 * @param bool $echo (default of true) - true: displays, false: returns data
*/ */
public static function get_memory_stats($echo = true) { 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')); $check = common::get_bool(misc::request_var('debug'));
@ -44,13 +51,6 @@ final class memory_usage {
$diff_peak = $peak - $mem_baseline; $diff_peak = $peak - $mem_baseline;
$s_diff_peak = self::convert_bytes($diff_peak); $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_headers = array('Memory Item', 'Size');
$a_fields[] = array('On-StartUp', $s_startup_mem); $a_fields[] = array('On-StartUp', $s_startup_mem);
$a_fields[] = array('Currently', $s_current); $a_fields[] = array('Currently', $s_current);
@ -58,13 +58,10 @@ final class memory_usage {
$a_fields[] = array('&nbsp;', '&nbsp;'); $a_fields[] = array('&nbsp;', '&nbsp;');
$a_fields[] = array('PEAK', $s_peak); $a_fields[] = array('PEAK', $s_peak);
$a_fields[] = array('<i>Total Diff PEAK</i>', "<i>{$s_diff_peak}</i>"); $a_fields[] = array('<i>Total Diff PEAK</i>', "<i>{$s_diff_peak}</i>");
}
if ($echo === true) { $options['escape'] = false;
html::show_table($a_headers, $a_fields, false); $options['echo'] = $echo;
} else { return html::show_table($a_headers, $a_fields, $options);
return $a_fields;
}
} }
} }

@ -53,8 +53,11 @@ trait form_validator {
} }
private static function is_required(array $data, string $field): bool { private static function is_required(array $data, string $field): bool {
$r = self::check_if_null_or_false($data, $field); // Don't use check_if_null_or_false here, it should always fail on false or null!
if (is_bool($r)) return $r; $r = $data[$field] ?? null;
if ($r === null || $r === false) {
return false; // It's a required field, so fail
}
if (common::get_count($r)) { if (common::get_count($r)) {
return false; // Should not be an array here return false; // Should not be an array here

@ -122,7 +122,7 @@ final class view {
* @param string $render_page * @param string $render_page
* @return bool was found * @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)) { if (! empty($render_page)) {
$render_page = str_replace('.php', '', $render_page); $render_page = str_replace('.php', '', $render_page);
$templ = "/templates/{$render_page}"; $templ = "/templates/{$render_page}";

Loading…
Cancel
Save